ENH: change the search path order (if several Tcl/Tk are installed, the "current...
[cmake.git] / Source / cmListFileCache.cxx
blob0f26bbe11bddb6be9e72abbea366a5d3a0f7abe9
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmListFileCache.cxx,v $
5 Language: C++
6 Date: $Date: 2002-04-11 21:02:10 $
7 Version: $Revision: 1.5 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmListFileCache.h"
18 #include "cmSystemTools.h"
21 cmListFileCache* cmListFileCache::Instance = 0;
24 cmListFileCache* cmListFileCache::GetInstance()
26 if(!cmListFileCache::Instance)
28 cmListFileCache::Instance = new cmListFileCache;
30 return cmListFileCache::Instance;
34 void cmListFileCache::ClearCache()
36 delete cmListFileCache::Instance;
37 cmListFileCache::Instance = 0;
42 cmListFile* cmListFileCache::GetFileCache(const char* path)
44 ListFileMap::iterator sl = m_ListFileCache.find(path);
45 if (sl == m_ListFileCache.end())
47 // if not already in the map, then parse and store the
48 // file
49 if(!this->CacheFile(path))
51 return 0;
53 sl = m_ListFileCache.find(path);
54 if (sl == m_ListFileCache.end())
56 cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
57 path);
58 return 0;
61 cmListFile& ret = sl->second;
62 if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
64 if(!this->CacheFile(path))
66 return 0;
68 else
70 sl = m_ListFileCache.find(path);
71 return &sl->second;
74 return &ret;
78 bool cmListFileCache::CacheFile(const char* path)
80 if(!cmSystemTools::FileExists(path))
82 return false;
85 std::ifstream fin(path);
86 if(!fin)
88 cmSystemTools::Error("cmListFileCache: error can not open file ", path);
89 return false;
91 std::string name;
92 std::vector<std::string> arguments;
93 cmListFile inFile;
94 inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
95 bool parseError;
96 while ( fin )
98 cmListFileFunction inFunction;
99 if(cmSystemTools::ParseFunction(fin,
100 inFunction.m_Name,
101 inFunction.m_Arguments,
102 path, parseError))
104 inFile.m_Functions.push_back(inFunction);
106 if (parseError)
108 inFile.m_ModifiedTime = 0;
111 m_ListFileCache[path] = inFile;
112 return true;