cmGlob.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "cmGlob.h"
  14. #include <cmsys/Directory.hxx>
  15. #include <cmsys/RegularExpression.hxx>
  16. #include <cmsys/SystemTools.hxx>
  17. #include <ctype.h>
  18. #if defined( _WIN32 ) || defined( APPLE ) || defined( __CYGWIN__ )
  19. // On Windows and apple, no difference between lower and upper case
  20. #define CM_GLOB_CASE_INDEPENDENT
  21. #endif
  22. #if defined( _WIN32 ) || defined( __CYGWIN__ )
  23. // Handle network paths
  24. #define CM_GLOB_SUPPORT_NETWORK_PATHS
  25. #endif
  26. class cmGlobInternal
  27. {
  28. public:
  29. std::vector<std::string> Files;
  30. std::vector<cmsys::RegularExpression> Expressions;
  31. std::vector<std::string> TextExpressions;
  32. };
  33. cmGlob::cmGlob()
  34. {
  35. this->Internals = new cmGlobInternal;
  36. this->Recurse = false;
  37. }
  38. cmGlob::~cmGlob()
  39. {
  40. delete this->Internals;
  41. }
  42. void cmGlob::Escape(int ch, char* buffer)
  43. {
  44. if (! (
  45. 'a' <= ch && ch <= 'z' ||
  46. 'A' <= ch && ch <= 'Z' ||
  47. '0' <= ch && ch <= '9') )
  48. {
  49. sprintf(buffer, "\\%c", ch);
  50. }
  51. else
  52. {
  53. #if defined( CM_GLOB_CASE_INDEPENDENT )
  54. // On Windows and apple, no difference between lower and upper case
  55. sprintf(buffer, "%c", tolower(ch));
  56. #else
  57. sprintf(buffer, "%c", ch);
  58. #endif
  59. }
  60. }
  61. std::vector<std::string>& cmGlob::GetFiles()
  62. {
  63. return this->Internals->Files;
  64. }
  65. std::string cmGlob::ConvertExpression(const std::string& expr)
  66. {
  67. std::string::size_type i = 0;
  68. std::string::size_type n = expr.size();
  69. std::string res = "^";
  70. std::string stuff = "";
  71. while ( i < n )
  72. {
  73. int c = expr[i];
  74. i = i+1;
  75. if ( c == '*' )
  76. {
  77. res = res + ".*";
  78. }
  79. else if ( c == '?' )
  80. {
  81. res = res + ".";
  82. }
  83. else if ( c == '[' )
  84. {
  85. std::string::size_type j = i;
  86. if ( j < n && ( expr[j] == '!' || expr[j] == '^' ) )
  87. {
  88. j = j+1;
  89. }
  90. if ( j < n && expr[j] == ']' )
  91. {
  92. j = j+1;
  93. }
  94. while ( j < n && expr[j] != ']' )
  95. {
  96. j = j+1;
  97. }
  98. if ( j >= n )
  99. {
  100. res = res + "\\[";
  101. }
  102. else
  103. {
  104. stuff = "";
  105. std::string::size_type cc;
  106. for ( cc = i; cc < j; cc ++ )
  107. {
  108. if ( expr[cc] == '\\' )
  109. {
  110. stuff += "\\\\";
  111. }
  112. else
  113. {
  114. stuff += expr[cc];
  115. }
  116. }
  117. i = j+1;
  118. if ( stuff[0] == '!' || stuff[0] == '^' )
  119. {
  120. stuff = '^' + stuff.substr(1);
  121. }
  122. else if ( stuff[0] == '^' )
  123. {
  124. stuff = '\\' + stuff;
  125. }
  126. res = res + "[" + stuff + "]";
  127. }
  128. }
  129. else
  130. {
  131. char buffer[100];
  132. buffer[0] = 0;
  133. this->Escape(c, buffer);
  134. res = res + buffer;
  135. }
  136. }
  137. return res + "$";
  138. }
  139. void cmGlob::RecurseDirectory(std::string::size_type start,
  140. const std::string& dir, bool dir_only)
  141. {
  142. cmsys::Directory d;
  143. if ( !d.Load(dir.c_str()) )
  144. {
  145. return;
  146. }
  147. unsigned long cc;
  148. std::string fullname;
  149. std::string realname;
  150. std::string fname;
  151. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  152. {
  153. fname = d.GetFile(cc);
  154. if ( strcmp(fname.c_str(), ".") == 0 ||
  155. strcmp(fname.c_str(), "..") == 0 )
  156. {
  157. continue;
  158. }
  159. if ( start == 0 )
  160. {
  161. realname = dir + fname;
  162. }
  163. else
  164. {
  165. realname = dir + "/" + fname;
  166. }
  167. #if defined( CM_GLOB_CASE_INDEPENDENT )
  168. // On Windows and apple, no difference between lower and upper case
  169. fname = cmsys::SystemTools::LowerCase(fname);
  170. #endif
  171. if ( start == 0 )
  172. {
  173. fullname = dir + fname;
  174. }
  175. else
  176. {
  177. fullname = dir + "/" + fname;
  178. }
  179. if ( !dir_only || !cmsys::SystemTools::FileIsDirectory(realname.c_str()) )
  180. {
  181. if ( this->Internals->Expressions[this->Internals->Expressions.size()-1].find(fname.c_str()) )
  182. {
  183. this->Internals->Files.push_back(realname);
  184. }
  185. }
  186. if ( cmsys::SystemTools::FileIsDirectory(realname.c_str()) )
  187. {
  188. this->RecurseDirectory(start+1, realname, dir_only);
  189. }
  190. }
  191. }
  192. void cmGlob::ProcessDirectory(std::string::size_type start,
  193. const std::string& dir, bool dir_only)
  194. {
  195. //std::cout << "ProcessDirectory: " << dir << std::endl;
  196. bool last = ( start == this->Internals->Expressions.size()-1 );
  197. if ( last && this->Recurse )
  198. {
  199. this->RecurseDirectory(start, dir, dir_only);
  200. return;
  201. }
  202. cmsys::Directory d;
  203. if ( !d.Load(dir.c_str()) )
  204. {
  205. return;
  206. }
  207. unsigned long cc;
  208. std::string fullname;
  209. std::string realname;
  210. std::string fname;
  211. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  212. {
  213. fname = d.GetFile(cc);
  214. if ( strcmp(fname.c_str(), ".") == 0 ||
  215. strcmp(fname.c_str(), "..") == 0 )
  216. {
  217. continue;
  218. }
  219. if ( start == 0 )
  220. {
  221. realname = dir + fname;
  222. }
  223. else
  224. {
  225. realname = dir + "/" + fname;
  226. }
  227. #if defined( CM_GLOB_CASE_INDEPENDENT )
  228. // On Windows and apple, no difference between lower and upper case
  229. fname = cmsys::SystemTools::LowerCase(fname);
  230. #endif
  231. if ( start == 0 )
  232. {
  233. fullname = dir + fname;
  234. }
  235. else
  236. {
  237. fullname = dir + "/" + fname;
  238. }
  239. //std::cout << "Look at file: " << fname << std::endl;
  240. //std::cout << "Match: " << this->Internals->TextExpressions[start].c_str() << std::endl;
  241. //std::cout << "Full name: " << fullname << std::endl;
  242. if ( (!dir_only || !last) && !cmsys::SystemTools::FileIsDirectory(realname.c_str()) )
  243. {
  244. continue;
  245. }
  246. if ( this->Internals->Expressions[start].find(fname.c_str()) )
  247. {
  248. if ( last )
  249. {
  250. this->Internals->Files.push_back(realname);
  251. }
  252. else
  253. {
  254. this->ProcessDirectory(start+1, realname + "/", dir_only);
  255. }
  256. }
  257. }
  258. }
  259. bool cmGlob::FindFiles(const std::string& inexpr)
  260. {
  261. std::string cexpr;
  262. std::string::size_type cc;
  263. std::string expr = inexpr;
  264. this->Internals->Expressions.clear();
  265. this->Internals->Files.clear();
  266. if ( !cmsys::SystemTools::FileIsFullPath(expr.c_str()) )
  267. {
  268. expr = cmsys::SystemTools::GetCurrentWorkingDirectory();
  269. expr += "/" + inexpr;
  270. }
  271. std::string fexpr = expr;
  272. int skip = 0;
  273. int last_slash = 0;
  274. for ( cc = 0; cc < expr.size(); cc ++ )
  275. {
  276. if ( cc > 0 && expr[cc] == '/' && expr[cc-1] != '\\' )
  277. {
  278. last_slash = cc;
  279. }
  280. if ( cc > 0 &&
  281. (expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
  282. expr[cc-1] != '\\' )
  283. {
  284. break;
  285. }
  286. }
  287. if ( last_slash > 0 )
  288. {
  289. //std::cout << "I can skip: " << fexpr.substr(0, last_slash) << std::endl;
  290. skip = last_slash;
  291. }
  292. if ( skip == 0 )
  293. {
  294. #if defined( CM_GLOB_SUPPORT_NETWORK_PATHS )
  295. // Handle network paths
  296. if ( expr[0] == '/' && expr[1] == '/' )
  297. {
  298. int cnt = 0;
  299. for ( cc = 2; cc < expr.size(); cc ++ )
  300. {
  301. if ( expr[cc] == '/' )
  302. {
  303. cnt ++;
  304. if ( cnt == 2 )
  305. {
  306. break;
  307. }
  308. }
  309. }
  310. skip = cc + 1;
  311. }
  312. else
  313. #endif
  314. // Handle drive letters on Windows
  315. if ( expr[1] == ':' && expr[0] != '/' )
  316. {
  317. skip = 2;
  318. }
  319. }
  320. if ( skip > 0 )
  321. {
  322. expr = expr.substr(skip);
  323. }
  324. cexpr = "";
  325. for ( cc = 0; cc < expr.size(); cc ++ )
  326. {
  327. int ch = expr[cc];
  328. if ( ch == '/' )
  329. {
  330. if ( cexpr.size() > 0 )
  331. {
  332. this->AddExpression(cexpr.c_str());
  333. }
  334. cexpr = "";
  335. }
  336. else
  337. {
  338. cexpr.append(1, (char)ch);
  339. }
  340. }
  341. if ( cexpr.size() > 0 )
  342. {
  343. this->AddExpression(cexpr.c_str());
  344. }
  345. // Handle network paths
  346. if ( skip > 0 )
  347. {
  348. this->ProcessDirectory(0, fexpr.substr(0, skip) + "/",
  349. true);
  350. }
  351. else
  352. {
  353. this->ProcessDirectory(0, "/", true);
  354. }
  355. return true;
  356. }
  357. void cmGlob::AddExpression(const char* expr)
  358. {
  359. this->Internals->Expressions.push_back(
  360. cmsys::RegularExpression(
  361. this->ConvertExpression(expr).c_str()));
  362. this->Internals->TextExpressions.push_back(this->ConvertExpression(expr));
  363. }