Config.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // STL includes
  2. #include <assert.h>
  3. #include <fstream>
  4. #include <map>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <unistd.h> //for getpwd() under linux
  9. #include <sys/param.h> //for MAXPATHLEN - the maximal path length
  10. // nice-core includes
  11. #include "core/basics/Exception.h"
  12. #include "core/basics/Config.h"
  13. #include "core/basics/ossettings.h"
  14. #include "core/basics/StringTools.h"
  15. #include "core/basics/FileName.h"
  16. using ::std::endl;
  17. using ::std::vector;
  18. using ::std::string;
  19. using ::std::istream;
  20. using ::std::ostream;
  21. using ::std::ios;
  22. using ::std::map;
  23. #undef DEBUGCONFIG
  24. #define DEBUGPRINT printf
  25. namespace NICE {
  26. Config::Config ()
  27. {
  28. ioUntilEndOfFile = true;
  29. }
  30. Config::Config ( const std::string & configfn )
  31. {
  32. if ( configfn.size() >0 ) {
  33. read(configfn);
  34. }
  35. ioUntilEndOfFile = true;
  36. m_sConfigFilename = configfn;
  37. }
  38. Config::Config ( int argc,
  39. char **argv )
  40. {
  41. readFromArguments ( argc, argv );
  42. std::string configfile = gS("main", "config", "" );
  43. NICE::FileName t_ConfigFilename( configfile );
  44. // check for relative path correction:
  45. // if dataset is a relative path, then make it absolute using the
  46. // currend working directory
  47. if( t_ConfigFilename.isRelative() )
  48. {
  49. char sCWDPath[MAXPATHLEN];
  50. getcwd(sCWDPath, MAXPATHLEN);
  51. t_ConfigFilename.set( string(sCWDPath) + "/" + configfile );
  52. t_ConfigFilename.convertToRealPath();
  53. configfile = t_ConfigFilename.str();
  54. }
  55. m_sConfigFilename = configfile;
  56. std::cerr << "configfile: " << configfile << std::endl;
  57. ioUntilEndOfFile = gB("main", "ioUntilEndOfFile", true );
  58. if ( configfile.size() > 0 )
  59. readWithoutOverwrite ( configfile.c_str(), CONFIG_DO_NOT_OVERWRITE_VALUES /*do not overwrite values*/ );
  60. }
  61. Config::Config ( const Config & conf ) : Persistent()
  62. {
  63. ioUntilEndOfFile = true;
  64. m_sConfigFilename = conf.m_sConfigFilename;
  65. confB.copyFrom ( conf.confB );
  66. confD.copyFrom ( conf.confD );
  67. confI.copyFrom ( conf.confI );
  68. confS.copyFrom ( conf.confS );
  69. }
  70. Config::~Config()
  71. {
  72. }
  73. void Config::clear()
  74. {
  75. #if defined DEBUGCONFIG
  76. DEBUGPRINT( "Config: clear ...\n" );
  77. #endif
  78. confB.clear();
  79. confD.clear();
  80. confI.clear();
  81. confS.clear();
  82. }
  83. void Config::addKeyValuePair ( const std::string & block,
  84. const std::string & key,
  85. const std::string & value )
  86. {
  87. vector<string> submatches;
  88. #if defined DEBUGCONFIG
  89. DEBUGPRINT( "Config: analyzing value %s\n", value.c_str() );
  90. #endif
  91. if ( StringTools::regexMatch ( value, "^ *([-[:digit:]]+) *;?$", submatches ) &&
  92. (submatches.size() == 2) ) {
  93. #if defined DEBUGCONFIG
  94. DEBUGPRINT ( "Config: integer value\n");
  95. #endif
  96. int v;
  97. v = StringTools::convert<int> ( submatches[1] );
  98. confI.store ( block, key, v );
  99. } else if ( value.compare("true") == 0 ) {
  100. #if defined DEBUGCONFIG
  101. DEBUGPRINT( "Config: boolean value\n");
  102. #endif
  103. confB.store ( block, key, true );
  104. } else if ( value.compare("false") == 0 ) {
  105. #if defined DEBUGCONFIG
  106. DEBUGPRINT ( "Config: boolean value\n");
  107. #endif
  108. confB.store ( block, key, false );
  109. } else if ( StringTools::regexMatch ( value, "^ *([-e.[:digit:]]+) *;?$", submatches ) &&
  110. (submatches.size() == 2) )
  111. {
  112. double v;
  113. if ( ! StringTools::convert<double> ( submatches[1], v ) )
  114. {
  115. DEBUGPRINT ( "Config: please ask Erik to debug this part of Config.cpp\n");
  116. exit(-1);
  117. }
  118. #if defined DEBUGCONFIG
  119. DEBUGPRINT ( "Config: double value\n");
  120. #endif
  121. confD.store ( block, key, v );
  122. } else {
  123. #if defined DEBUGCONFIG
  124. DEBUGPRINT ( "Config: string value\n");
  125. #endif
  126. string trimValue = value;
  127. StringTools::trimbounds ( trimValue, '\"' );
  128. StringTools::trimbounds ( trimValue, '\'' );
  129. confS.store ( block, key, trimValue );
  130. }
  131. }
  132. void Config::readFromArguments ( int argc, char **argv )
  133. {
  134. std::string section;
  135. std::string key;
  136. std::string value;
  137. ioUntilEndOfFile = true;
  138. for ( int i = 1 ; i < argc ; i++ )
  139. {
  140. if ( argv[i] == NULL ) break;
  141. std::string arg ( argv[i] );
  142. vector<string> submatches;
  143. bool match = false;
  144. match = StringTools::regexMatch ( arg, "^--?([[:alpha:]][[:alpha:][:digit:]_-]*):([[:alpha:][:digit:]_-]+)", submatches );
  145. if ( (match) && (submatches.size() == 3) ) {
  146. if ( key.size() > 0 ) {
  147. addArgBoolean ( section, key );
  148. }
  149. section = submatches[1];
  150. key = submatches[2];
  151. continue;
  152. }
  153. match = StringTools::regexMatch ( arg, "^--?([[:alpha:]][[:alpha:][:digit:]_-]*)", submatches );
  154. if ( (match) && (submatches.size() == 2) ) {
  155. if ( key.size() > 0 )
  156. {
  157. addArgBoolean ( section, key );
  158. }
  159. section = "main";
  160. key = submatches[1];
  161. continue;
  162. }
  163. value = string(argv[i]);
  164. if ( key.size() <= 0 )
  165. {
  166. // add to more options
  167. moreOptions.push_back ( value );
  168. continue;
  169. }
  170. #if defined DEBUGCONFIG
  171. cout << "Config: add command line option: " << section << ":" << key << " = " << value << endl;
  172. #endif
  173. addKeyValuePair ( section, key, value );
  174. key = "";
  175. }
  176. if ( key.size() > 0 )
  177. addArgBoolean ( section, key );
  178. }
  179. void Config::addArgBoolean ( const std::string & section, const std::string & key )
  180. {
  181. vector<string> submatches;
  182. if ( StringTools::regexMatch ( key, "^no-(.*)$", submatches )
  183. && (submatches.size() == 2) )
  184. {
  185. confB.store ( section, submatches[1], false );
  186. } else {
  187. confB.store ( section, key, true );
  188. }
  189. }
  190. void Config::restore (istream & is, int format)
  191. {
  192. std::string block = "main";
  193. std::string key;
  194. std::string value;
  195. std::string line;
  196. int count = 0;
  197. //check for the first word
  198. //if this is "Config::store--PreciseTermination" then we do not read until eof
  199. //but bis the precise termination statement "Config::store--done" arises
  200. std::string firstWord;
  201. if (! is.eof() )
  202. {
  203. is >> firstWord;
  204. if ( firstWord.compare( "Config::store--PreciseTermination" ) == 0)
  205. {
  206. ioUntilEndOfFile = false;
  207. }
  208. else
  209. ioUntilEndOfFile = true;
  210. }
  211. //jump to the beginning of the file
  212. is.clear();
  213. is.seekg(0, ios::beg);
  214. //we keep the while-loop untached, but break out of ioUntilEndOfFile is false and we reached the termination-statement
  215. while (! is.eof())
  216. {
  217. size_t i=0;
  218. if ( !getline(is, line) )
  219. break;
  220. //do we have to check for the precise termination statement and reaches it yet?
  221. if ( (!ioUntilEndOfFile) && (line.compare("Config::store--done") == 0) )
  222. break;
  223. if ( line.size() <= 0 )
  224. continue;
  225. size_t len = line.size();
  226. count++;
  227. if ( (len < 1) || (line[0] == '#' ) )
  228. continue;
  229. line = StringTools::chomp ( line );
  230. len = line.size();
  231. #if defined DEBUGCONFIG
  232. DEBUGPRINT ("Config: (%d) '%s' (len = %d) / %s\n", static_cast<int>(count), line.c_str(), static_cast<int>(len),
  233. block.c_str());
  234. #endif
  235. while ( ( (line[i] == '\t') || (line[i]==' ') ) && (i<len)) i++;
  236. if ( i == len ) continue;
  237. if ( line[i] == '[' ) {
  238. size_t j = i+1;
  239. while ((line[j]!=']') && (j<len)) j++;
  240. if ( (j == len) || (i==j-1) ) continue;
  241. block = line.substr( i+1, j-i-1 );
  242. #if defined DEBUGCONFIG
  243. DEBUGPRINT ("Config: reading block %s\n", block.c_str());
  244. #endif
  245. StringTools::normalize_string(block);
  246. continue;
  247. }
  248. if ( line[i] == '%' ) {
  249. size_t j = i+1;
  250. while ((line[j]!='%') && (j<len)) j++;
  251. if ( (j == len) || (i==j-1) ) continue;
  252. std::string includefile = line.substr( i+1, j-i-1 );
  253. #if defined DEBUGCONFIG
  254. DEBUGPRINT ("Config: including config file %s\n", includefile.c_str());
  255. #endif
  256. StringTools::normalize_string ( includefile );
  257. readWithoutOverwrite ( includefile.c_str(), format );
  258. continue;
  259. }
  260. size_t p = i;
  261. while ( (line[p]!='=') && (p<len) ) p++;
  262. if ( (p >= len-1) || (p<=i) ) continue;
  263. key = line.substr( i, p-i );
  264. value = line.substr( p+1 ); // with only one argument, substr copies from the specified position until the end of the string
  265. StringTools::normalize_string(value);
  266. StringTools::normalize_string(key);
  267. // transform(key.begin(), key.end(), key.begin(), ::tolower);
  268. #if defined DEBUGCONFIG
  269. DEBUGPRINT ("Config: found key value pair (%s,%s) in section %s\n",
  270. key.c_str(), value.c_str(), block.c_str());
  271. #endif
  272. if ( (format == CONFIG_OVERWRITE_VALUES) || ( !keyExists(block, key) ) )
  273. addKeyValuePair ( block, key, value );
  274. }
  275. #if defined DEBUGCONFIG
  276. DEBUGPRINT ("Config: successfully loaded.\n" );
  277. DEBUGPRINT ("Config: %d double values\n", static_cast<int>(confD.size()) );
  278. DEBUGPRINT ("Config: %d integer values\n", static_cast<int>(confI.size()) );
  279. DEBUGPRINT ("Config: %d bool values\n", static_cast<int>(confB.size()) );
  280. DEBUGPRINT ("Config: %d std::string values\n", static_cast<int>(confS.size()) );
  281. #endif
  282. }
  283. bool Config::keyExists ( const std::string & block, const std::string & key ) const
  284. {
  285. return ( confS.keyExists ( block, key ) ||
  286. confI.keyExists ( block, key ) ||
  287. confB.keyExists ( block, key ) ||
  288. confD.keyExists ( block, key ) );
  289. }
  290. void Config::addHelp ( const std::string & block, const std::string & key,
  291. const std::string & helpText )
  292. {
  293. helpTexts[ block + "::" + key ] = helpText;
  294. }
  295. std::string Config::help ( const std::string & block, const std::string & key ) const
  296. {
  297. std::string k = block + "::" + key;
  298. map<string, string>::const_iterator i = helpTexts.find(k);
  299. if ( i == helpTexts.end() )
  300. return "no helping information for this variable, sorry!";
  301. else
  302. return i->second;
  303. }
  304. std::string Config::gS(const std::string & key) const
  305. {
  306. return gS(key, "main");
  307. }
  308. std::string Config::gS(const std::string & block, const std::string & key) const
  309. {
  310. std::string v ("");
  311. if ( confS.find(block, key, v) )
  312. {
  313. return v;
  314. }
  315. else
  316. {
  317. std::string errorMessage = "Config: setting " + block + "::" + key + " not found !\n";
  318. errorMessage += "\n \n Config: " + help(block, key) + "\n";
  319. throw NICE::Exception( errorMessage );
  320. }
  321. }
  322. std::string Config::gS(const std::string & block, const std::string & key, const std::string & defv) const
  323. {
  324. std::string v ("");
  325. if ( confS.find(block, key, v) ) {
  326. return v;
  327. } else {
  328. #if defined DEBUGCONFIG
  329. DEBUGPRINT("Config: Setting %s::%s not found using default value %s\n", block.c_str(), key.c_str(), defv.c_str() );
  330. #endif
  331. return defv;
  332. }
  333. }
  334. double Config::gD(const std::string & key) const
  335. {
  336. return gD(key, "main");
  337. }
  338. double Config::gD(const std::string & block, const std::string & key) const
  339. {
  340. double v = 0.0;
  341. if ( confD.find(block, key, v) ) {
  342. return v;
  343. } else {
  344. int vi;
  345. if ( confI.find(block, key, vi) ) {
  346. DEBUGPRINT("Config: Setting %s::%s should be double (please change in the config)\n", block.c_str(), key.c_str() );
  347. return static_cast<int>(vi);
  348. }
  349. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  350. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  351. exit(-1);
  352. return -1.0; // never reached
  353. }
  354. }
  355. double Config::gD(const std::string & block, const std::string & key, const double defv) const
  356. {
  357. double v = 0.0;
  358. if ( confD.find(block, key, v) ) {
  359. return v;
  360. } else {
  361. int vi;
  362. if ( confI.find(block, key, vi) ) {
  363. DEBUGPRINT("Config: Setting %s::%s should be double (please change in the config)\n", block.c_str(), key.c_str() );
  364. return static_cast<int>(vi);
  365. }
  366. #if defined DEBUGCONFIG
  367. DEBUGPRINT("Config: Setting %s::%s not found using default value %f\n", block.c_str(), key.c_str(), defv );
  368. #endif
  369. return defv;
  370. }
  371. }
  372. int Config::gI(const std::string & key) const
  373. {
  374. return gI(key, "main");
  375. }
  376. int Config::gI(const std::string & block, const std::string & key) const
  377. {
  378. int v = 0;
  379. if ( confI.find(block, key, v) ) {
  380. return v;
  381. } else {
  382. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  383. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  384. exit(-1);
  385. return 1; // never reached
  386. }
  387. }
  388. int Config::gI(const std::string & block, const std::string & key, int defv) const
  389. {
  390. int v = 0;
  391. if ( confI.find(block, key, v) ) {
  392. return v;
  393. } else {
  394. #if defined DEBUGCONFIG
  395. DEBUGPRINT("Config: Setting %s::%s not found using default value %d\n", block.c_str(), key.c_str(), defv );
  396. #endif
  397. return defv;
  398. }
  399. }
  400. bool Config::gB(const std::string & key) const
  401. {
  402. return gB(key, "main");
  403. }
  404. bool Config::gB(const std::string & block, const std::string & key) const
  405. {
  406. bool v = true;
  407. if ( confB.find(block, key, v) ) {
  408. return v;
  409. } else {
  410. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  411. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  412. exit(-1);
  413. return true; // never reached
  414. }
  415. }
  416. bool Config::gB(const std::string & block, const std::string & key, bool defv) const
  417. {
  418. bool v = true;
  419. if ( confB.find(block, key, v) ) {
  420. return v;
  421. } else {
  422. #if defined DEBUGCONFIG
  423. DEBUGPRINT("Config: Setting %s::%s not found using default value %d\n", block.c_str(), key.c_str(), defv );
  424. #endif
  425. return defv;
  426. }
  427. }
  428. void Config::getAllS ( const std::string & block, std::map< string, std::string > & list ) const
  429. {
  430. confS.getAll ( block, list );
  431. }
  432. void Config::getAllD ( const std::string & block, std::map< string, double > & list ) const
  433. {
  434. confD.getAll ( block, list );
  435. }
  436. void Config::getAllI ( const std::string & block, std::map< string, int > & list ) const
  437. {
  438. confI.getAll ( block, list );
  439. }
  440. void Config::getAllB ( const std::string & block, std::map< string, bool > & list ) const
  441. {
  442. confB.getAll ( block, list );
  443. }
  444. void Config::sD(const std::string & block, const std::string & key, const double defv)
  445. {
  446. confD.store ( block, key, defv );
  447. }
  448. void Config::sI(const std::string & block, const std::string & key, const int defv)
  449. {
  450. confI.store ( block, key, defv );
  451. }
  452. void Config::sB(const std::string & block, const std::string & key, const bool defv)
  453. {
  454. confB.store ( block, key, defv );
  455. }
  456. void Config::sS(const std::string & block, const std::string & key, const std::string & defv)
  457. {
  458. confS.store ( block, key, defv );
  459. }
  460. void Config::store (ostream & os, int format) const
  461. {
  462. if (!ioUntilEndOfFile)
  463. {
  464. //let's give us a hint that we do not have to restore until end of file but until "Config::store--done"
  465. os << "Config::store--PreciseTermination" << std::endl;
  466. }
  467. os << "# -----------------------------------------" << endl;
  468. os << "# | Config |" << endl;
  469. os << "# -----------------------------------------" << endl;
  470. os << endl << "# ------------- double values" << endl;
  471. confD.store( os, format );
  472. os << endl << "# ------------- boolean values" << endl;
  473. confB.store( os, format );
  474. os << endl << "# ------------- integer values" << endl;
  475. confI.store( os, format );
  476. os << endl << "# ------------- string values" << endl;
  477. confS.store( os, format );
  478. os << "# -----------------------------------------" << endl;
  479. if (!ioUntilEndOfFile)
  480. {
  481. os << "Config::store--done" << std::endl;
  482. }
  483. }
  484. void Config::getAllBlocks ( std::set< std::string > & list ) const
  485. {
  486. list.clear();
  487. confB.getAllBlocks ( list );
  488. confD.getAllBlocks ( list );
  489. confS.getAllBlocks ( list );
  490. confI.getAllBlocks ( list );
  491. }
  492. string Config::getAbsoluteFilenameRelativeToThisConfig(const string &p_Filename) const
  493. {
  494. if( m_sConfigFilename.empty() )
  495. return p_Filename;
  496. NICE::FileName t_DatasetFilename( p_Filename );
  497. if( !t_DatasetFilename.isRelative() )
  498. {
  499. return p_Filename;
  500. }
  501. NICE::FileName t_ConfigFilename( this->m_sConfigFilename );
  502. return t_ConfigFilename.extractPath().str() + p_Filename;
  503. }
  504. }