Config.cpp 16 KB

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