cmCacheManager.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 0
  22. };
  23. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  24. {
  25. int i = 0;
  26. while(cmCacheManagerTypes[i])
  27. {
  28. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  29. {
  30. return static_cast<CacheEntryType>(i);
  31. }
  32. ++i;
  33. }
  34. return STRING;
  35. }
  36. cmCacheManager* cmCacheManager::s_Instance = 0;
  37. cmCacheManager* cmCacheManager::GetInstance()
  38. {
  39. if(!cmCacheManager::s_Instance)
  40. {
  41. cmCacheManager::s_Instance = new cmCacheManager;
  42. }
  43. return cmCacheManager::s_Instance;
  44. }
  45. bool cmCacheManager::LoadCache(cmMakefile* mf)
  46. {
  47. std::string cacheFile = mf->GetHomeOutputDirectory();
  48. cacheFile += "/CMakeCache.txt";
  49. // clear the old cache
  50. m_Cache.clear();
  51. std::ifstream fin(cacheFile.c_str());
  52. if(!fin)
  53. {
  54. return false;
  55. }
  56. const int bsize = 4096;
  57. char buffer[bsize];
  58. // input line is: key:type=value
  59. cmRegularExpression reg("(.*):(.*)=(.*)");
  60. while(fin)
  61. {
  62. // Format is key:type=value
  63. CacheEntry e;
  64. fin.getline(buffer, bsize);
  65. // skip blank lines and comment lines
  66. if(buffer[0] == '#' || buffer[0] == 0)
  67. {
  68. continue;
  69. }
  70. if(reg.find(buffer))
  71. {
  72. e.m_Type = cmCacheManager::StringToType(reg.match(2).c_str());
  73. e.m_Value = reg.match(3);
  74. m_Cache[reg.match(1)] = e;
  75. }
  76. else
  77. {
  78. cmSystemTools::Error("Parse error in cache file ", cacheFile.c_str());
  79. }
  80. }
  81. return true;
  82. }
  83. bool cmCacheManager::SaveCache(cmMakefile* mf)
  84. {
  85. std::string cacheFile = mf->GetHomeOutputDirectory();
  86. cacheFile += "/CMakeCache.txt";
  87. std::string tempFile = cacheFile;
  88. tempFile += ".tmp";
  89. std::ofstream fout(tempFile.c_str());
  90. if(!fout)
  91. {
  92. cmSystemTools::Error("Unable to open cache file for save. ",
  93. cacheFile.c_str());
  94. return false;
  95. }
  96. fout << "# This is the CMakeCache file.\n"
  97. << "# You can edit this file to change values found and used by cmake.\n"
  98. << "# If you do not want to change any of the values, simply exit the editor.\n"
  99. << "# If you do want to change a value, simply edit, save, and exit the editor.\n"
  100. << "# The syntax for the file is as follows:\n"
  101. << "# KEY:TYPE=VALUE\n"
  102. << "# KEY is the name of a varible in the cache.\n"
  103. << "# TYPE is a hint to GUI's for the type of VALUE, DO NOT EDIT TYPE!.\n"
  104. << "# VALUE is the current value for the KEY.\n\n";
  105. for( std::map<std::string, CacheEntry>::iterator i = m_Cache.begin();
  106. i != m_Cache.end(); ++i)
  107. {
  108. CacheEntryType t = (*i).second.m_Type;
  109. // Format is key:type=value
  110. fout << (*i).first.c_str() << ":"
  111. << cmCacheManagerTypes[t] << "="
  112. << (*i).second.m_Value << "\n";
  113. }
  114. fout << "\n";
  115. fout.close();
  116. cmSystemTools::CopyFileIfDifferent(tempFile.c_str(),
  117. cacheFile.c_str());
  118. cmSystemTools::RemoveFile(tempFile.c_str());
  119. return true;
  120. }
  121. void cmCacheManager::AddCacheEntry(const char* key,
  122. const char* value,
  123. CacheEntryType type)
  124. {
  125. CacheEntry e;
  126. e.m_Value = value;
  127. e.m_Type = type;
  128. m_Cache[key] = e;
  129. }
  130. const char* cmCacheManager::GetCacheValue(const char* key)
  131. {
  132. if(m_Cache.count(key))
  133. {
  134. return m_Cache[key].m_Value.c_str();
  135. }
  136. return 0;
  137. }
  138. void cmCacheManager::PrintCache(std::ostream& out)
  139. {
  140. out << "=================================================" << std::endl;
  141. out << "CMakeCache Contents:" << std::endl;
  142. for(std::map<std::string, CacheEntry>::iterator i = m_Cache.begin();
  143. i != m_Cache.end(); ++i)
  144. {
  145. out << (*i).first.c_str() << " = " << (*i).second.m_Value.c_str() << std::endl;
  146. }
  147. out << "\n\n";
  148. out << "To change values in the CMakeCache, \nedit CMakeCache.txt in your output directory.\n";
  149. out << "=================================================" << std::endl;
  150. }