cmListFileCache.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. std::ifstream fin(path);
  66. if(!fin)
  67. {
  68. cmSystemTools::Error("error can not open file ", path);
  69. return false;
  70. }
  71. std::string name;
  72. std::vector<std::string> arguments;
  73. cmListFile inFile;
  74. inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
  75. bool parseError;
  76. while ( fin )
  77. {
  78. cmListFileFunction inFunction;
  79. if(cmSystemTools::ParseFunction(fin,
  80. inFunction.m_Name,
  81. inFunction.m_Arguments,
  82. path, parseError))
  83. {
  84. inFile.m_Functions.push_back(inFunction);
  85. }
  86. if (parseError)
  87. {
  88. inFile.m_ModifiedTime = 0;
  89. }
  90. }
  91. m_ListFileCache[path] = inFile;
  92. return true;
  93. }