cmCacheManager.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCacheManager.h"
  14. #include "cmSystemTools.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "stdio.h"
  18. #include <cmsys/RegularExpression.hxx>
  19. #if defined(_WIN32) || defined(__CYGWIN__)
  20. # include <windows.h>
  21. #endif // _WIN32
  22. const char* cmCacheManagerTypes[] =
  23. { "BOOL",
  24. "PATH",
  25. "FILEPATH",
  26. "STRING",
  27. "INTERNAL",
  28. "STATIC",
  29. "UNINITIALIZED",
  30. 0
  31. };
  32. const char* cmCacheManager::TypeToString(cmCacheManager::CacheEntryType type)
  33. {
  34. if ( type > 6 || type < 0 )
  35. {
  36. return cmCacheManagerTypes[6];
  37. }
  38. return cmCacheManagerTypes[type];
  39. }
  40. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  41. {
  42. int i = 0;
  43. while(cmCacheManagerTypes[i])
  44. {
  45. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  46. {
  47. return static_cast<CacheEntryType>(i);
  48. }
  49. ++i;
  50. }
  51. return STRING;
  52. }
  53. bool cmCacheManager::LoadCache(cmMakefile* mf)
  54. {
  55. return this->LoadCache(mf->GetHomeOutputDirectory());
  56. }
  57. bool cmCacheManager::LoadCache(const char* path)
  58. {
  59. return this->LoadCache(path,true);
  60. }
  61. bool cmCacheManager::LoadCache(const char* path,
  62. bool internal)
  63. {
  64. std::set<std::string> emptySet;
  65. return this->LoadCache(path, internal, emptySet, emptySet);
  66. }
  67. bool cmCacheManager::ParseEntry(const char* entry,
  68. std::string& var,
  69. std::string& value)
  70. {
  71. // input line is: key:type=value
  72. cmsys::RegularExpression reg("^([^:]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  73. // input line is: "key":type=value
  74. cmsys::RegularExpression regQuoted("^\"([^\"]*)\"=(.*[^\t ]|[\t ]*)[\t ]*$");
  75. bool flag = false;
  76. if(regQuoted.find(entry))
  77. {
  78. var = regQuoted.match(1);
  79. value = regQuoted.match(2);
  80. flag = true;
  81. }
  82. else if (reg.find(entry))
  83. {
  84. var = reg.match(1);
  85. value = reg.match(2);
  86. flag = true;
  87. }
  88. // if value is enclosed in single quotes ('foo') then remove them
  89. // it is used to enclose trailing space or tab
  90. if (flag &&
  91. value.size() >= 2 &&
  92. value[0] == '\'' &&
  93. value[value.size() - 1] == '\'')
  94. {
  95. value = value.substr(1,
  96. value.size() - 2);
  97. }
  98. return flag;
  99. }
  100. bool cmCacheManager::ParseEntry(const char* entry,
  101. std::string& var,
  102. std::string& value,
  103. CacheEntryType& type)
  104. {
  105. // input line is: key:type=value
  106. cmsys::RegularExpression reg("^([^:]*):([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  107. // input line is: "key":type=value
  108. cmsys::RegularExpression regQuoted("^\"([^\"]*)\":([^=]*)=(.*[^\t ]|[\t ]*)[\t ]*$");
  109. bool flag = false;
  110. if(regQuoted.find(entry))
  111. {
  112. var = regQuoted.match(1);
  113. type = cmCacheManager::StringToType(regQuoted.match(2).c_str());
  114. value = regQuoted.match(3);
  115. flag = true;
  116. }
  117. else if (reg.find(entry))
  118. {
  119. var = reg.match(1);
  120. type = cmCacheManager::StringToType(reg.match(2).c_str());
  121. value = reg.match(3);
  122. flag = true;
  123. }
  124. // if value is enclosed in single quotes ('foo') then remove them
  125. // it is used to enclose trailing space or tab
  126. if (flag &&
  127. value.size() >= 2 &&
  128. value[0] == '\'' &&
  129. value[value.size() - 1] == '\'')
  130. {
  131. value = value.substr(1,
  132. value.size() - 2);
  133. }
  134. return flag;
  135. }
  136. bool cmCacheManager::LoadCache(const char* path,
  137. bool internal,
  138. std::set<std::string>& excludes,
  139. std::set<std::string>& includes)
  140. {
  141. std::string cacheFile = path;
  142. cacheFile += "/CMakeCache.txt";
  143. // clear the old cache, if we are reading in internal values
  144. if ( internal )
  145. {
  146. m_Cache.clear();
  147. }
  148. std::ifstream fin(cacheFile.c_str());
  149. if(!fin)
  150. {
  151. return false;
  152. }
  153. const char *realbuffer;
  154. std::string buffer;
  155. std::string entryKey;
  156. while(fin)
  157. {
  158. // Format is key:type=value
  159. CacheEntry e;
  160. cmSystemTools::GetLineFromStream(fin, buffer);
  161. realbuffer = buffer.c_str();
  162. while(*realbuffer != '0' &&
  163. (*realbuffer == ' ' ||
  164. *realbuffer == '\t' ||
  165. *realbuffer == '\n'))
  166. {
  167. realbuffer++;
  168. }
  169. // skip blank lines and comment lines
  170. if(realbuffer[0] == '#' || realbuffer[0] == 0)
  171. {
  172. continue;
  173. }
  174. while(realbuffer[0] == '/' && realbuffer[1] == '/')
  175. {
  176. e.m_Properties["HELPSTRING"] += &realbuffer[2];
  177. cmSystemTools::GetLineFromStream(fin, buffer);
  178. realbuffer = buffer.c_str();
  179. if(!fin)
  180. {
  181. continue;
  182. }
  183. }
  184. if(cmCacheManager::ParseEntry(realbuffer, entryKey, e.m_Value, e.m_Type))
  185. {
  186. if ( excludes.find(entryKey) == excludes.end() )
  187. {
  188. // Load internal values if internal is set.
  189. // If the entry is not internal to the cache being loaded
  190. // or if it is in the list of internal entries to be
  191. // imported, load it.
  192. if ( internal || (e.m_Type != INTERNAL) ||
  193. (includes.find(entryKey) != includes.end()) )
  194. {
  195. // If we are loading the cache from another project,
  196. // make all loaded entries internal so that it is
  197. // not visible in the gui
  198. if (!internal)
  199. {
  200. e.m_Type = INTERNAL;
  201. e.m_Properties["HELPSTRING"] = "DO NOT EDIT, ";
  202. e.m_Properties["HELPSTRING"] += entryKey;
  203. e.m_Properties["HELPSTRING"] += " loaded from external file. "
  204. "To change this value edit this file: ";
  205. e.m_Properties["HELPSTRING"] += path;
  206. e.m_Properties["HELPSTRING"] += "/CMakeCache.txt" ;
  207. }
  208. if ( e.m_Type == cmCacheManager::INTERNAL &&
  209. (entryKey.size() > strlen("-ADVANCED")) &&
  210. strcmp(entryKey.c_str() + (entryKey.size() - strlen("-ADVANCED")),
  211. "-ADVANCED") == 0 )
  212. {
  213. std::string value = e.m_Value;
  214. std::string akey = entryKey.substr(0, (entryKey.size() - strlen("-ADVANCED")));
  215. cmCacheManager::CacheIterator it = this->GetCacheIterator(akey.c_str());
  216. if ( it.IsAtEnd() )
  217. {
  218. e.m_Type = cmCacheManager::UNINITIALIZED;
  219. m_Cache[akey] = e;
  220. }
  221. if (!it.Find(akey.c_str()))
  222. {
  223. cmSystemTools::Error("Internal CMake error when reading cache");
  224. }
  225. it.SetProperty("ADVANCED", value.c_str());
  226. }
  227. else
  228. {
  229. e.m_Initialized = true;
  230. m_Cache[entryKey] = e;
  231. }
  232. }
  233. }
  234. }
  235. else
  236. {
  237. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str(),
  238. ". Offending entry: ", realbuffer);
  239. }
  240. }
  241. // if CMAKE version not found in the list file
  242. // add them as version 0.0
  243. if(!this->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  244. {
  245. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
  246. "Minor version of cmake used to create the "
  247. "current loaded cache", cmCacheManager::INTERNAL);
  248. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
  249. "Major version of cmake used to create the "
  250. "current loaded cache", cmCacheManager::INTERNAL);
  251. }
  252. // check to make sure the cache directory has not
  253. // been moved
  254. if ( internal && this->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  255. {
  256. std::string currentcwd = path;
  257. std::string oldcwd = this->GetCacheValue("CMAKE_CACHEFILE_DIR");
  258. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  259. currentcwd += "/CMakeCache.txt";
  260. oldcwd += "/CMakeCache.txt";
  261. if(!cmSystemTools::SameFile(oldcwd.c_str(), currentcwd.c_str()))
  262. {
  263. std::string message =
  264. std::string("The current CMakeCache.txt directory ") +
  265. currentcwd + std::string(" is different than the directory ") +
  266. std::string(this->GetCacheValue("CMAKE_CACHEFILE_DIR")) +
  267. std::string(" where CMackeCache.txt was created. This may result "
  268. "in binaries being created in the wrong place. If you "
  269. "are not sure, reedit the CMakeCache.txt");
  270. cmSystemTools::Error(message.c_str());
  271. }
  272. }
  273. return true;
  274. }
  275. bool cmCacheManager::SaveCache(cmMakefile* mf)
  276. {
  277. return this->SaveCache(mf->GetHomeOutputDirectory());
  278. }
  279. bool cmCacheManager::SaveCache(const char* path)
  280. {
  281. std::string cacheFile = path;
  282. cacheFile += "/CMakeCache.txt";
  283. std::string tempFile = cacheFile;
  284. tempFile += ".tmp";
  285. std::ofstream fout(tempFile.c_str());
  286. if(!fout)
  287. {
  288. cmSystemTools::Error("Unable to open cache file for save. ",
  289. cacheFile.c_str());
  290. return false;
  291. }
  292. // before writting the cache, update the version numbers
  293. // to the
  294. char temp[1024];
  295. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  296. this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", temp,
  297. "Minor version of cmake used to create the "
  298. "current loaded cache", cmCacheManager::INTERNAL);
  299. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  300. this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", temp,
  301. "Major version of cmake used to create the "
  302. "current loaded cache", cmCacheManager::INTERNAL);
  303. this->AddCacheEntry("CMAKE_CACHE_RELEASE_VERSION", cmMakefile::GetReleaseVersion(),
  304. "Major version of cmake used to create the "
  305. "current loaded cache", cmCacheManager::INTERNAL);
  306. // Let us store the current working directory so that if somebody
  307. // Copies it, he will not be surprised
  308. std::string currentcwd = path;
  309. if ( currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' &&
  310. currentcwd[1] == ':' )
  311. {
  312. currentcwd[0] = currentcwd[0] - 'A' + 'a';
  313. }
  314. cmSystemTools::ConvertToUnixSlashes(currentcwd);
  315. this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd.c_str(),
  316. "This is the directory where this CMakeCahe.txt"
  317. " was created", cmCacheManager::INTERNAL);
  318. fout << "# This is the CMakeCache file.\n"
  319. << "# For build in directory: " << currentcwd << "\n"
  320. << "# You can edit this file to change values found and used by cmake.\n"
  321. << "# If you do not want to change any of the values, simply exit the editor.\n"
  322. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  323. << "# The syntax for the file is as follows:\n"
  324. << "# KEY:TYPE=VALUE\n"
  325. << "# KEY is the name of a variable in the cache.\n"
  326. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  327. << "# VALUE is the current value for the KEY.\n\n";
  328. fout << "########################\n";
  329. fout << "# EXTERNAL cache entries\n";
  330. fout << "########################\n";
  331. fout << "\n";
  332. for( std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  333. i != m_Cache.end(); ++i)
  334. {
  335. const CacheEntry& ce = (*i).second;
  336. CacheEntryType t = ce.m_Type;
  337. if(t == cmCacheManager::UNINITIALIZED || !ce.m_Initialized)
  338. {
  339. /*
  340. // This should be added in, but is not for now.
  341. cmSystemTools::Error("Cache entry \"", (*i).first.c_str(),
  342. "\" is uninitialized");
  343. */
  344. }
  345. else if(t != INTERNAL)
  346. {
  347. // Format is key:type=value
  348. std::map<cmStdString,cmStdString>::const_iterator it =
  349. ce.m_Properties.find("HELPSTRING");
  350. if ( it == ce.m_Properties.end() )
  351. {
  352. cmCacheManager::OutputHelpString(fout, "Missing description");
  353. }
  354. else
  355. {
  356. cmCacheManager::OutputHelpString(fout, it->second);
  357. }
  358. std::string key;
  359. // support : in key name by double quoting
  360. if((*i).first.find(':') != std::string::npos ||
  361. (*i).first.find("//") == 0)
  362. {
  363. key = "\"";
  364. key += i->first;
  365. key += "\"";
  366. }
  367. else
  368. {
  369. key = i->first;
  370. }
  371. fout << key.c_str() << ":"
  372. << cmCacheManagerTypes[t] << "=";
  373. // if value has trailing space or tab, enclose it in single quotes
  374. if (ce.m_Value.size() &&
  375. (ce.m_Value[ce.m_Value.size() - 1] == ' ' ||
  376. ce.m_Value[ce.m_Value.size() - 1] == '\t'))
  377. {
  378. fout << '\'' << ce.m_Value << '\'';
  379. }
  380. else
  381. {
  382. fout << ce.m_Value;
  383. }
  384. fout << "\n\n";
  385. }
  386. }
  387. fout << "\n";
  388. fout << "########################\n";
  389. fout << "# INTERNAL cache entries\n";
  390. fout << "########################\n";
  391. fout << "\n";
  392. for( cmCacheManager::CacheIterator i = this->NewIterator();
  393. !i.IsAtEnd(); i.Next())
  394. {
  395. if ( !i.Initialized() )
  396. {
  397. continue;
  398. }
  399. CacheEntryType t = i.GetType();
  400. bool advanced = i.PropertyExists("ADVANCED");
  401. if ( advanced )
  402. {
  403. // Format is key:type=value
  404. std::string key;
  405. std::string rkey = i.GetName();
  406. std::string helpstring;
  407. // If this is advanced variable, we have to do some magic for
  408. // backward compatibility
  409. helpstring = "Advanced flag for variable: ";
  410. helpstring += i.GetName();
  411. rkey += "-ADVANCED";
  412. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  413. // support : in key name by double quoting
  414. if(rkey.find(':') != std::string::npos ||
  415. rkey.find("//") == 0)
  416. {
  417. key = "\"";
  418. key += rkey;
  419. key += "\"";
  420. }
  421. else
  422. {
  423. key = rkey;
  424. }
  425. fout << key.c_str() << ":INTERNAL="
  426. << (i.GetPropertyAsBool("ADVANCED") ? "1" : "0") << "\n";
  427. }
  428. if(t == cmCacheManager::INTERNAL)
  429. {
  430. // Format is key:type=value
  431. std::string key;
  432. std::string rkey = i.GetName();
  433. std::string helpstring;
  434. const char* hs = i.GetProperty("HELPSTRING");
  435. if ( hs )
  436. {
  437. helpstring = i.GetProperty("HELPSTRING");
  438. }
  439. else
  440. {
  441. helpstring = "";
  442. }
  443. cmCacheManager::OutputHelpString(fout, helpstring.c_str());
  444. // support : in key name by double quoting
  445. if(rkey.find(':') != std::string::npos ||
  446. rkey.find("//") == 0)
  447. {
  448. key = "\"";
  449. key += rkey;
  450. key += "\"";
  451. }
  452. else
  453. {
  454. key = rkey;
  455. }
  456. fout << key.c_str() << ":"
  457. << cmCacheManagerTypes[t] << "=";
  458. // if value has trailing space or tab, enclose it in single quotes
  459. std::string value = i.GetValue();
  460. if (value.size() &&
  461. (value[value.size() - 1] == ' ' ||
  462. value[value.size() - 1] == '\t'))
  463. {
  464. fout << '\'' << value << '\'';
  465. }
  466. else
  467. {
  468. fout << value;
  469. }
  470. fout << "\n";
  471. }
  472. }
  473. fout << "\n";
  474. fout.close();
  475. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  476. cacheFile.c_str());
  477. cmSystemTools::RemoveFile(tempFile.c_str());
  478. std::string checkCacheFile = path;
  479. checkCacheFile += "/cmake.check_cache";
  480. std::ofstream checkCache(checkCacheFile.c_str());
  481. if(!checkCache)
  482. {
  483. cmSystemTools::Error("Unable to open check cache file for write. ",
  484. checkCacheFile.c_str());
  485. return false;
  486. }
  487. checkCache << "# This file is generated by cmake for dependency checking of the CMakeCache.txt file\n";
  488. return true;
  489. }
  490. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  491. const std::string& helpString)
  492. {
  493. std::string::size_type end = helpString.size();
  494. if(end == 0)
  495. {
  496. return;
  497. }
  498. std::string oneLine;
  499. std::string::size_type pos = 0;
  500. std::string::size_type nextBreak = 60;
  501. bool done = false;
  502. while(!done)
  503. {
  504. if(nextBreak >= end)
  505. {
  506. nextBreak = end;
  507. done = true;
  508. }
  509. else
  510. {
  511. while(nextBreak < end && helpString[nextBreak] != ' ')
  512. {
  513. nextBreak++;
  514. }
  515. }
  516. oneLine = helpString.substr(pos, nextBreak - pos);
  517. fout << "//" << oneLine.c_str() << "\n";
  518. pos = nextBreak;
  519. nextBreak += 60;
  520. }
  521. }
  522. void cmCacheManager::RemoveCacheEntry(const char* key)
  523. {
  524. CacheEntryMap::iterator i = m_Cache.find(key);
  525. if(i != m_Cache.end())
  526. {
  527. m_Cache.erase(i);
  528. }
  529. else
  530. {
  531. std::cerr << "Failed to remove entry:" << key << std::endl;
  532. }
  533. }
  534. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  535. {
  536. CacheEntryMap::iterator i = m_Cache.find(key);
  537. if(i != m_Cache.end())
  538. {
  539. return &i->second;
  540. }
  541. return 0;
  542. }
  543. cmCacheManager::CacheIterator cmCacheManager::GetCacheIterator(const char *key)
  544. {
  545. return CacheIterator(*this, key);
  546. }
  547. const char* cmCacheManager::GetCacheValue(const char* key) const
  548. {
  549. CacheEntryMap::const_iterator i = m_Cache.find(key);
  550. if(i != m_Cache.end() && i->second.m_Type != cmCacheManager::UNINITIALIZED &&
  551. i->second.m_Initialized)
  552. {
  553. return i->second.m_Value.c_str();
  554. }
  555. return 0;
  556. }
  557. void cmCacheManager::PrintCache(std::ostream& out) const
  558. {
  559. out << "=================================================" << std::endl;
  560. out << "CMakeCache Contents:" << std::endl;
  561. for(std::map<cmStdString, CacheEntry>::const_iterator i = m_Cache.begin();
  562. i != m_Cache.end(); ++i)
  563. {
  564. if((*i).second.m_Type != INTERNAL)
  565. {
  566. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  567. }
  568. }
  569. out << "\n\n";
  570. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  571. out << "=================================================" << std::endl;
  572. }
  573. void cmCacheManager::AddCacheEntry(const char* key,
  574. const char* value,
  575. const char* helpString,
  576. CacheEntryType type)
  577. {
  578. CacheEntry& e = m_Cache[key];
  579. if ( value )
  580. {
  581. e.m_Value = value;
  582. e.m_Initialized = true;
  583. }
  584. else
  585. {
  586. e.m_Value = "";
  587. }
  588. e.m_Type = type;
  589. // make sure we only use unix style paths
  590. if(type == FILEPATH || type == PATH)
  591. {
  592. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  593. }
  594. if ( helpString )
  595. {
  596. e.m_Properties["HELPSTRING"] = helpString;
  597. }
  598. else
  599. {
  600. e.m_Properties["HELPSTRING"] = "(This variable does not exists and should not be used)";
  601. }
  602. m_Cache[key] = e;
  603. }
  604. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  605. const char* helpString)
  606. {
  607. if(v)
  608. {
  609. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  610. }
  611. else
  612. {
  613. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  614. }
  615. }
  616. bool cmCacheManager::CacheIterator::IsAtEnd() const
  617. {
  618. return m_Position == m_Container.m_Cache.end();
  619. }
  620. void cmCacheManager::CacheIterator::Begin()
  621. {
  622. m_Position = m_Container.m_Cache.begin();
  623. }
  624. bool cmCacheManager::CacheIterator::Find(const char* key)
  625. {
  626. m_Position = m_Container.m_Cache.find(key);
  627. return !this->IsAtEnd();
  628. }
  629. void cmCacheManager::CacheIterator::Next()
  630. {
  631. if (!this->IsAtEnd())
  632. {
  633. ++m_Position;
  634. }
  635. }
  636. void cmCacheManager::CacheIterator::SetValue(const char* value)
  637. {
  638. if (this->IsAtEnd())
  639. {
  640. return;
  641. }
  642. CacheEntry* entry = &this->GetEntry();
  643. if ( value )
  644. {
  645. entry->m_Value = value;
  646. entry->m_Initialized = true;
  647. }
  648. else
  649. {
  650. entry->m_Value = "";
  651. }
  652. }
  653. const char* cmCacheManager::CacheIterator::GetProperty(const char* property) const
  654. {
  655. // make sure it is not at the end
  656. if (this->IsAtEnd())
  657. {
  658. return 0;
  659. }
  660. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  661. {
  662. cmSystemTools::Error("Property \"", property,
  663. "\" cannot be accessed through the GetProperty()");
  664. return 0;
  665. }
  666. const CacheEntry* ent = &this->GetEntry();
  667. std::map<cmStdString,cmStdString>::const_iterator it =
  668. ent->m_Properties.find(property);
  669. if ( it == ent->m_Properties.end() )
  670. {
  671. return 0;
  672. }
  673. return it->second.c_str();
  674. }
  675. void cmCacheManager::CacheIterator::SetProperty(const char* p, const char* v)
  676. {
  677. // make sure it is not at the end
  678. if (this->IsAtEnd())
  679. {
  680. return;
  681. }
  682. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  683. {
  684. cmSystemTools::Error("Property \"", p,
  685. "\" cannot be accessed through the SetProperty()");
  686. return;
  687. }
  688. CacheEntry* ent = &this->GetEntry();
  689. ent->m_Properties[p] = v;
  690. }
  691. bool cmCacheManager::CacheIterator::GetValueAsBool() const
  692. {
  693. return cmSystemTools::IsOn(this->GetEntry().m_Value.c_str());
  694. }
  695. bool cmCacheManager::CacheIterator::GetPropertyAsBool(const char* property) const
  696. {
  697. // make sure it is not at the end
  698. if (this->IsAtEnd())
  699. {
  700. return false;
  701. }
  702. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  703. {
  704. cmSystemTools::Error("Property \"", property,
  705. "\" cannot be accessed through the GetPropertyAsBool()");
  706. return false;
  707. }
  708. const CacheEntry* ent = &this->GetEntry();
  709. std::map<cmStdString,cmStdString>::const_iterator it =
  710. ent->m_Properties.find(property);
  711. if ( it == ent->m_Properties.end() )
  712. {
  713. return false;
  714. }
  715. return cmSystemTools::IsOn(it->second.c_str());
  716. }
  717. void cmCacheManager::CacheIterator::SetProperty(const char* p, bool v)
  718. {
  719. // make sure it is not at the end
  720. if (this->IsAtEnd())
  721. {
  722. return;
  723. }
  724. if ( !strcmp(p, "TYPE") || !strcmp(p, "VALUE") )
  725. {
  726. cmSystemTools::Error("Property \"", p,
  727. "\" cannot be accessed through the SetProperty()");
  728. return;
  729. }
  730. CacheEntry* ent = &this->GetEntry();
  731. ent->m_Properties[p] = v ? "ON" : "OFF";
  732. }
  733. bool cmCacheManager::CacheIterator::PropertyExists(const char* property) const
  734. {
  735. // make sure it is not at the end
  736. if (this->IsAtEnd())
  737. {
  738. return false;
  739. }
  740. if ( !strcmp(property, "TYPE") || !strcmp(property, "VALUE") )
  741. {
  742. cmSystemTools::Error("Property \"", property,
  743. "\" cannot be accessed through the PropertyExists()");
  744. return false;
  745. }
  746. const CacheEntry* ent = &this->GetEntry();
  747. std::map<cmStdString,cmStdString>::const_iterator it =
  748. ent->m_Properties.find(property);
  749. if ( it == ent->m_Properties.end() )
  750. {
  751. return false;
  752. }
  753. return true;
  754. }