1
0

cmCacheManager.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmCacheManager.h"
  33. #include "cmSystemTools.h"
  34. #include "cmCacheManager.h"
  35. #include "cmMakefile.h"
  36. #include "cmRegularExpression.h"
  37. const char* cmCacheManagerTypes[] =
  38. { "BOOL",
  39. "PATH",
  40. "FILEPATH",
  41. "STRING",
  42. "INTERNAL",
  43. 0
  44. };
  45. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  46. {
  47. int i = 0;
  48. while(cmCacheManagerTypes[i])
  49. {
  50. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  51. {
  52. return static_cast<CacheEntryType>(i);
  53. }
  54. ++i;
  55. }
  56. return STRING;
  57. }
  58. cmCacheManager* cmCacheManager::s_Instance = 0;
  59. cmCacheManager* cmCacheManager::GetInstance()
  60. {
  61. if(!cmCacheManager::s_Instance)
  62. {
  63. cmCacheManager::s_Instance = new cmCacheManager;
  64. }
  65. return cmCacheManager::s_Instance;
  66. }
  67. bool cmCacheManager::LoadCache(cmMakefile* mf)
  68. {
  69. return this->LoadCache(mf->GetHomeOutputDirectory());
  70. }
  71. bool cmCacheManager::LoadCache(const char* path)
  72. {
  73. std::string cacheFile = path;
  74. cacheFile += "/CMakeCache.txt";
  75. // clear the old cache
  76. m_Cache.clear();
  77. std::ifstream fin(cacheFile.c_str());
  78. if(!fin)
  79. {
  80. return false;
  81. }
  82. const int bsize = 4096;
  83. char buffer[bsize];
  84. // input line is: key:type=value
  85. cmRegularExpression reg("(.*):(.*)=(.*)");
  86. while(fin)
  87. {
  88. // Format is key:type=value
  89. CacheEntry e;
  90. fin.getline(buffer, bsize);
  91. // skip blank lines and comment lines
  92. if(buffer[0] == '#' || buffer[0] == 0)
  93. {
  94. continue;
  95. }
  96. while(buffer[0] == '/')
  97. {
  98. e.m_HelpString += &buffer[2];
  99. fin.getline(buffer, bsize);
  100. if(!fin)
  101. {
  102. continue;
  103. }
  104. }
  105. if(reg.find(buffer))
  106. {
  107. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  108. e.m_Value = reg.match(3);
  109. m_Cache[reg.match(1)] = e;
  110. }
  111. else
  112. {
  113. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  114. }
  115. }
  116. return true;
  117. }
  118. void cmCacheManager::DefineCache(cmMakefile *mf)
  119. {
  120. if (!mf)
  121. {
  122. return;
  123. }
  124. // add definition to the makefile
  125. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  126. i != m_Cache.end(); ++i)
  127. {
  128. const CacheEntry& ce = (*i).second;
  129. mf->AddDefinition((*i).first.c_str(), ce.m_Value.c_str());
  130. }
  131. }
  132. bool cmCacheManager::SaveCache(cmMakefile* mf) const
  133. {
  134. return this->SaveCache(mf->GetHomeOutputDirectory());
  135. }
  136. bool cmCacheManager::SaveCache(const char* path) const
  137. {
  138. std::string cacheFile = path;
  139. cacheFile += "/CMakeCache.txt";
  140. std::string tempFile = cacheFile;
  141. tempFile += ".tmp";
  142. std::ofstream fout(tempFile.c_str());
  143. if(!fout)
  144. {
  145. cmSystemTools::Error("Unable to open cache file for save. ",
  146. cacheFile.c_str());
  147. return false;
  148. }
  149. fout << "# This is the CMakeCache file.\n"
  150. << "# For build in directory: " << path << "\n"
  151. << "# You can edit this file to change values found and used by cmake.\n"
  152. << "# If you do not want to change any of the values, simply exit the editor.\n"
  153. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  154. << "# The syntax for the file is as follows:\n"
  155. << "# KEY:TYPE=VALUE\n"
  156. << "# KEY is the name of a varible in the cache.\n"
  157. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  158. << "# VALUE is the current value for the KEY.\n\n";
  159. for( std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  160. i != m_Cache.end(); ++i)
  161. {
  162. const CacheEntry& ce = (*i).second;
  163. CacheEntryType t = ce.m_Type;
  164. // Format is key:type=value
  165. cmCacheManager::OutputHelpString(fout, ce.m_HelpString);
  166. fout << (*i).first.c_str() << ":"
  167. << cmCacheManagerTypes[t] << "="
  168. << ce.m_Value << "\n";
  169. }
  170. fout << "\n";
  171. fout.close();
  172. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  173. cacheFile.c_str());
  174. cmSystemTools::RemoveFile(tempFile.c_str());
  175. return true;
  176. }
  177. void cmCacheManager::OutputHelpString(std::ofstream& fout,
  178. const std::string& helpString)
  179. {
  180. std::string::size_type end = helpString.size();
  181. if(end == 0)
  182. {
  183. return;
  184. }
  185. std::string oneLine;
  186. std::string::size_type pos = 0;
  187. std::string::size_type nextBreak = 60;
  188. bool done = false;
  189. while(!done)
  190. {
  191. if(nextBreak >= end)
  192. {
  193. nextBreak = end;
  194. done = true;
  195. }
  196. else
  197. {
  198. while(nextBreak < end && helpString[nextBreak] != ' ')
  199. {
  200. nextBreak++;
  201. }
  202. }
  203. oneLine = helpString.substr(pos, nextBreak - pos);
  204. fout << "//" << oneLine.c_str() << "\n";
  205. pos = nextBreak;
  206. nextBreak += 60;
  207. }
  208. }
  209. void cmCacheManager::RemoveCacheEntry(const char* key)
  210. {
  211. m_Cache.erase(key);
  212. }
  213. cmCacheManager::CacheEntry *cmCacheManager::GetCacheEntry(const char* key)
  214. {
  215. if(m_Cache.count(key))
  216. {
  217. return &(m_Cache.find(key)->second);
  218. }
  219. return 0;
  220. }
  221. const char* cmCacheManager::GetCacheValue(const char* key) const
  222. {
  223. if(m_Cache.count(key))
  224. {
  225. return m_Cache.find(key)->second.m_Value.c_str();
  226. }
  227. return 0;
  228. }
  229. bool cmCacheManager::IsOn(const char* key) const
  230. {
  231. if(!m_Cache.count(key))
  232. {
  233. return false;
  234. }
  235. const std::string &v = m_Cache.find(key)->second.m_Value;
  236. return cmSystemTools::IsOn(v.c_str());
  237. }
  238. void cmCacheManager::PrintCache(std::ostream& out) const
  239. {
  240. out << "=================================================" << std::endl;
  241. out << "CMakeCache Contents:" << std::endl;
  242. for(std::map<std::string, CacheEntry>::const_iterator i = m_Cache.begin();
  243. i != m_Cache.end(); ++i)
  244. {
  245. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  246. }
  247. out << "\n\n";
  248. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  249. out << "=================================================" << std::endl;
  250. }
  251. void cmCacheManager::AddCacheEntry(const char* key,
  252. const char* value,
  253. const char* helpString,
  254. CacheEntryType type)
  255. {
  256. CacheEntry e;
  257. e.m_Value = value;
  258. e.m_Type = type;
  259. // make sure we only use unix style paths
  260. if(type == FILEPATH || type == PATH)
  261. {
  262. cmSystemTools::ConvertToUnixSlashes(e.m_Value);
  263. }
  264. e.m_HelpString = helpString;
  265. m_Cache[key] = e;
  266. }
  267. void cmCacheManager::AddCacheEntry(const char* key, bool v,
  268. const char* helpString)
  269. {
  270. if(v)
  271. {
  272. this->AddCacheEntry(key, "ON", helpString, cmCacheManager::BOOL);
  273. }
  274. else
  275. {
  276. this->AddCacheEntry(key, "OFF", helpString, cmCacheManager::BOOL);
  277. }
  278. }