cmListFileCache.cxx 8.54 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1
2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
Will Schroeder's avatar
Will Schroeder committed
4
5
6
7
8
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

9
10
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
Will Schroeder's avatar
Will Schroeder committed
11
12
13
14
15
16

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
17
18
#include "cmListFileCache.h"
#include "cmSystemTools.h"
19
#include "cmRegularExpression.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

cmListFileCache* cmListFileCache::Instance = 0;


cmListFileCache* cmListFileCache::GetInstance()
{
  if(!cmListFileCache::Instance)
    {
    cmListFileCache::Instance = new cmListFileCache;
    }
  return cmListFileCache::Instance;
}


void cmListFileCache::ClearCache()
{
  delete cmListFileCache::Instance;
  cmListFileCache::Instance = 0;
}



42
43
cmListFile* cmListFileCache::GetFileCache(const char* path,
                                          bool requireProjectCommand)
44
45
46
47
48
49
{
  ListFileMap::iterator sl = m_ListFileCache.find(path);
  if (sl == m_ListFileCache.end())
    {
    // if not already in the map, then parse and store the 
    // file
50
    if(!this->CacheFile(path, requireProjectCommand))
51
52
53
54
55
56
57
58
59
60
61
62
63
64
      {
      return 0;
      }
    sl = m_ListFileCache.find(path);
    if (sl == m_ListFileCache.end())
      {
      cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
                           path);
      return 0;
      }
    }
  cmListFile& ret = sl->second;
  if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
    {
65
    if(!this->CacheFile(path, requireProjectCommand))
66
67
68
69
70
71
72
73
74
75
76
77
78
      {
      return 0;
      }
    else
      {
      sl = m_ListFileCache.find(path);
      return &sl->second;
      }
    } 
  return &ret;
}


79
bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
80
{
Bill Hoffman's avatar
Bill Hoffman committed
81
82
83
84
85
  if(!cmSystemTools::FileExists(path))
    {
    return false;
    }
    
86
87
88
  std::ifstream fin(path);
  if(!fin)
    {
Bill Hoffman's avatar
Bill Hoffman committed
89
    cmSystemTools::Error("cmListFileCache: error can not open file ", path);
90
91
    return false;
    }
92
  long line=0;
93
94
  cmListFile inFile;
  inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
95
  bool parseError;
96
97
98
  while ( fin )
    {
    cmListFileFunction inFunction;
99
100
    if(cmListFileCache::ParseFunction(fin, inFunction, path, parseError,
                                      &line))
101
      {
102
      inFunction.m_FilePath = path;
103
104
      inFile.m_Functions.push_back(inFunction);
      }
105
106
107
108
    if (parseError)
      {
      inFile.m_ModifiedTime = 0;
      }
109
    }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  if(requireProjectCommand)
    {
    bool hasProject = false;
    // search for a project command
    for(std::vector<cmListFileFunction>::iterator i 
          = inFile.m_Functions.begin();
        i != inFile.m_Functions.end(); ++i)
      {
      if(i->m_Name == "PROJECT")
        {
        hasProject = true;
        break;
        }
      }
    // if no project command is found, add one
    if(!hasProject)
      {
      cmListFileFunction project;
      project.m_Name = "PROJECT";
129
130
      cmListFileArgument prj("Project", false);
      project.m_Arguments.push_back(prj);
131
      inFile.m_Functions.insert(inFile.m_Functions.begin(),project);
132
133
      }
    }
134
135
136
  m_ListFileCache[path] = inFile;
  return true;
}
137
138
139
140
141
142
143
144
145
146

void cmListFileCache::FlushCache(const char* path)
{
  ListFileMap::iterator it = m_ListFileCache.find(path);
  if ( it != m_ListFileCache.end() )
    {
    m_ListFileCache.erase(it);
    return;
    }
}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

inline void RemoveComments(char* ptr)
{
  while(*ptr)
    {
    if(*ptr == '#')
      {
      *ptr = 0;
      break;
      }
    ++ptr;
    }
}

