37 #include <boost/tokenizer.hpp> 38 #include "boost/lexical_cast.hpp" 39 #include <boost/algorithm/string.hpp> 43 #define FOR_INI_ITEMS(i) for((i)=read_ini->ini_item;(i)!=NULL;(i)=(i)->next) 71 OLD_ASSERT( ini,
"Failed to open the ini file: %s",fname);
75 sscanf( line,
"%s",
string );
78 if (strlen(
string)==0)
84 strcpy(section,section_ptr);
88 if(strchr(line,
'=') == NULL)
92 tmp = strstr(line,
"#");
101 sscanf(tmp,
"%s",
string);
102 if(strlen(
string) == 0)
111 if(strlen(tmp) == 0){
124 prev =
new_item(prev,section,key,value);
135 while((
string[0] ==
' ') || (
string[0] ==
'\t')){
138 i = strlen(
string) - 1;
139 while((
string[i] ==
' ') || (
string[i] ==
'\t') || (
string[i] ==
'\n') || (
string[i] ==
'\r')){
151 if((section != NULL) && (key != NULL) && (value != NULL)){
156 ini_item->
prev = NULL;
160 ini_item->
next = NULL;
161 prev->
next = ini_item;
178 if(line == NULL)
return NULL;
180 if( (line[0] ==
'[') && (line[strlen(line)-1] ==
']') && (strlen(line) > 2))
195 xprintf(
Err,
"OptGetXXX input interface is not supported anymore.\n");
198 const char *rc = NULL;
202 if( (!strcmp(ini_item->
section,section)) && (!strcmp(ini_item->
key,key)) ){
203 rc = ini_item->
value;
209 xprintf(
UsrErr,
"Required parameter: section '%s' key '%s' is not given.\n",section,key);
236 if (sscanf(str,
"%ld",&res) == 0) {
237 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to integer parameter: [%s] %s.\n",section,key);
238 xprintf(
PrgErr,
"Default value %s of parameter: [%s] %s is not an integer.\n",defval,section,key);
254 if (sscanf(str,
"%lg",&res) == 0) {
255 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to double parameter: [%s] %s.\n",section,key);
256 if (sscanf(defval,
"%lg",&res) == 0)
257 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an double.\n",defval,section,key);
273 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
274 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
277 if (defval == NULL)
xprintf(
UsrErr,
"Required parameter: [%s] %s is not a boolen.\n",section,key);
279 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
280 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
281 else xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not a boolean.\n",defval,section,key);
324 char * tmp_str =
OptGetStr(section,key,defval);
325 std::string str = tmp_str;
327 boost::tokenizer<boost::char_separator<char> > line_tokenizer(str, boost::char_separator<char>(
"\t "));
328 boost::tokenizer<boost::char_separator<char> >::iterator tok;
332 for( tok = line_tokenizer.begin();
333 tok != line_tokenizer.end();
335 value = boost::lexical_cast<
double> (*tok);
336 array.push_back(value);
338 }
catch (boost::bad_lexical_cast &) {
339 xprintf(
UsrErr,
"INI file: Can not convert token `%s` of key `[%s] %s` to double.\n", (*tok).c_str(),
section,
key);
354 for(i = 1; i < ArrSize; i++){
355 if (sscanf(str,
"%d",&res) == 0) {
356 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert %d. ini-file entry to integer parameter: [%s] %s.\n",i,section,key);
357 if (sscanf(defval,
"%d",&res) == 0)
358 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an integer.\n",defval,section,key);
360 *(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.