Config.cpp 17 KB

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