cmListFileCache.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "cmRegularExpression.h"
  16. cmListFileCache* cmListFileCache::Instance = 0;
  17. cmListFileCache* cmListFileCache::GetInstance()
  18. {
  19. if(!cmListFileCache::Instance)
  20. {
  21. cmListFileCache::Instance = new cmListFileCache;
  22. }
  23. return cmListFileCache::Instance;
  24. }
  25. void cmListFileCache::ClearCache()
  26. {
  27. delete cmListFileCache::Instance;
  28. cmListFileCache::Instance = 0;
  29. }
  30. cmListFile* cmListFileCache::GetFileCache(const char* path,
  31. bool requireProjectCommand)
  32. {
  33. ListFileMap::iterator sl = m_ListFileCache.find(path);
  34. if (sl == m_ListFileCache.end())
  35. {
  36. // if not already in the map, then parse and store the
  37. // file
  38. if(!this->CacheFile(path, requireProjectCommand))
  39. {
  40. return 0;
  41. }
  42. sl = m_ListFileCache.find(path);
  43. if (sl == m_ListFileCache.end())
  44. {
  45. cmSystemTools::Error("Fatal error, in cmListFileCache CacheFile failed",
  46. path);
  47. return 0;
  48. }
  49. }
  50. cmListFile& ret = sl->second;
  51. if(cmSystemTools::ModifiedTime(path) > ret.m_ModifiedTime )
  52. {
  53. if(!this->CacheFile(path, requireProjectCommand))
  54. {
  55. return 0;
  56. }
  57. else
  58. {
  59. sl = m_ListFileCache.find(path);
  60. return &sl->second;
  61. }
  62. }
  63. return &ret;
  64. }
  65. bool cmListFileCache::CacheFile(const char* path, bool requireProjectCommand)
  66. {
  67. if(!cmSystemTools::FileExists(path))
  68. {
  69. return false;
  70. }
  71. std::ifstream fin(path);
  72. if(!fin)
  73. {
  74. cmSystemTools::Error("cmListFileCache: error can not open file ", path);
  75. return false;
  76. }
  77. long line=0;
  78. cmListFile inFile;
  79. inFile.m_ModifiedTime = cmSystemTools::ModifiedTime(path);
  80. bool parseError;
  81. while ( fin )
  82. {
  83. cmListFileFunction inFunction;
  84. if(cmListFileCache::ParseFunction(fin, inFunction, path, parseError,
  85. &line))
  86. {
  87. inFunction.m_FilePath = path;
  88. inFile.m_Functions.push_back(inFunction);
  89. }
  90. if (parseError)
  91. {
  92. inFile.m_ModifiedTime = 0;
  93. }
  94. }
  95. if(requireProjectCommand)
  96. {
  97. bool hasProject = false;
  98. // search for a project command
  99. for(std::vector<cmListFileFunction>::iterator i
  100. = inFile.m_Functions.begin();
  101. i != inFile.m_Functions.end(); ++i)
  102. {
  103. if(i->m_Name == "PROJECT")
  104. {
  105. hasProject = true;
  106. break;
  107. }
  108. }
  109. // if no project command is found, add one
  110. if(!hasProject)
  111. {
  112. cmListFileFunction project;
  113. project.m_Name = "PROJECT";
  114. cmListFileArgument prj("Project", false);
  115. project.m_Arguments.push_back(prj);
  116. inFile.m_Functions.insert(inFile.m_Functions.begin(),project);
  117. }
  118. }
  119. m_ListFileCache[path] = inFile;
  120. return true;
  121. }
  122. void cmListFileCache::FlushCache(const char* path)
  123. {
  124. ListFileMap::iterator it = m_ListFileCache.find(path);
  125. if ( it != m_ListFileCache.end() )
  126. {
  127. m_ListFileCache.erase(it);
  128. return;
  129. }
  130. }
  131. inline void RemoveComments(char* ptr)
  132. {
  133. while(*ptr)
  134. {
  135. if(*ptr == '#')
  136. {
  137. *ptr = 0;
  138. break;
  139. }
  140. ++ptr;
  141. }
  142. }
  143. bool cmListFileCache::ParseFunction(std::ifstream& fin,
  144. cmListFileFunction& function,
  145. const char* filename,
  146. bool& parseError,
  147. long* line)
  148. {
  149. parseError = false;
  150. std::string& name = function.m_Name;
  151. std::vector<cmListFileArgument>& arguments = function.m_Arguments;
  152. name = "";
  153. arguments = std::vector<cmListFileArgument>();
  154. const int BUFFER_SIZE = 4096;
  155. char inbuffer[BUFFER_SIZE];
  156. if(!fin)
  157. {
  158. return false;
  159. }
  160. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  161. {
  162. if(line) { ++*line; }
  163. RemoveComments(inbuffer);
  164. cmRegularExpression blankLine("^[ \t\r]*$");
  165. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  166. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  167. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  168. // check for blank line or comment
  169. if(blankLine.find(inbuffer) )
  170. {
  171. return false;
  172. }
  173. // look for a oneline fun(arg arg2)
  174. else if(oneLiner.find(inbuffer))
  175. {
  176. // the arguments are the second match
  177. std::string args = oneLiner.match(2);
  178. name = oneLiner.match(1);
  179. // break up the arguments
  180. cmListFileCache::GetArguments(args, arguments);
  181. if(line)
  182. {
  183. function.m_Line = *line;
  184. }
  185. return true;
  186. }
  187. // look for a start of a multiline with no trailing ")" fun(arg arg2
  188. else if(multiLine.find(inbuffer))
  189. {
  190. name = multiLine.match(1);
  191. std::string args = multiLine.match(2);
  192. cmListFileCache::GetArguments(args, arguments);
  193. if(line)
  194. {
  195. function.m_Line = *line;
  196. }
  197. // Read lines until the closing paren is hit
  198. bool done = false;
  199. while(!done)
  200. {
  201. // read lines until the end paren is found
  202. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  203. {
  204. ++line;
  205. RemoveComments(inbuffer);
  206. // Check for comment lines and ignore them.
  207. if(blankLine.find(inbuffer))
  208. { continue; }
  209. // Is this the last line?
  210. if(lastLine.find(inbuffer))
  211. {
  212. done = true;
  213. std::string gargs = lastLine.match(1);
  214. cmListFileCache::GetArguments(gargs, arguments);
  215. }
  216. else
  217. {
  218. std::string line = inbuffer;
  219. cmListFileCache::GetArguments(line, arguments);
  220. }
  221. }
  222. else
  223. {
  224. parseError = true;
  225. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  226. filename, "\nCurrent line:", inbuffer);
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. else
  233. {
  234. parseError = true;
  235. cmSystemTools::Error("Parse error in read function\nIn file:",
  236. filename, "\nCurrent line:", inbuffer);
  237. return false;
  238. }
  239. }
  240. return false;
  241. }
  242. void cmListFileCache::GetArguments(std::string& line,
  243. std::vector<cmListFileArgument>& arguments)
  244. {
  245. // Match a normal argument (not quoted, no spaces).
  246. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  247. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  248. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  249. bool done = false;
  250. while(!done)
  251. {
  252. std::string arg;
  253. std::string::size_type endpos=0;
  254. bool quoted = false;
  255. bool foundQuoted = quotedArgument.find(line.c_str());
  256. bool foundNormal = normalArgument.find(line.c_str());
  257. if(foundQuoted && foundNormal)
  258. {
  259. // Both matches were found. Take the earlier one.
  260. // Favor double-quoted version if there is a tie.
  261. if(normalArgument.start(1) < quotedArgument.start(1))
  262. {
  263. arg = normalArgument.match(1);
  264. endpos = normalArgument.end(1);
  265. }
  266. else
  267. {
  268. arg = quotedArgument.match(1);
  269. endpos = quotedArgument.end(1);
  270. // Strip off the double quotes on the ends.
  271. arg = arg.substr(1, arg.length()-2);
  272. quoted = true;
  273. }
  274. }
  275. else if(foundQuoted)
  276. {
  277. arg = quotedArgument.match(1);
  278. endpos = quotedArgument.end(1);
  279. // Strip off the double quotes on the ends.
  280. arg = arg.substr(1, arg.length()-2);
  281. quoted = true;
  282. }
  283. else if(foundNormal)
  284. {
  285. arg = normalArgument.match(1);
  286. endpos = normalArgument.end(1);
  287. }
  288. else
  289. {
  290. done = true;
  291. }
  292. if(!done)
  293. {
  294. cmListFileArgument a(cmSystemTools::RemoveEscapes(arg.c_str()), quoted);
  295. arguments.push_back(a);
  296. line = line.substr(endpos, line.length() - endpos);
  297. }
  298. }
  299. }