49 #include <boost/tokenizer.hpp>
50 #include "boost/lexical_cast.hpp"
51 #include <boost/algorithm/string.hpp>
55 #define FOR_INI_ITEMS(i) for((i)=read_ini->ini_item;(i)!=NULL;(i)=(i)->next)
83 ASSERT( ini,
"Failed to open the ini file: %s",fname);
87 sscanf( line,
"%s",
string );
90 if (strlen(
string)==0)
96 strcpy(section,section_ptr);
100 if(strchr(line,
'=') == NULL)
104 tmp = strstr(line,
"#");
106 sprintf(tmp,
"%s",
"");
107 if(strlen(line) == 0)
113 sscanf(tmp,
"%s",
string);
114 if(strlen(
string) == 0)
123 if(strlen(tmp) == 0){
136 prev =
new_item(prev,section,key,value);
147 while((
string[0] ==
' ') || (
string[0] ==
'\t')){
150 i = strlen(
string) - 1;
151 while((
string[i] ==
' ') || (
string[i] ==
'\t') || (
string[i] ==
'\n') || (
string[i] ==
'\r')){
163 if((section != NULL) && (key != NULL) && (value != NULL)){
168 ini_item->
prev = NULL;
172 ini_item->
next = NULL;
173 prev->
next = ini_item;
190 if(line == NULL)
return NULL;
192 if( (line[0] ==
'[') && (line[strlen(line)-1] ==
']') && (strlen(line) > 2))
207 xprintf(
Err,
"OptGetXXX input interface is not supported anymore.\n");
210 const char *rc = NULL;
214 if( (!strcmp(ini_item->
section,section)) && (!strcmp(ini_item->
key,key)) ){
215 rc = ini_item->
value;
221 xprintf(
UsrErr,
"Required parameter: section '%s' key '%s' is not given.\n",section,key);
248 if (sscanf(str,
"%ld",&res) == 0) {
249 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to integer parameter: [%s] %s.\n",section,key);
250 xprintf(
PrgErr,
"Default value %s of parameter: [%s] %s is not an integer.\n",defval,section,key);
266 if (sscanf(str,
"%lg",&res) == 0) {
267 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to double parameter: [%s] %s.\n",section,key);
268 if (sscanf(defval,
"%lg",&res) == 0)
269 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an double.\n",defval,section,key);
285 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
286 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
289 if (defval == NULL)
xprintf(
UsrErr,
"Required parameter: [%s] %s is not a boolen.\n",section,key);
291 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
292 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
293 else xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not a boolean.\n",defval,section,key);
306 ASSERT( fname,
"NULL file name\n");
336 char * tmp_str =
OptGetStr(section,key,defval);
337 std::string str = tmp_str;
339 boost::tokenizer<boost::char_separator<char> > line_tokenizer(str, boost::char_separator<char>(
"\t "));
340 boost::tokenizer<boost::char_separator<char> >::iterator tok;
344 for( tok = line_tokenizer.begin();
345 tok != line_tokenizer.end();
347 value = boost::lexical_cast<
double> (*tok);
348 array.push_back(value);
350 }
catch (boost::bad_lexical_cast &) {
351 xprintf(
UsrErr,
"INI file: Can not convert token `%s` of key `[%s] %s` to double.\n", (*tok).c_str(),
section,
key);
366 for(i = 1; i < ArrSize; i++){
367 if (sscanf(str,
"%d",&res) == 0) {
368 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert %d. ini-file entry to integer parameter: [%s] %s.\n",i,section,key);
369 if (sscanf(defval,
"%d",&res) == 0)
370 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an integer.\n",defval,section,key);
372 *(Array + (i-1)*
sizeof(
double)) = res;
struct Ini_item * ini_item
void OptGetDblArray(const char *section, const char *key, const char *defval, std::vector< double > &array)
char * xfgets(char *s, int n, FILE *in)
FGETS WITH ERROR HANDLING and line count.
bool OptGetBool(const char *section, const char *key, const char *defval)
char * xstrcpy(const char *src)
MAKE BRAND NEW COPY OF STRING.
I/O functions with filename storing, able to track current line in opened file. All standard stdio fu...
char * OptGetFileName(const char *section, const char *key, const char *defval)
static struct Read_ini * read_ini
void OptGetIntArray(const char *section, const char *key, const char *defval, int ArrSize, int *Array)
void OptionsInit(const char *fname)
Load options file.
static char * section_test(char *section)
static struct Ini_item * new_item(struct Ini_item *prev, char *section, char *key, char *value)
long int OptGetInt(const char *section, const char *key, const char *defval)
double OptGetDbl(const char *section, const char *key, const char *defval)
void * xmalloc(size_t size)
Memory allocation with checking.
static void make_ini_item_list(const char *fname)
char * xstrtok(char *s, int position)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
static char * strip_spaces(char *string)
FILE * xfopen(const std::string &fname, const char *mode)
char * OptGetStr(const char *section, const char *key, const char *defval)
Create new string from selected variable from ini file.