bool cmListFileCache::ParseFunction(std::ifstream& fin,
                                    cmListFileFunction& function,
                                    const char* filename,
                                    bool& parseError,
                                    long* line)
{
  parseError = false;
  std::string& name = function.m_Name;
  std::vector<cmListFileArgument>& arguments = function.m_Arguments;
  name = "";
  arguments = std::vector<cmListFileArgument>();
  const int BUFFER_SIZE = 4096;
  char inbuffer[BUFFER_SIZE];
  if(!fin)
    {
    return false;
    }
  if(fin.getline(inbuffer, BUFFER_SIZE ) )
    {
    if(line) { ++*line; }
    RemoveComments(inbuffer);
    cmRegularExpression blankLine("^[ \t\r]*$");
    cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
    cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
    cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");

    // check for blank line or comment
    if(blankLine.find(inbuffer) )
      {
      return false;
      }
    // look for a oneline fun(arg arg2) 
    else if(oneLiner.find(inbuffer))
      {
      // the arguments are the second match
      std::string args = oneLiner.match(2);
      name = oneLiner.match(1);
      // break up the arguments
      cmListFileCache::GetArguments(args, arguments);
      if(line)
        {
        function.m_Line = *line;
        }
      return true;
      }
    // look for a start of a multiline with no trailing ")"  fun(arg arg2 
    else if(multiLine.find(inbuffer))
      {
      name = multiLine.match(1);
      std::string args = multiLine.match(2);
      cmListFileCache::GetArguments(args, arguments);
      if(line)
        {
        function.m_Line = *line;
        }
      // Read lines until the closing paren is hit
      bool done = false;
      while(!done)
        {
        // read lines until the end paren is found
        if(fin.getline(inbuffer, BUFFER_SIZE ) )
          {
223
          if(line) { ++*line; }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
          RemoveComments(inbuffer);
          // Check for comment lines and ignore them.
          if(blankLine.find(inbuffer))
            { continue; }
          // Is this the last line?
          if(lastLine.find(inbuffer))
            {
            done = true;
            std::string gargs = lastLine.match(1);
            cmListFileCache::GetArguments(gargs, arguments);
            }
          else
            {
            std::string line = inbuffer;
            cmListFileCache::GetArguments(line, arguments);
            }
          }
        else
          {
          parseError = true;
          cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
                               filename, "\nCurrent line:", inbuffer);
          return false;
          }
        }
      return true;
      }
    else
      {
      parseError = true;
      cmSystemTools::Error("Parse error in read function\nIn file:", 
                           filename, "\nCurrent line:", inbuffer);
      return false;
      }
    }
  return false;

}

void cmListFileCache::GetArguments(std::string& line,
                                 std::vector<cmListFileArgument>& arguments)
{
  // Match a normal argument (not quoted, no spaces).
  cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  // Match a quoted argument (surrounded by double quotes, spaces allowed).
  cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");

  bool done = false;
  while(!done)
    {
    std::string arg;
    std::string::size_type endpos=0;
    bool quoted = false;
    bool foundQuoted = quotedArgument.find(line.c_str());
    bool foundNormal = normalArgument.find(line.c_str());

    if(foundQuoted && foundNormal)
      {
      // Both matches were found.  Take the earlier one.
      // Favor double-quoted version if there is a tie.
      if(normalArgument.start(1) < quotedArgument.start(1))
        {
        arg = normalArgument.match(1);
        endpos = normalArgument.end(1);
        }
      else
        {
        arg = quotedArgument.match(1);
        endpos = quotedArgument.end(1);
        // Strip off the double quotes on the ends.
        arg = arg.substr(1, arg.length()-2);
        quoted = true;
        }
      }    
    else if(foundQuoted)
      {
      arg = quotedArgument.match(1);
      endpos = quotedArgument.end(1);
      // Strip off the double quotes on the ends.
      arg = arg.substr(1, arg.length()-2);
      quoted = true;
      }
    else if(foundNormal)
      {
      arg = normalArgument.match(1);
      endpos = normalArgument.end(1);
      }
    else
      {
      done = true;
      }
    if(!done)
      {
      cmListFileArgument a(cmSystemTools::RemoveEscapes(arg.c_str()), quoted);
      arguments.push_back(a);
      line = line.substr(endpos, line.length() - endpos);
      }
    }
}