Config.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #if defined DEBUGCONFIG
  202. DEBUGPRINT ("Config: (%d) '%s' (len = %d) / %s\n", (int)count, line.c_str(), (int)len,
  203. block.c_str());
  204. #endif
  205. while ( ( (line[i] == '\t') || (line[i]==' ') ) && (i<len)) i++;
  206. if ( i == len ) continue;
  207. if ( line[i] == '[' ) {
  208. size_t j = i+1;
  209. while ((line[j]!=']') && (j<len)) j++;
  210. if ( (j == len) || (i==j-1) ) continue;
  211. block = line.substr( i+1, j-i-1 );
  212. #if defined DEBUGCONFIG
  213. DEBUGPRINT ("Config: reading block %s\n", block.c_str());
  214. #endif
  215. StringTools::normalize_string(block);
  216. continue;
  217. }
  218. if ( line[i] == '%' ) {
  219. size_t j = i+1;
  220. while ((line[j]!='%') && (j<len)) j++;
  221. if ( (j == len) || (i==j-1) ) continue;
  222. std::string includefile = line.substr( i+1, j-i-1 );
  223. #if defined DEBUGCONFIG
  224. DEBUGPRINT ("Config: including config file %s\n", includefile.c_str());
  225. #endif
  226. StringTools::normalize_string ( includefile );
  227. readWithoutOverwrite ( includefile.c_str(), format );
  228. continue;
  229. }
  230. size_t p = i;
  231. while ( (line[p]!='=') && (p<len) ) p++;
  232. if ( (p >= len-1) || (p<=i) ) continue;
  233. key = line.substr( i, p-i );
  234. value = line.substr( p+1, len-p );
  235. StringTools::normalize_string(value);
  236. StringTools::normalize_string(key);
  237. // transform(key.begin(), key.end(), key.begin(), ::tolower);
  238. #if defined DEBUGCONFIG
  239. DEBUGPRINT ("Config: found key value pair (%s,%s) in section %s\n",
  240. key.c_str(), value.c_str(), block.c_str());
  241. #endif
  242. if ( (format == CONFIG_OVERWRITE_VALUES) || ( !keyExists(block, key) ) )
  243. addKeyValuePair ( block, key, value );
  244. }
  245. #if defined DEBUGCONFIG
  246. DEBUGPRINT ("Config: successfully loaded.\n" );
  247. DEBUGPRINT ("Config: %d double values\n", (int)confD.size() );
  248. DEBUGPRINT ("Config: %d integer values\n", (int)confI.size() );
  249. DEBUGPRINT ("Config: %d bool values\n", (int)confB.size() );
  250. // refactor-nice.pl: check this substitution
  251. // old: DEBUGPRINT ("Config: %d string values\n", (int)confS.size() );
  252. DEBUGPRINT ("Config: %d std::string values\n", (int)confS.size() );
  253. #endif
  254. }
  255. bool Config::keyExists ( const std::string & block, const std::string & key ) const
  256. {
  257. return ( confS.keyExists ( block, key ) ||
  258. confI.keyExists ( block, key ) ||
  259. confB.keyExists ( block, key ) ||
  260. confD.keyExists ( block, key ) );
  261. }
  262. void Config::addHelp ( const std::string & block, const std::string & key,
  263. const std::string & helpText )
  264. {
  265. helpTexts[ block + "::" + key ] = helpText;
  266. }
  267. std::string Config::help ( const std::string & block, const std::string & key ) const
  268. {
  269. std::string k = block + "::" + key;
  270. map<string, string>::const_iterator i = helpTexts.find(k);
  271. if ( i == helpTexts.end() )
  272. return "no helping information for this variable, sorry!";
  273. else
  274. return i->second;
  275. }
  276. std::string Config::gS(const std::string & key) const
  277. {
  278. return gS(key, "main");
  279. }
  280. std::string Config::gS(const std::string & block, const std::string & key) const
  281. {
  282. std::string v ("");
  283. if ( confS.find(block, key, v) ) {
  284. return v;
  285. } else {
  286. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  287. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  288. exit(-1);
  289. return ""; // never reached
  290. }
  291. }
  292. // refactor-nice.pl: check this substitution
  293. // old: string Config::gS(const string & block, const string & key, const string & defv) const
  294. std::string Config::gS(const std::string & block, const std::string & key, const std::string & defv) const
  295. {
  296. // refactor-nice.pl: check this substitution
  297. // old: string v ("");
  298. std::string v ("");
  299. if ( confS.find(block, key, v) ) {
  300. return v;
  301. } else {
  302. #if defined DEBUGCONFIG
  303. DEBUGPRINT("Config: Setting %s::%s not found using default value %s\n", block.c_str(), key.c_str(), defv.c_str() );
  304. #endif
  305. return defv;
  306. }
  307. }
  308. // refactor-nice.pl: check this substitution
  309. // old: double Config::gD(const string & key) const
  310. double Config::gD(const std::string & key) const
  311. {
  312. return gD(key, "main");
  313. }
  314. // refactor-nice.pl: check this substitution
  315. // old: double Config::gD(const string & block, const string & key) const
  316. double Config::gD(const std::string & block, const std::string & key) const
  317. {
  318. double v = 0.0;
  319. if ( confD.find(block, key, v) ) {
  320. return v;
  321. } else {
  322. int vi;
  323. if ( confI.find(block, key, vi) ) {
  324. DEBUGPRINT("Config: Setting %s::%s should be double (please change in the config)\n", block.c_str(), key.c_str() );
  325. return (int)vi;
  326. }
  327. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  328. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  329. exit(-1);
  330. return -1.0; // never reached
  331. }
  332. }
  333. // refactor-nice.pl: check this substitution
  334. // old: double Config::gD(const string & block, const string & key, const double defv) const
  335. double Config::gD(const std::string & block, const std::string & key, const double defv) const
  336. {
  337. double v = 0.0;
  338. if ( confD.find(block, key, v) ) {
  339. return v;
  340. } else {
  341. int vi;
  342. if ( confI.find(block, key, vi) ) {
  343. DEBUGPRINT("Config: Setting %s::%s should be double (please change in the config)\n", block.c_str(), key.c_str() );
  344. return (int)vi;
  345. }
  346. #if defined DEBUGCONFIG
  347. DEBUGPRINT("Config: Setting %s::%s not found using default value %f\n", block.c_str(), key.c_str(), defv );
  348. #endif
  349. return defv;
  350. }
  351. }
  352. // refactor-nice.pl: check this substitution
  353. // old: int Config::gI(const string & key) const
  354. int Config::gI(const std::string & key) const
  355. {
  356. return gI(key, "main");
  357. }
  358. // refactor-nice.pl: check this substitution
  359. // old: int Config::gI(const string & block, const string & key) const
  360. int Config::gI(const std::string & block, const std::string & key) const
  361. {
  362. int v = 0;
  363. if ( confI.find(block, key, v) ) {
  364. return v;
  365. } else {
  366. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  367. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  368. exit(-1);
  369. return 1; // never reached
  370. }
  371. }
  372. // refactor-nice.pl: check this substitution
  373. // old: int Config::gI(const string & block, const string & key, int defv) const
  374. int Config::gI(const std::string & block, const std::string & key, int defv) const
  375. {
  376. int v = 0;
  377. if ( confI.find(block, key, v) ) {
  378. return v;
  379. } else {
  380. #if defined DEBUGCONFIG
  381. DEBUGPRINT("Config: Setting %s::%s not found using default value %d\n", block.c_str(), key.c_str(), defv );
  382. #endif
  383. return defv;
  384. }
  385. }
  386. // refactor-nice.pl: check this substitution
  387. // old: bool Config::gB(const string & key) const
  388. bool Config::gB(const std::string & key) const
  389. {
  390. return gB(key, "main");
  391. }
  392. // refactor-nice.pl: check this substitution
  393. // old: bool Config::gB(const string & block, const string & key) const
  394. bool Config::gB(const std::string & block, const std::string & key) const
  395. {
  396. bool v = true;
  397. if ( confB.find(block, key, v) ) {
  398. return v;
  399. } else {
  400. fprintf (stderr, "Config: setting %s::%s not found !\n", block.c_str(), key.c_str() );
  401. fprintf (stderr, "Config: %s\n", help(block, key).c_str() );
  402. exit(-1);
  403. return true; // never reached
  404. }
  405. }
  406. // refactor-nice.pl: check this substitution
  407. // old: bool Config::gB(const string & block, const string & key, bool defv) const
  408. bool Config::gB(const std::string & block, const std::string & key, bool defv) const
  409. {
  410. bool v = true;
  411. if ( confB.find(block, key, v) ) {
  412. return v;
  413. } else {
  414. #if defined DEBUGCONFIG
  415. DEBUGPRINT("Config: Setting %s::%s not found using default value %d\n", block.c_str(), key.c_str(), defv );
  416. #endif
  417. return defv;
  418. }
  419. }
  420. // refactor-nice.pl: check this substitution
  421. // old: void Config::getAllS ( const std::string & block, std::map< string, string > & list ) const
  422. void Config::getAllS ( const std::string & block, std::map< string, std::string > & list ) const
  423. {
  424. confS.getAll ( block, list );
  425. }
  426. void Config::getAllD ( const std::string & block, std::map< string, double > & list ) const
  427. {
  428. confD.getAll ( block, list );
  429. }
  430. void Config::getAllI ( const std::string & block, std::map< string, int > & list ) const
  431. {
  432. confI.getAll ( block, list );
  433. }
  434. void Config::getAllB ( const std::string & block, std::map< string, bool > & list ) const
  435. {
  436. confB.getAll ( block, list );
  437. }
  438. void Config::sD(const std::string & block, const std::string & key, const double defv)
  439. {
  440. confD.store ( block, key, defv );
  441. }
  442. void Config::sI(const std::string & block, const std::string & key, const int defv)
  443. {
  444. confI.store ( block, key, defv );
  445. }
  446. void Config::sB(const std::string & block, const std::string & key, const bool defv)
  447. {
  448. confB.store ( block, key, defv );
  449. }
  450. void Config::sS(const std::string & block, const std::string & key, const std::string & defv)
  451. {
  452. confS.store ( block, key, defv );
  453. }
  454. void Config::store (ostream & os, int format) const
  455. {
  456. if (!ioUntilEndOfFile)
  457. {
  458. //let's give us a hint that we do not have to restore until end of file but until "Config::store--done"
  459. os << "Config::store--PreciseTermination" << std::endl;
  460. }
  461. os << "# -----------------------------------------" << endl;
  462. os << "# | Config |" << endl;
  463. os << "# -----------------------------------------" << endl;
  464. os << endl << "# ------------- double values" << endl;
  465. confD.store( os, format );
  466. os << endl << "# ------------- boolean values" << endl;
  467. confB.store( os, format );
  468. os << endl << "# ------------- integer values" << endl;
  469. confI.store( os, format );
  470. os << endl << "# ------------- string values" << endl;
  471. confS.store( os, format );
  472. os << "# -----------------------------------------" << endl;
  473. if (!ioUntilEndOfFile)
  474. {
  475. os << "Config::store--done" << std::endl;
  476. }
  477. }
  478. void Config::getAllBlocks ( std::set< std::string > & list ) const
  479. {
  480. list.clear();
  481. confB.getAllBlocks ( list );
  482. confD.getAllBlocks ( list );
  483. confS.getAllBlocks ( list );
  484. confI.getAllBlocks ( list );
  485. }