Config.cpp 16 KB

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