cmCacheManager.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmCacheManager.h"
  12. #include "cmSystemTools.h"
  13. #include "cmCacheManager.h"
  14. #include "cmMakefile.h"
  15. #include "cmRegularExpression.h"
  16. const char* cmCacheManagerTypes[] =
  17. { "BOOL",
  18. "PATH",
  19. "FILEPATH",
  20. "STRING",
  21. "INTERNAL",
  22. 0
  23. };
  24. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  25. {
  26. int i = 0;
  27. while(cmCacheManagerTypes[i])
  28. {
  29. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  30. {
  31. return static_cast<CacheEntryType>(i);
  32. }
  33. ++i;
  34. }
  35. return STRING;
  36. }
  37. cmCacheManager* cmCacheManager::s_Instance = 0;
  38. cmCacheManager* cmCacheManager::GetInstance()
  39. {
  40. if(!cmCacheManager::s_Instance)
  41. {
  42. cmCacheManager::s_Instance = new cmCacheManager;
  43. }
  44. return cmCacheManager::s_Instance;
  45. }
  46. bool cmCacheManager::LoadCache(cmMakefile* mf)
  47. {
  48. std::string cacheFile = mf->GetHomeOutputDirectory();
  49. cacheFile += "/CMakeCache.txt";
  50. // clear the old cache
  51. m_Cache.clear();
  52. std::ifstream fin(cacheFile.c_str());
  53. if(!fin)
  54. {
  55. return false;
  56. }
  57. const int bsize = 4096;
  58. char buffer[bsize];
  59. // input line is: key:type=value
  60. cmRegularExpression reg("(.*):(.*)=(.*)");
  61. while(fin)
  62. {
  63. // Format is key:type=value
  64. CacheEntry e;
  65. fin.getline(buffer, bsize);
  66. // skip blank lines and comment lines
  67. if(buffer[0] == '#' || buffer[0] == 0)
  68. {
  69. continue;
  70. }
  71. if(reg.find(buffer))
  72. {
  73. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  74. e.m_Value = reg.match(3);
  75. m_Cache[reg.match(1)] = e;
  76. }
  77. else
  78. {
  79. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  80. }
  81. }
  82. return true;
  83. }
  84. bool cmCacheManager::SaveCache(cmMakefile* mf) const
  85. {
  86. std::string cacheFile = mf->GetHomeOutputDirectory();
  87. cacheFile += "/CMakeCache.txt";
  88. std::string tempFile = cacheFile;
  89. tempFile += ".tmp";
  90. std::ofstream fout(tempFile.c_str());
  91. if(!fout)
  92. {
  93. cmSystemTools::Error("Unable to open cache file for save. ",
  94. cacheFile.c_str());
  95. return false;
  96. }
  97. fout << "# This is the CMakeCache file.\n"
  98. << "# You can edit this file to change values found and used by cmake.\n"
  99. << "# If you do not want to change any of the values, simply exit the editor.\n"
  100. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  101. << "# The syntax for the file is as follows:\n"
  102. << "# KEY:TYPE=VALUE\n"
  103. << "# KEY is the name of a varible in the cache.\n"
  104. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  105. << "# VALUE is the current value for the KEY.\n\n";
  106. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  107. i != m_Cache.end(); ++i)
  108. {
  109. CacheEntryType t = (*i).second.m_Type;
  110. // Format is key:type=value
  111. fout << (*i).first.c_str() << ":"
  112. << cmCacheManagerTypes[t] << "="
  113. << (*i).second.m_Value << "\n";
  114. }
  115. fout << "\n";
  116. fout.close();
  117. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  118. cacheFile.c_str());
  119. cmSystemTools::RemoveFile(tempFile.c_str());
  120. return true;
  121. }
  122. void cmCacheManager::RemoveCacheEntry(const char* key)
  123. {
  124. m_Cache.erase(key);
  125. }
  126. void cmCacheManager::AddCacheEntry(const char* key,
  127. const char* value,
  128. CacheEntryType type)
  129. {
  130. CacheEntry e;
  131. e.m_Value = value;
  132. e.m_Type = type;
  133. m_Cache[key] = e;
  134. }
  135. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  136. {
  137. if(m_Cache.count(key))
  138. {
  139. return &(m_Cache.find(key)->second);
  140. }
  141. return 0;
  142. }
  143. const char* cmCacheManager::GetCacheValue(const char* key) const
  144. {
  145. if(m_Cache.count(key))
  146. {
  147. return m_Cache.find(key)->second.m_Value.c_str();
  148. }
  149. return 0;
  150. }
  151. bool cmCacheManager::IsOn(const char* key) const
  152. {
  153. if(!m_Cache.count(key))
  154. {
  155. return false;
  156. }
  157. const std::string &v = m_Cache.find(key)->second.m_Value;
  158. return (v == "ON" || v == "on" || v == "1" || v == "true" || v == "yev"
  159. || v == "TRUE" || v == "True" || v == "y" || v == "Y");
  160. }
  161. void cmCacheManager::PrintCache(std::ostream& out) const
  162. {
  163. out << "=================================================" << std::endl;
  164. out << "CMakeCache Contents:" << std::endl;
  165. for(std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  166. i != m_Cache.end(); ++i)
  167. {
  168. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  169. }
  170. out << "\n\n";
  171. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  172. out << "=================================================" << std::endl;
  173. }
  174. void cmCacheManager::AddCacheEntry(const char* key, bool v)
  175. {
  176. if(v)
  177. {
  178. this->AddCacheEntry(key, "ON", cmCacheManager::BOOL);
  179. }
  180. else
  181. {
  182. this->AddCacheEntry(key, "OFF", cmCacheManager::BOOL);
  183. }
  184. }