cmCacheManager.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. return this->LoadCache(mf->GetHomeOutputDirectory());
  49. }
  50. bool cmCacheManager::LoadCache(const char* path)
  51. {
  52. std::string cacheFile = path;
  53. cacheFile += "/CMakeCache.txt";
  54. // clear the old cache
  55. m_Cache.clear();
  56. std::ifstream fin(cacheFile.c_str());
  57. if(!fin)
  58. {
  59. return false;
  60. }
  61. const int bsize = 4096;
  62. char buffer[bsize];
  63. // input line is: key:type=value
  64. cmRegularExpression reg("(.*):(.*)=(.*)");
  65. while(fin)
  66. {
  67. // Format is key:type=value
  68. CacheEntry e;
  69. fin.getline(buffer, bsize);
  70. // skip blank lines and comment lines
  71. if(buffer[0] == '#' || buffer[0] == 0)
  72. {
  73. continue;
  74. }
  75. if(reg.find(buffer))
  76. {
  77. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  78. e.m_Value = reg.match(3);
  79. m_Cache[reg.match(1)] = e;
  80. }
  81. else
  82. {
  83. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  84. }
  85. }
  86. return true;
  87. }
  88. bool cmCacheManager::SaveCache(cmMakefile* mf) const
  89. {
  90. return this->SaveCache(mf->GetHomeOutputDirectory());
  91. }
  92. bool cmCacheManager::SaveCache(const char* path) const
  93. {
  94. std::string cacheFile = path;
  95. cacheFile += "/CMakeCache.txt";
  96. std::string tempFile = cacheFile;
  97. tempFile += ".tmp";
  98. std::ofstream fout(tempFile.c_str());
  99. if(!fout)
  100. {
  101. cmSystemTools::Error("Unable to open cache file for save. ",
  102. cacheFile.c_str());
  103. return false;
  104. }
  105. fout << "# This is the CMakeCache file.\n"
  106. << "# For build in directory: " << path << "\n"
  107. << "# You can edit this file to change values found and used by cmake.\n"
  108. << "# If you do not want to change any of the values, simply exit the editor.\n"
  109. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  110. << "# The syntax for the file is as follows:\n"
  111. << "# KEY:TYPE=VALUE\n"
  112. << "# KEY is the name of a varible in the cache.\n"
  113. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  114. << "# VALUE is the current value for the KEY.\n\n";
  115. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  116. i != m_Cache.end(); ++i)
  117. {
  118. CacheEntryType t = (*i).second.m_Type;
  119. // Format is key:type=value
  120. fout << (*i).first.c_str() << ":"
  121. << cmCacheManagerTypes[t] << "="
  122. << (*i).second.m_Value << "\n";
  123. }
  124. fout << "\n";
  125. fout.close();
  126. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  127. cacheFile.c_str());
  128. cmSystemTools::RemoveFile(tempFile.c_str());
  129. return true;
  130. }
  131. void cmCacheManager::RemoveCacheEntry(const char* key)
  132. {
  133. m_Cache.erase(key);
  134. }
  135. void cmCacheManager::AddCacheEntry(const char* key,
  136. const char* value,
  137. CacheEntryType type)
  138. {
  139. CacheEntry e;
  140. e.m_Value = value;
  141. e.m_Type = type;
  142. m_Cache[key] = e;
  143. }
  144. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  145. {
  146. if(m_Cache.count(key))
  147. {
  148. return &(m_Cache.find(key)->second);
  149. }
  150. return 0;
  151. }
  152. const char* cmCacheManager::GetCacheValue(const char* key) const
  153. {
  154. if(m_Cache.count(key))
  155. {
  156. return m_Cache.find(key)->second.m_Value.c_str();
  157. }
  158. return 0;
  159. }
  160. bool cmCacheManager::IsOn(const char* key) const
  161. {
  162. if(!m_Cache.count(key))
  163. {
  164. return false;
  165. }
  166. const std::string &v = m_Cache.find(key)->second.m_Value;
  167. return cmSystemTools::IsOn(v.c_str());
  168. }
  169. void cmCacheManager::PrintCache(std::ostream& out) const
  170. {
  171. out << "=================================================" << std::endl;
  172. out << "CMakeCache Contents:" << std::endl;
  173. for(std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  174. i != m_Cache.end(); ++i)
  175. {
  176. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  177. }
  178. out << "\n\n";
  179. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  180. out << "=================================================" << std::endl;
  181. }
  182. void cmCacheManager::AddCacheEntry(const char* key, bool v)
  183. {
  184. if(v)
  185. {
  186. this->AddCacheEntry(key, "ON", cmCacheManager::BOOL);
  187. }
  188. else
  189. {
  190. this->AddCacheEntry(key, "OFF", cmCacheManager::BOOL);
  191. }
  192. }