cmCacheManager.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. while(buffer[0] == '/')
  76. {
  77. e.m_HelpString += &buffer[2];
  78. fin.getline(buffer, bsize);
  79. if(!fin)
  80. {
  81. continue;
  82. }
  83. }
  84. if(reg.find(buffer))
  85. {
  86. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  87. e.m_Value = reg.match(3);
  88. m_Cache[reg.match(1)] = e;
  89. }
  90. else
  91. {
  92. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  93. }
  94. }
  95. return true;
  96. }
  97. bool cmCacheManager::SaveCache(cmMakefile* mf) const
  98. {
  99. return this->SaveCache(mf->GetHomeOutputDirectory());
  100. }
  101. bool cmCacheManager::SaveCache(const char* path) const
  102. {
  103. std::string cacheFile = path;
  104. cacheFile += "/CMakeCache.txt";
  105. std::string tempFile = cacheFile;
  106. tempFile += ".tmp";
  107. std::ofstream fout(tempFile.c_str());
  108. if(!fout)
  109. {
  110. cmSystemTools::Error("Unable to open cache file for save. ",
  111. cacheFile.c_str());
  112. return false;
  113. }
  114. fout << "# This is the CMakeCache file.\n"
  115. << "# For build in directory: " << path << "\n"
  116. << "# You can edit this file to change values found and used by cmake.\n"
  117. << "# If you do not want to change any of the values, simply exit the editor.\n"
  118. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  119. << "# The syntax for the file is as follows:\n"
  120. << "# KEY:TYPE=VALUE\n"
  121. << "# KEY is the name of a varible in the cache.\n"
  122. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  123. << "# VALUE is the current value for the KEY.\n\n";
  124. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  125. i != m_Cache.end(); ++i)
  126. {
  127. const CacheEntry& ce = (*i).second;
  128. CacheEntryType t = ce.m_Type;
  129. // Format is key:type=value
  130. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  131. fout << (*i).first.c_str() << ":"
  132. << cmCacheManagerTypes[t] << "="
  133. << ce.m_Value << "\n";
  134. }
  135. fout << "\n";
  136. fout.close();
  137. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  138. cacheFile.c_str());
  139. cmSystemTools::RemoveFile(tempFile.c_str());
  140. return true;
  141. }
  142. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  143. const std::string& helpString)
  144. {
  145. std::string::size_type end = helpString.size();
  146. if(end == 0)
  147. {
  148. return;
  149. }
  150. std::string oneLine;
  151. std::string::size_type pos = 0;
  152. std::string::size_type nextBreak = 60;
  153. bool done = false;
  154. while(!done)
  155. {
  156. if(nextBreak >= end)
  157. {
  158. nextBreak = end;
  159. done = true;
  160. }
  161. else
  162. {
  163. while(nextBreak < end && helpString[nextBreak] != ' ')
  164. {
  165. nextBreak++;
  166. }
  167. }
  168. oneLine = helpString.substr(pos, nextBreak - pos);
  169. fout << "//" << oneLine.c_str() << "\n";
  170. pos = nextBreak;
  171. nextBreak += 60;
  172. }
  173. }
  174. void cmCacheManager::RemoveCacheEntry(const char* key)
  175. {
  176. m_Cache.erase(key);
  177. }
  178. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  179. {
  180. if(m_Cache.count(key))
  181. {
  182. return &(m_Cache.find(key)->second);
  183. }
  184. return 0;
  185. }
  186. const char* cmCacheManager::GetCacheValue(const char* key) const
  187. {
  188. if(m_Cache.count(key))
  189. {
  190. return m_Cache.find(key)->second.m_Value.c_str();
  191. }
  192. return 0;
  193. }
  194. bool cmCacheManager::IsOn(const char* key) const
  195. {
  196. if(!m_Cache.count(key))
  197. {
  198. return false;
  199. }
  200. const std::string &v = m_Cache.find(key)->second.m_Value;
  201. return cmSystemTools::IsOn(v.c_str());
  202. }
  203. void cmCacheManager::PrintCache(std::ostream& out) const
  204. {
  205. out << "=================================================" << std::endl;
  206. out << "CMakeCache Contents:" << std::endl;
  207. for(std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  208. i != m_Cache.end(); ++i)
  209. {
  210. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  211. }
  212. out << "\n\n";
  213. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  214. out << "=================================================" << std::endl;
  215. }
  216. void cmCacheManager::AddCacheEntry(const char* key,
  217. const char* value,
  218. const char* helpString,
  219. CacheEntryType type)
  220. {
  221. CacheEntry e;
  222. e.m_Value = value;
  223. e.m_Type = type;
  224. e.m_HelpString = helpString;
  225. m_Cache[key] = e;
  226. }
  227. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  228. const char* helpString)
  229. {
  230. if(v)
  231. {
  232. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  233. }
  234. else
  235. {
  236. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  237. }
  238. }