cmListFileCache.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "cmListFileCache.h"
  2. #include "cmSystemTools.h"
  3. cmListFileCache* cmListFileCache::Instance = 0;
  4. cmListFileCache* cmListFileCache::GetInstance()
  5. {
  6. if(!cmListFileCache::Instance)
  7. {
  8. cmListFileCache::Instance = new cmListFileCache;
  9. }
  10. return cmListFileCache::Instance;
  11. }
  12. void cmListFileCache::ClearCache()
  13. {
  14. delete cmListFileCache::Instance;
  15. cmListFileCache::Instance = 0;
  16. }
  17. cmListFile* cmListFileCache::GetFileCache(const char* path)
  18. {
  19. ListFileMap::iterator sl = m_ListFileCache.find(path);
  20. if (sl == m_ListFileCache.end())
  21. {
  22. // if not already in the map, then parse and store the
  23. // file
  24. if(!this->CacheFile(path))
  25. {
  26. return 0;
  27. }
  28. sl = m_ListFileCache.find(path);
  29. if (sl == m_ListFileCache.end())
  30. {
  31. cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
  32. path);
  33. return 0;
  34. }
  35. }
  36. cmListFile& ret = sl->second;
  37. if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
  38. {
  39. if(!this->CacheFile(path))
  40. {
  41. return 0;
  42. }
  43. else
  44. {
  45. sl = m_ListFileCache.find(path);
  46. return &sl->second;
  47. }
  48. }
  49. return &ret;
  50. }
  51. bool cmListFileCache::CacheFile(const char* path)
  52. {
  53. std::ifstream fin(path);
  54. if(!fin)
  55. {
  56. cmSystemTools::Error("error can not open file ", path);
  57. return false;
  58. }
  59. std::string name;
  60. std::vector<std::string> arguments;
  61. cmListFile inFile;
  62. inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
  63. while ( fin )
  64. {
  65. cmListFileFunction inFunction;
  66. if(cmSystemTools::ParseFunction(fin,
  67. inFunction.m_Name,
  68. inFunction.m_Arguments,
  69. path))
  70. {
  71. inFile.m_Functions.push_back(inFunction);
  72. }
  73. }
  74. m_ListFileCache[path] = inFile;
  75. return true;
  76. }