cmListFileCache.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmListFileCache.h"
  14. #include "cmSystemTools.h"
  15. cmListFileCache* cmListFileCache::Instance = 0;
  16. cmListFileCache* cmListFileCache::GetInstance()
  17. {
  18. if(!cmListFileCache::Instance)
  19. {
  20. cmListFileCache::Instance = new cmListFileCache;
  21. }
  22. return cmListFileCache::Instance;
  23. }
  24. void cmListFileCache::ClearCache()
  25. {
  26. delete cmListFileCache::Instance;
  27. cmListFileCache::Instance = 0;
  28. }
  29. cmListFile* cmListFileCache::GetFileCache(const char* path,
  30. bool requireProjectCommand)
  31. {
  32. ListFileMap::iterator sl = m_ListFileCache.find(path);
  33. if (sl == m_ListFileCache.end())
  34. {
  35. // if not already in the map, then parse and store the
  36. // file
  37. if(!this->CacheFile(path, requireProjectCommand))
  38. {
  39. return 0;
  40. }
  41. sl = m_ListFileCache.find(path);
  42. if (sl == m_ListFileCache.end())
  43. {
  44. cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
  45. path);
  46. return 0;
  47. }
  48. }
  49. cmListFile& ret = sl->second;
  50. if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
  51. {
  52. if(!this->CacheFile(path, requireProjectCommand))
  53. {
  54. return 0;
  55. }
  56. else
  57. {
  58. sl = m_ListFileCache.find(path);
  59. return &sl->second;
  60. }
  61. }
  62. return &ret;
  63. }
  64. bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
  65. {
  66. if(!cmSystemTools::FileExists(path))
  67. {
  68. return false;
  69. }
  70. std::ifstream fin(path);
  71. if(!fin)
  72. {
  73. cmSystemTools::Error("cmListFileCache: error can not open file ", path);
  74. return false;
  75. }
  76. cmListFile inFile;
  77. inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
  78. bool parseError;
  79. while ( fin )
  80. {
  81. cmListFileFunction inFunction;
  82. if(cmSystemTools::ParseFunction(fin,
  83. inFunction.m_Name,
  84. inFunction.m_Arguments,
  85. path, parseError))
  86. {
  87. inFile.m_Functions.push_back(inFunction);
  88. }
  89. if (parseError)
  90. {
  91. inFile.m_ModifiedTime = 0;
  92. }
  93. }
  94. if(requireProjectCommand)
  95. {
  96. bool hasProject = false;
  97. // search for a project command
  98. for(std::vector<cmListFileFunction>::iterator i
  99. = inFile.m_Functions.begin();
  100. i != inFile.m_Functions.end(); ++i)
  101. {
  102. if(i->m_Name == "PROJECT")
  103. {
  104. hasProject = true;
  105. break;
  106. }
  107. }
  108. // if no project command is found, add one
  109. if(!hasProject)
  110. {
  111. cmListFileFunction project;
  112. project.m_Name = "PROJECT";
  113. project.m_Arguments.push_back("Project");
  114. inFile.m_Functions.insert(inFile.m_Functions.begin(),project);
  115. }
  116. }
  117. m_ListFileCache[path] = inFile;
  118. return true;
  119. }
  120. void cmListFileCache::FlushCache(const char* path)
  121. {
  122. ListFileMap::iterator it = m_ListFileCache.find(path);
  123. if ( it != m_ListFileCache.end() )
  124. {
  125. m_ListFileCache.erase(it);
  126. return;
  127. }
  128. }