cmCacheManager.cxx 23 KB

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