Nilorea Library
C utilities for networking, threading, graphics
ex_fluid_config.c
1
13#include "ex_fluid_config.h"
14#include "nilorea/n_str.h"
15#include "nilorea/n_fluids.h"
17
18
19int load_app_state( char *state_filename , size_t *WIDTH , size_t *HEIGHT , bool *fullscreen , char **bgmusic , double *drawFPS , double *logicFPS , N_FLUID *fluid , int *threaded )
20{
21 __n_assert( state_filename , return FALSE );
22 __n_assert( fluid , return FALSE );
23
24 if( access( state_filename , F_OK ) != 0 )
25 {
26 n_log( LOG_INFO , "no app state %s to load !" , state_filename );
27 return FALSE ;
28 }
29
30 /* N_STR *data = NULL ;
31 data = file_to_nstr( state_filename );
32 if( !data )
33 {
34 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
35 return FALSE;
36 }
37
38 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
39 if (monitor_json == NULL)
40 {
41 const char *error_ptr = cJSON_GetErrorPtr();
42 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
43 cJSON_Delete( monitor_json );
44 free_nstr( &data );
45 return FALSE ;
46 }
47
48 cJSON *value = NULL ;
49 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "width" ); if( cJSON_IsNumber( value ) ){ (*WIDTH) = value -> valueint ; } else { n_log( LOG_ERR , "width is not a number"); }
50 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "height" ); if( cJSON_IsNumber( value ) ){ (*HEIGHT) = value -> valueint ; } else { n_log( LOG_ERR , "height is not a number"); }
51 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "fullscreen" ); if( cJSON_IsNumber( value ) ){ (*fullscreen) = value -> valueint ; } else { n_log( LOG_ERR , "fullscreen is not a number"); }
52 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "bg-music" ); if( cJSON_IsString( value ) ){ (*bgmusic) = strdup( value -> valuestring ); } else { n_log( LOG_ERR , "bg-music is not a string"); }
53 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "drawFPS" ); if( cJSON_IsNumber( value ) ){ (*drawFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "drawFPS is not a number"); }
54 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "logicFPS" ); if( cJSON_IsNumber( value ) ){ (*logicFPS) = value -> valuedouble ; } else { n_log( LOG_ERR , "logicFPS is not a number"); }
55
56 cJSON_Delete(monitor_json);
57 free_nstr( &data );
58 */
59 int errors = 0 ;
60 CONFIG_FILE *config = load_config_file( state_filename , &errors );
61 if( !config )
62 {
63 n_log( LOG_ERR, "Unable to load config file from %s", state_filename );
64 return FALSE ;
65 }
66 if( errors != 0 )
67 {
68 n_log( LOG_ERR, "There were %d errors in %s. Check the logs !", errors, state_filename );
69 return FALSE ;
70 }
71 /* default section, only one should be allocated. Let's test it ! */
72 int nb = get_nb_config_file_sections( config, "__DEFAULT__" );
73 for( int it = 0 ; it < nb ; it++ )
74 {
75 char *value = NULL ;
76 value = get_config_section_value( config, "__DEFAULT__", it, "width", 0 );
77 if( value ) (*WIDTH) = atoi( value );
78 value = get_config_section_value( config, "__DEFAULT__", it, "height", 0 );
79 if( value ) (*HEIGHT) = atoi( value );
80 value = get_config_section_value( config, "__DEFAULT__", it, "fullscreen", 0 );
81 if( value ) (*fullscreen) = atoi( value );
82 value = get_config_section_value( config, "__DEFAULT__", it, "bg-music", 0 );
83 if( value ) (*bgmusic) = strdup( value );
84 value = get_config_section_value( config, "__DEFAULT__", it, "drawFPS", 0 );
85 if( value ) (*drawFPS) = strtold( value , NULL );
86 value = get_config_section_value( config, "__DEFAULT__", it, "logicFPS", 0 );
87 if( value ) (*logicFPS) = strtold( value , NULL );
88 value = get_config_section_value( config, "__DEFAULT__", it, "numIters", 0 );
89 if( value ) fluid -> numIters = atoi( value );
90 value = get_config_section_value( config, "__DEFAULT__", it, "density", 0 );
91 if( value ) fluid -> density = strtold( value , NULL );
92 value = get_config_section_value( config, "__DEFAULT__", it, "gravity", 0 );
93 if( value ) fluid -> gravity = strtold( value , NULL );
94 value = get_config_section_value( config, "__DEFAULT__", it, "overRelaxation", 0 );
95 if( value ) fluid -> overRelaxation = strtold( value , NULL );
96 value = get_config_section_value( config, "__DEFAULT__", it, "fluid_production_percentage", 0 );
97 if( value ) fluid -> fluid_production_percentage = strtold( value , NULL );
98 value = get_config_section_value( config, "__DEFAULT__", it, "cScale", 0 );
99 if( value ) fluid -> cScale = strtold( value , NULL );
100 value = get_config_section_value( config, "__DEFAULT__", it, "threadedProcessing", 0 );
101 if( value ) (*threaded) = atoi( value );
102 }
103
104 /*data = file_to_nstr( state_filename );
105 if( !data )
106 {
107 n_log( LOG_ERR , "Error reading file %s, defaults will be used" , state_filename );
108 return FALSE;
109 }
110
111 cJSON *monitor_json = cJSON_Parse( _nstr( data ) );
112 if (monitor_json == NULL)
113 {
114 const char *error_ptr = cJSON_GetErrorPtr();
115 n_log( LOG_ERR , "%s: Error before: %s, defaults will be used", state_filename , _str( error_ptr ) );
116 cJSON_Delete( monitor_json );
117 free_nstr( &data );
118 return FALSE ;
119 }
120
121 cJSON *value = NULL ;
122 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "numIters" ); if( cJSON_IsNumber( value ) ){ fluid -> numIters = value -> valueint ; } else { n_log( LOG_ERR , "numIters is not a number"); }
123 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "density" ); if( cJSON_IsNumber( value ) ){ fluid -> density = value -> valuedouble ; } else { n_log( LOG_ERR , "density is not a number"); }
124 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "gravity" ); if( cJSON_IsNumber( value ) ){ fluid -> gravity = value -> valuedouble ; } else { n_log( LOG_ERR , "gravity is not a number"); }
125 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "overRelaxation" ); if( cJSON_IsNumber( value ) ){ fluid -> overRelaxation = value -> valuedouble ; } else { n_log( LOG_ERR , "overRelaxation is not a number"); }
126 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "fluid_production_percentage" ); if( cJSON_IsNumber( value ) ){ fluid -> fluid_production_percentage = value -> valuedouble ; }else { n_log( LOG_ERR , "fluid_production_percentage is not a number"); }
127 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "cScale" ); if( cJSON_IsNumber( value ) ){ fluid -> cScale = value -> valuedouble ; } else { n_log( LOG_ERR , "cScale is not a number"); }
128 value = cJSON_GetObjectItemCaseSensitive( monitor_json, "threadedProcessing" ); if( cJSON_IsNumber( value ) ){ (*threaded) = value -> valueint ; } else { n_log( LOG_ERR , "threadedProcessing is not a number"); }
129
130 cJSON_Delete(monitor_json);
131 free_nstr( &data ); */
132
133 destroy_config_file( &config );
134
135 return TRUE ;
136}
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
Definition: n_config_file.c:51
char * get_config_section_value(CONFIG_FILE *cfg_file, char *section_name, int section_position, char *entry, int entry_position)
Function to parse sections and get entries values.
int destroy_config_file(CONFIG_FILE **cfg_file)
Destroy a loaded config file.
int get_nb_config_file_sections(CONFIG_FILE *cfg_file, char *section_name)
Get the number of config file with section_name.
Structure of a config file.
Definition: n_config_file.h:41
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_ERR
error conditions
Definition: n_log.h:58
#define LOG_INFO
informational
Definition: n_log.h:64
structure of a fluid
Definition: n_fluids.h:54
Config file reading and writing.
fluid management port from "How to write an Eulerian fluid simulator with 200 lines of code",...
N_STR and string function declaration.