cmCacheManager.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. const char* cmCacheManagerTypes[] =
  14. { "BOOL",
  15. "PATH",
  16. "STRING",
  17. 0
  18. };
  19. cmCacheManager::CacheEntryType cmCacheManager::StringToType(const char* s)
  20. {
  21. int i = 0;
  22. while(cmCacheManagerTypes[i])
  23. {
  24. if(strcmp(s, cmCacheManagerTypes[i]) == 0)
  25. {
  26. return static_cast<CacheEntryType>(i);
  27. }
  28. }
  29. return STRING;
  30. }
  31. cmCacheManager* cmCacheManager::s_Instance = 0;
  32. cmCacheManager* cmCacheManager::GetInstance()
  33. {
  34. if(!cmCacheManager::s_Instance)
  35. {
  36. cmCacheManager::s_Instance = new cmCacheManager;
  37. }
  38. return cmCacheManager::s_Instance;
  39. }
  40. bool cmCacheManager::LoadCache(const char* path)
  41. {
  42. std::ifstream fin(path);
  43. if(!fin)
  44. {
  45. cmSystemTools::Error("Unable to open cache file for load. ", path);
  46. return false;
  47. }
  48. const int bsize = 4096;
  49. char buffer[bsize];
  50. std::string inputLine;
  51. while(fin)
  52. {
  53. CacheEntry e;
  54. std::string key;
  55. fin.getline(buffer, bsize, '|');
  56. key = buffer;
  57. fin.getline(buffer, bsize, '|');
  58. e.m_Value = buffer;
  59. fin.getline(buffer, bsize); // last token is separated by a newline
  60. e.m_Type = cmCacheManager::StringToType(buffer);
  61. if(fin)
  62. {
  63. m_Cache[key] = e;
  64. }
  65. }
  66. }
  67. bool cmCacheManager::SaveCache(const char* path)
  68. {
  69. std::ofstream fout(path);
  70. if(!fout)
  71. {
  72. cmSystemTools::Error("Unable to open cache file for save. ", path);
  73. return false;
  74. }
  75. for( std::map<std::string, CacheEntry>::iterator i = m_Cache.begin();
  76. i != m_Cache.end(); ++i)
  77. {
  78. fout << (*i).first.c_str() << " | " << (*i).second.m_Value << " | ";
  79. CacheEntryType t = (*i).second.m_Type;
  80. fout << cmCacheManagerTypes[t];
  81. }
  82. fout << "\n";
  83. }
  84. void cmCacheManager::AddCacheEntry(const char* key,
  85. const char* value,
  86. CacheEntryType type)
  87. {
  88. CacheEntry e;
  89. e.m_Value = value;
  90. e.m_Type = type;
  91. m_Cache[key] = e;
  92. }
  93. const char* cmCacheManager::GetCacheValue(const char* key)
  94. {
  95. if(m_Cache.count(key))
  96. {
  97. return m_Cache[key].m_Value.c_str();
  98. }
  99. return 0;
  100. }