cmListFileCache.cxx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. bool parseError;
  64. while ( fin )
  65. {
  66. cmListFileFunction inFunction;
  67. if(cmSystemTools::ParseFunction(fin,
  68. inFunction.m_Name,
  69. inFunction.m_Arguments,
  70. path, parseError))
  71. {
  72. inFile.m_Functions.push_back(inFunction);
  73. }
  74. if (parseError)
  75. {
  76. inFile.m_ModifiedTime = 0;
  77. }
  78. }
  79. m_ListFileCache[path] = inFile;
  80. return true;
  81. }