Config.cpp 15 KB

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