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) 64 const char *
const whitespace_delim=
" \t\r\n";
66 rc =
xstrtok( s, whitespace_delim, position);
79 char *
xstrtok(
char *s1,
const char *delim,
int position )
82 static char * full_string = NULL;
83 static int token_count;
85 OLD_ASSERT(!( delim == NULL ),
"NULL pointer as delimiter in xstrtok()\n");
91 full_string = (
char *)xmalloc(
LINE_SIZE );
99 INPUT_CHECK( token_count == position || position < 0,
"Requested position %d dosn't match token position %d", position, token_count);
100 rc = strtok( s1, delim );
103 INPUT_CHECK(!( rc == NULL ),
"Missing token no. %d: original string '%s' with delimiters '%s'\n", token_count, full_string, delim );
129 ASSERT(ini)(fname).error(
"Failed to open the ini file");
133 sscanf( line,
"%s",
string );
136 if (strlen(
string)==0)
142 strcpy(section,section_ptr);
146 if(strchr(line,
'=') == NULL)
150 tmp = strstr(line,
"#");
153 if(strlen(line) == 0)
159 sscanf(tmp,
"%s",
string);
160 if(strlen(
string) == 0)
163 key = xstrcpy(
string);
169 if(strlen(tmp) == 0){
174 value = xstrcpy(tmp);
182 prev =
new_item(prev,section,key,value);
193 while((
string[0] ==
' ') || (
string[0] ==
'\t')){
196 i = strlen(
string) - 1;
197 while((
string[i] ==
' ') || (
string[i] ==
'\t') || (
string[i] ==
'\n') || (
string[i] ==
'\r')){
209 if((section != NULL) && (key != NULL) && (value != NULL)){
214 ini_item->
prev = NULL;
218 ini_item->
next = NULL;
219 prev->
next = ini_item;
222 ini_item->
section = xstrcpy(section);
223 ini_item->
key = xstrcpy(key);
224 ini_item->
value = xstrcpy(value);
236 if(line == NULL)
return NULL;
238 if( (line[0] ==
'[') && (line[strlen(line)-1] ==
']') && (strlen(line) > 2))
253 xprintf(
Err,
"OptGetXXX input interface is not supported anymore.\n");
256 const char *rc = NULL;
260 if( (!strcmp(ini_item->
section,section)) && (!strcmp(ini_item->
key,key)) ){
261 rc = ini_item->
value;
267 xprintf(
UsrErr,
"Required parameter: section '%s' key '%s' is not given.\n",section,key);
294 if (sscanf(str,
"%ld",&res) == 0) {
295 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to integer parameter: [%s] %s.\n",section,key);
296 xprintf(
PrgErr,
"Default value %s of parameter: [%s] %s is not an integer.\n",defval,section,key);
312 if (sscanf(str,
"%lg",&res) == 0) {
313 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert to double parameter: [%s] %s.\n",section,key);
314 if (sscanf(defval,
"%lg",&res) == 0)
315 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an double.\n",defval,section,key);
331 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
332 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
335 if (defval == NULL)
xprintf(
UsrErr,
"Required parameter: [%s] %s is not a boolen.\n",section,key);
337 if ( boost::iequals(str,
"yes") || boost::iequals(str,
"true") || boost::iequals(str,
"1") ) res=
true;
338 else if ( boost::iequals(str,
"no") || boost::iequals(str,
"false") || boost::iequals(str,
"0") ) res=
false;
339 else xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not a boolean.\n",defval,section,key);
352 ASSERT(fname).error(
"NULL file name.\n");
382 char * tmp_str =
OptGetStr(section,key,defval);
383 std::string str = tmp_str;
385 boost::tokenizer<boost::char_separator<char> > line_tokenizer(str, boost::char_separator<char>(
"\t "));
386 boost::tokenizer<boost::char_separator<char> >::iterator tok;
390 for( tok = line_tokenizer.begin();
391 tok != line_tokenizer.end();
393 value = boost::lexical_cast<
double> (*tok);
394 array.push_back(value);
396 }
catch (boost::bad_lexical_cast &) {
397 xprintf(
UsrErr,
"INI file: Can not convert token `%s` of key `[%s] %s` to double.\n", (*tok).c_str(),
section,
key);
412 for(i = 1; i < ArrSize; i++){
413 if (sscanf(str,
"%d",&res) == 0) {
414 if (defval == NULL)
xprintf(
UsrErr,
"Can not convert %d. ini-file entry to integer parameter: [%s] %s.\n",i,section,key);
415 if (sscanf(defval,
"%d",&res) == 0)
416 xprintf(
PrgErr,
"Default value \"%s\" of parameter: [%s] %s is not an integer.\n",defval,section,key);
418 *(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)
#define INPUT_CHECK(i,...)
Debugging macros.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
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.
std::string sprintf(CStringRef format, ArgList args)
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)
char * xstrtok(char *s, int position)
STRTOK WITH ERROR HANDLING and whitespace delimiters.
double OptGetDbl(const char *section, const char *key, const char *defval)
static void make_ini_item_list(const char *fname)
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.