31 #include "utilitaires.h" 46 size_t size, read_size;
48 if( (fp = fopen (fname,
"r")) == NULL)
50 cout <<
"ERROR: Could not open config-file: '" << fname <<
"'\n";
57 data =
static_cast<char*
> (
MyMalloc (size+10));
58 read_size = fread ( data, 1, size, fp);
59 data [read_size] =
'\0';
77 static char *prev_fname = NULL;
78 static char *data = NULL;
83 if ( prev_fname && !strcmp(prev_fname, fname) )
99 #define FMT_STRING "string" // reading in strings needs some special treatment 110 read_variable (
const char *fname,
const char *var_name,
char *fmt,
void *varp)
113 char *seek, *pos, *bol;
125 if ( (pos = strstr(seek, var_name)) == NULL)
127 seek = pos + strlen (var_name);
135 after = *(pos+strlen(var_name));
137 if ( isalnum(before) || isalnum(after) || (before ==
'_') || (after ==
'_') )
141 bol = (pos > data) ? pos - 1 : data;
142 while ( (bol > data) && (*bol !=
'\n') )
144 if ( *bol ==
'\n' ) bol++;
148 if ( strspn(bol,
" \t") !=
static_cast<size_t>(pos - bol) )
156 cout <<
"ERROR: variable " << var_name <<
" was not found in config-file!\n";
160 found += strlen (var_name);
163 found += strspn(found,
" \t=:");
168 if ( !strcmp(fmt, FMT_STRING) )
172 if ( (pos = strchr(found+1,
'"')) == NULL )
174 cout <<
"ERROR: no closing quotes found \n";
181 if ( (pos = strchr (found,
'\n')) == NULL)
182 pos = data + strlen(data);
186 char **cstr =
static_cast<char**
>(varp);
187 len = int(pos - found);
188 (*cstr) =
static_cast<char*
>(
MyMalloc(len+1));
189 strncpy ((*cstr), found, len);
194 ret = sscanf (found, fmt, varp);
197 if ( (ret == 0) || (ret == EOF) )
199 cout <<
"WARNING: Variable " << var_name <<
" was not readable using the format '"<<fmt<<
"'\n";
213 int ret =
read_variable(fname, var_name, const_cast<char*>(
"%d"), &var);
215 cout <<
"DEBUG: " << var_name <<
" = " << var <<endl;
224 int ret =
read_variable(fname, var_name, const_cast<char*>(
"%d"), &buf);
226 var =
static_cast<bool>(buf);
228 cout <<
"DEBUG: " << var_name <<
" = " << var <<endl;
236 int ret =
read_variable(fname, var_name, const_cast<char*>(
"%lf"), &var);
238 cout <<
"DEBUG: " << var_name <<
" = " << var <<endl;
250 cout <<
"ERROR: return-string needs to be NULL in read_variable()\n";
254 int ret =
read_variable(fname, var_name, const_cast<char*>(FMT_STRING), &cstr);
256 if ((ret == OK) && cstr)
259 cout <<
"DEBUG: " << var_name <<
" = " << *str <<endl;
277 pos = int(ftell (f));
278 fseek (f, 0, SEEK_END);
279 end = int(ftell (f));
280 fseek (f, pos, SEEK_SET);
301 if ((Mptr = calloc (1, bytes)) == NULL)
303 cout <<
"MyMalloc("<< bytes <<
") did not succeed! Terminating...\n";
void * MyMalloc(long bytes)
'Improved' malloc that sets memory to 0 and also auto-terminates on error.
char * load_file(char *fname)
Read file into memory and returns pointer to data.
int FS_filelength(FILE *f)
A portable routine to determine the length of a file.
char * load_file_buffered(char *fname)
Returns pointer to data from a file using a buffer.
int read_variable(const char *fname, const char *var_name, char *fmt, void *varp)
Reads a variable from file.