simulator_modules/memory.c
/** memory load json from file
 * given a file name it retrieves the json string and passes it to the parser
 * before it free's the json string and closes the file
 * @param file_name the absolute path to the file
 * @param parser a json_parser of type
 * @return -2 if it could not open the file or the result of the parser otherwise
 */
memory load json from file
given a file name it retrieves the json string and passes it to the parser
before it free's the json string and closes the file
@param file_name the absolute path to the file
@param parser a json_parser of type
@return -2 if it could not open the file or the result of the parser otherwise
memory load json from file given a file name it retrieves the json string and passes it to the parser before it free ' s the json string and closes the file @param file_name the absolute path to the file @param parser a json_parser of type @return - 2 if it could not open the file or the result of the parser otherwise
int32_t memory_load_json_from_file(char *file_name, json_parser parser)
{
	int32_t ret = 0;
	char file_path[256];
	snprintf(file_path, 256, "%s/%s", path_in_windows, file_name);
	FILE *stream;
	int32_t BUFFER_SIZE = 10;
	char *json_txt = malloc(BUFFER_SIZE);
	char buffer[BUFFER_SIZE];
	int32_t numread = 0;
	if ((fopen_s(&stream, file_path, "r+t")) == 0) 
	{
		printf("The file '%s' was opened\n", file_path);
		if (fgets(json_txt, BUFFER_SIZE, stream) != NULL)
		{
			numread = BUFFER_SIZE;
			printf("first characters: %s", json_txt);
			while (fgets(buffer, BUFFER_SIZE, stream) != NULL)
			{
				numread += BUFFER_SIZE;
				char *temp = malloc(numread);
				snprintf(temp, numread, "%s%s", json_txt, buffer);
				free(json_txt);
				json_txt = temp;
			}
		}
		printf("number of characters read %d", numread);
		printf("############### CONTENT ################\n\n\n");
		printf(json_txt);
		printf("############### CONTENT END ################\n\n\n");
		ret = parser(json_txt);
		free(json_txt);
		if (fclose(stream))
		{
			printf("The file '%s' was not closed\n", file_path);
			ret = 1;
		}
	}
	else
	{
		printf("The file %s was not opened", file_path);
		ret = 2;
	}
	int32_t numclosed = _fcloseall();
	printf("Number of files closed by _fcloseall: %u\n", numclosed);
	return ret;
}
int32_t memory_load_json_from_file ( char * file_name , json_parser parser ) { int32_t ret = 0 ; char file_path [ 256 ] ; snprintf ( file_path , 256 , " " , path_in_windows , file_name ) ; FILE * stream ; int32_t BUFFER_SIZE = 10 ; char * json_txt = malloc ( BUFFER_SIZE ) ; char buffer [ BUFFER_SIZE ] ; int32_t numread = 0 ; if ( ( fopen_s ( & stream , file_path , " " ) ) == 0 ) { printf ( " \n " , file_path ) ; if ( fgets ( json_txt , BUFFER_SIZE , stream ) != NULL ) { numread = BUFFER_SIZE ; printf ( " " , json_txt ) ; while ( fgets ( buffer , BUFFER_SIZE , stream ) != NULL ) { numread += BUFFER_SIZE ; char * temp = malloc ( numread ) ; snprintf ( temp , numread , " " , json_txt , buffer ) ; free ( json_txt ) ; json_txt = temp ; } } printf ( " " , numread ) ; printf ( " \n \n \n " ) ; printf ( json_txt ) ; printf ( " \n \n \n " ) ; ret = parser ( json_txt ) ; free ( json_txt ) ; if ( fclose ( stream ) ) { printf ( " \n " , file_path ) ; ret = 1 ; } } else { printf ( " " , file_path ) ; ret = 2 ; } int32_t numclosed = _fcloseall ( ) ; printf ( " \n " , numclosed ) ; return ret ; }
memory load json from file
 given a file name it retrieves the json string and passes it to the parser
 before it free's the json string and closes the file
 @param file_name the absolute path to the file
 @param parser a json_parser of type
 @return -2 if it could not open the file or the result of the parser otherwise
memory load json from file given a file name it retrieves the json string and passes it to the parser before it free ' s the json string and closes the file @param file_name the absolute path to the file @param parser a json_parser of type @return - 2 if it could not open the file or the result of the parser otherwise
// Open for write // C4996 // Close stream if it is not NULL // All other files are closed: