cmListFileCache.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. {
  31. ListFileMap::iterator sl = m_ListFileCache.find(path);
  32. if (sl == m_ListFileCache.end())
  33. {
  34. // if not already in the map, then parse and store the
  35. // file
  36. if(!this->CacheFile(path))
  37. {
  38. return 0;
  39. }
  40. sl = m_ListFileCache.find(path);
  41. if (sl == m_ListFileCache.end())
  42. {
  43. cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
  44. path);
  45. return 0;
  46. }
  47. }
  48. cmListFile& ret = sl->second;
  49. if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
  50. {
  51. if(!this->CacheFile(path))
  52. {
  53. return 0;
  54. }
  55. else
  56. {
  57. sl = m_ListFileCache.find(path);
  58. return &sl->second;
  59. }
  60. }
  61. return &ret;
  62. }
  63. bool cmListFileCache::CacheFile(const char* path)
  64. {
  65. if(!cmSystemTools::FileExists(path))
  66. {
  67. return false;
  68. }
  69. std::ifstream fin(path);
  70. if(!fin)
  71. {
  72. cmSystemTools::Error("cmListFileCache: error can not open file ", path);
  73. return false;
  74. }
  75. cmListFile inFile;
  76. inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
  77. bool parseError;
  78. while ( fin )
  79. {
  80. cmListFileFunction inFunction;
  81. if(cmSystemTools::ParseFunction(fin,
  82. inFunction.m_Name,
  83. inFunction.m_Arguments,
  84. path, parseError))
  85. {
  86. inFile.m_Functions.push_back(inFunction);
  87. }
  88. if (parseError)
  89. {
  90. inFile.m_ModifiedTime = 0;
  91. }
  92. }
  93. m_ListFileCache[path] = inFile;
  94. return true;
  95. }
  96. void cmListFileCache::FlushCache(const char* path)
  97. {
  98. ListFileMap::iterator it = m_ListFileCache.find(path);
  99. if ( it != m_ListFileCache.end() )
  100. {
  101. m_ListFileCache.erase(it);
  102. return;
  103. }
  104. }