Glob.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(Glob.hxx)
  12. #include KWSYS_HEADER(Configure.hxx)
  13. #include KWSYS_HEADER(RegularExpression.hxx)
  14. #include KWSYS_HEADER(SystemTools.hxx)
  15. #include KWSYS_HEADER(Directory.hxx)
  16. #include KWSYS_HEADER(stl/string)
  17. #include KWSYS_HEADER(stl/vector)
  18. // Work-around CMake dependency scanning limitation. This must
  19. // duplicate the above list of headers.
  20. #if 0
  21. # include "Glob.hxx.in"
  22. # include "Directory.hxx.in"
  23. # include "Configure.hxx.in"
  24. # include "RegularExpression.hxx.in"
  25. # include "SystemTools.hxx.in"
  26. # include "kwsys_stl.hxx.in"
  27. # include "kwsys_stl_string.hxx.in"
  28. #endif
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. namespace KWSYS_NAMESPACE
  33. {
  34. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  35. // On Windows and apple, no difference between lower and upper case
  36. # define KWSYS_GLOB_CASE_INDEPENDENT
  37. #endif
  38. #if defined(_WIN32) || defined(__CYGWIN__)
  39. // Handle network paths
  40. # define KWSYS_GLOB_SUPPORT_NETWORK_PATHS
  41. #endif
  42. //----------------------------------------------------------------------------
  43. class GlobInternals
  44. {
  45. public:
  46. kwsys_stl::vector<kwsys_stl::string> Files;
  47. kwsys_stl::vector<kwsys::RegularExpression> Expressions;
  48. };
  49. //----------------------------------------------------------------------------
  50. Glob::Glob()
  51. {
  52. this->Internals = new GlobInternals;
  53. this->Recurse = false;
  54. this->Relative = "";
  55. this->RecurseThroughSymlinks = true;
  56. // RecurseThroughSymlinks is true by default for backwards compatibility,
  57. // not because it's a good idea...
  58. this->FollowedSymlinkCount = 0;
  59. }
  60. //----------------------------------------------------------------------------
  61. Glob::~Glob()
  62. {
  63. delete this->Internals;
  64. }
  65. //----------------------------------------------------------------------------
  66. kwsys_stl::vector<kwsys_stl::string>& Glob::GetFiles()
  67. {
  68. return this->Internals->Files;
  69. }
  70. //----------------------------------------------------------------------------
  71. kwsys_stl::string Glob::PatternToRegex(const kwsys_stl::string& pattern,
  72. bool require_whole_string,
  73. bool preserve_case)
  74. {
  75. // Incrementally build the regular expression from the pattern.
  76. kwsys_stl::string regex = require_whole_string? "^" : "";
  77. kwsys_stl::string::const_iterator pattern_first = pattern.begin();
  78. kwsys_stl::string::const_iterator pattern_last = pattern.end();
  79. for(kwsys_stl::string::const_iterator i = pattern_first;
  80. i != pattern_last; ++i)
  81. {
  82. int c = *i;
  83. if(c == '*')
  84. {
  85. // A '*' (not between brackets) matches any string.
  86. // We modify this to not match slashes since the orignal glob
  87. // pattern documentation was meant for matching file name
  88. // components separated by slashes.
  89. regex += "[^/]*";
  90. }
  91. else if(c == '?')
  92. {
  93. // A '?' (not between brackets) matches any single character.
  94. // We modify this to not match slashes since the orignal glob
  95. // pattern documentation was meant for matching file name
  96. // components separated by slashes.
  97. regex += "[^/]";
  98. }
  99. else if(c == '[')
  100. {
  101. // Parse out the bracket expression. It begins just after the
  102. // opening character.
  103. kwsys_stl::string::const_iterator bracket_first = i+1;
  104. kwsys_stl::string::const_iterator bracket_last = bracket_first;
  105. // The first character may be complementation '!' or '^'.
  106. if(bracket_last != pattern_last &&
  107. (*bracket_last == '!' || *bracket_last == '^'))
  108. {
  109. ++bracket_last;
  110. }
  111. // If the next character is a ']' it is included in the brackets
  112. // because the bracket string may not be empty.
  113. if(bracket_last != pattern_last && *bracket_last == ']')
  114. {
  115. ++bracket_last;
  116. }
  117. // Search for the closing ']'.
  118. while(bracket_last != pattern_last && *bracket_last != ']')
  119. {
  120. ++bracket_last;
  121. }
  122. // Check whether we have a complete bracket string.
  123. if(bracket_last == pattern_last)
  124. {
  125. // The bracket string did not end, so it was opened simply by
  126. // a '[' that is supposed to be matched literally.
  127. regex += "\\[";
  128. }
  129. else
  130. {
  131. // Convert the bracket string to its regex equivalent.
  132. kwsys_stl::string::const_iterator k = bracket_first;
  133. // Open the regex block.
  134. regex += "[";
  135. // A regex range complement uses '^' instead of '!'.
  136. if(k != bracket_last && *k == '!')
  137. {
  138. regex += "^";
  139. ++k;
  140. }
  141. // Convert the remaining characters.
  142. for(; k != bracket_last; ++k)
  143. {
  144. // Backslashes must be escaped.
  145. if(*k == '\\')
  146. {
  147. regex += "\\";
  148. }
  149. // Store this character.
  150. regex += *k;
  151. }
  152. // Close the regex block.
  153. regex += "]";
  154. // Jump to the end of the bracket string.
  155. i = bracket_last;
  156. }
  157. }
  158. else
  159. {
  160. // A single character matches itself.
  161. int ch = c;
  162. if(!(('a' <= ch && ch <= 'z') ||
  163. ('A' <= ch && ch <= 'Z') ||
  164. ('0' <= ch && ch <= '9')))
  165. {
  166. // Escape the non-alphanumeric character.
  167. regex += "\\";
  168. }
  169. #if defined(KWSYS_GLOB_CASE_INDEPENDENT)
  170. else
  171. {
  172. // On case-insensitive systems file names are converted to lower
  173. // case before matching.
  174. if(!preserve_case)
  175. {
  176. ch = tolower(ch);
  177. }
  178. }
  179. #endif
  180. (void)preserve_case;
  181. // Store the character.
  182. regex.append(1, static_cast<char>(ch));
  183. }
  184. }
  185. if(require_whole_string)
  186. {
  187. regex += "$";
  188. }
  189. return regex;
  190. }
  191. //----------------------------------------------------------------------------
  192. void Glob::RecurseDirectory(kwsys_stl::string::size_type start,
  193. const kwsys_stl::string& dir, bool dir_only)
  194. {
  195. kwsys::Directory d;
  196. if ( !d.Load(dir.c_str()) )
  197. {
  198. return;
  199. }
  200. unsigned long cc;
  201. kwsys_stl::string fullname;
  202. kwsys_stl::string realname;
  203. kwsys_stl::string fname;
  204. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  205. {
  206. fname = d.GetFile(cc);
  207. if ( strcmp(fname.c_str(), ".") == 0 ||
  208. strcmp(fname.c_str(), "..") == 0 )
  209. {
  210. continue;
  211. }
  212. if ( start == 0 )
  213. {
  214. realname = dir + fname;
  215. }
  216. else
  217. {
  218. realname = dir + "/" + fname;
  219. }
  220. #if defined( KWSYS_GLOB_CASE_INDEPENDENT )
  221. // On Windows and apple, no difference between lower and upper case
  222. fname = kwsys::SystemTools::LowerCase(fname);
  223. #endif
  224. if ( start == 0 )
  225. {
  226. fullname = dir + fname;
  227. }
  228. else
  229. {
  230. fullname = dir + "/" + fname;
  231. }
  232. if ( !dir_only || !kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
  233. {
  234. if ( (this->Internals->Expressions.size() > 0) &&
  235. this->Internals->Expressions[
  236. this->Internals->Expressions.size()-1].find(fname.c_str()) )
  237. {
  238. this->AddFile(this->Internals->Files, realname.c_str());
  239. }
  240. }
  241. if ( kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
  242. {
  243. bool isSymLink = kwsys::SystemTools::FileIsSymlink(realname.c_str());
  244. if (!isSymLink || this->RecurseThroughSymlinks)
  245. {
  246. if (isSymLink)
  247. {
  248. ++this->FollowedSymlinkCount;
  249. }
  250. this->RecurseDirectory(start+1, realname, dir_only);
  251. }
  252. }
  253. }
  254. }
  255. //----------------------------------------------------------------------------
  256. void Glob::ProcessDirectory(kwsys_stl::string::size_type start,
  257. const kwsys_stl::string& dir, bool dir_only)
  258. {
  259. //kwsys_ios::cout << "ProcessDirectory: " << dir << kwsys_ios::endl;
  260. bool last = ( start == this->Internals->Expressions.size()-1 );
  261. if ( last && this->Recurse )
  262. {
  263. this->RecurseDirectory(start, dir, dir_only);
  264. return;
  265. }
  266. if ( start >= this->Internals->Expressions.size() )
  267. {
  268. return;
  269. }
  270. kwsys::Directory d;
  271. if ( !d.Load(dir.c_str()) )
  272. {
  273. return;
  274. }
  275. unsigned long cc;
  276. kwsys_stl::string fullname;
  277. kwsys_stl::string realname;
  278. kwsys_stl::string fname;
  279. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  280. {
  281. fname = d.GetFile(cc);
  282. if ( strcmp(fname.c_str(), ".") == 0 ||
  283. strcmp(fname.c_str(), "..") == 0 )
  284. {
  285. continue;
  286. }
  287. if ( start == 0 )
  288. {
  289. realname = dir + fname;
  290. }
  291. else
  292. {
  293. realname = dir + "/" + fname;
  294. }
  295. #if defined(KWSYS_GLOB_CASE_INDEPENDENT)
  296. // On case-insensitive file systems convert to lower case for matching.
  297. fname = kwsys::SystemTools::LowerCase(fname);
  298. #endif
  299. if ( start == 0 )
  300. {
  301. fullname = dir + fname;
  302. }
  303. else
  304. {
  305. fullname = dir + "/" + fname;
  306. }
  307. //kwsys_ios::cout << "Look at file: " << fname << kwsys_ios::endl;
  308. //kwsys_ios::cout << "Match: "
  309. // << this->Internals->TextExpressions[start].c_str() << kwsys_ios::endl;
  310. //kwsys_ios::cout << "Full name: " << fullname << kwsys_ios::endl;
  311. if ( (!dir_only || !last) &&
  312. !kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
  313. {
  314. continue;
  315. }
  316. if ( this->Internals->Expressions[start].find(fname.c_str()) )
  317. {
  318. if ( last )
  319. {
  320. this->AddFile(this->Internals->Files, realname.c_str());
  321. }
  322. else
  323. {
  324. this->ProcessDirectory(start+1, realname + "/", dir_only);
  325. }
  326. }
  327. }
  328. }
  329. //----------------------------------------------------------------------------
  330. bool Glob::FindFiles(const kwsys_stl::string& inexpr)
  331. {
  332. kwsys_stl::string cexpr;
  333. kwsys_stl::string::size_type cc;
  334. kwsys_stl::string expr = inexpr;
  335. this->Internals->Expressions.clear();
  336. this->Internals->Files.clear();
  337. if ( !kwsys::SystemTools::FileIsFullPath(expr.c_str()) )
  338. {
  339. expr = kwsys::SystemTools::GetCurrentWorkingDirectory();
  340. expr += "/" + inexpr;
  341. }
  342. kwsys_stl::string fexpr = expr;
  343. kwsys_stl::string::size_type skip = 0;
  344. kwsys_stl::string::size_type last_slash = 0;
  345. for ( cc = 0; cc < expr.size(); cc ++ )
  346. {
  347. if ( cc > 0 && expr[cc] == '/' && expr[cc-1] != '\\' )
  348. {
  349. last_slash = cc;
  350. }
  351. if ( cc > 0 &&
  352. (expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
  353. expr[cc-1] != '\\' )
  354. {
  355. break;
  356. }
  357. }
  358. if ( last_slash > 0 )
  359. {
  360. //kwsys_ios::cout << "I can skip: " << fexpr.substr(0, last_slash)
  361. //<< kwsys_ios::endl;
  362. skip = last_slash;
  363. }
  364. if ( skip == 0 )
  365. {
  366. #if defined( KWSYS_GLOB_SUPPORT_NETWORK_PATHS )
  367. // Handle network paths
  368. if ( expr[0] == '/' && expr[1] == '/' )
  369. {
  370. int cnt = 0;
  371. for ( cc = 2; cc < expr.size(); cc ++ )
  372. {
  373. if ( expr[cc] == '/' )
  374. {
  375. cnt ++;
  376. if ( cnt == 2 )
  377. {
  378. break;
  379. }
  380. }
  381. }
  382. skip = int(cc + 1);
  383. }
  384. else
  385. #endif
  386. // Handle drive letters on Windows
  387. if ( expr[1] == ':' && expr[0] != '/' )
  388. {
  389. skip = 2;
  390. }
  391. }
  392. if ( skip > 0 )
  393. {
  394. expr = expr.substr(skip);
  395. }
  396. cexpr = "";
  397. for ( cc = 0; cc < expr.size(); cc ++ )
  398. {
  399. int ch = expr[cc];
  400. if ( ch == '/' )
  401. {
  402. if ( cexpr.size() > 0 )
  403. {
  404. this->AddExpression(cexpr.c_str());
  405. }
  406. cexpr = "";
  407. }
  408. else
  409. {
  410. cexpr.append(1, static_cast<char>(ch));
  411. }
  412. }
  413. if ( cexpr.size() > 0 )
  414. {
  415. this->AddExpression(cexpr.c_str());
  416. }
  417. // Handle network paths
  418. if ( skip > 0 )
  419. {
  420. this->ProcessDirectory(0, fexpr.substr(0, skip) + "/",
  421. true);
  422. }
  423. else
  424. {
  425. this->ProcessDirectory(0, "/", true);
  426. }
  427. return true;
  428. }
  429. //----------------------------------------------------------------------------
  430. void Glob::AddExpression(const char* expr)
  431. {
  432. this->Internals->Expressions.push_back(
  433. kwsys::RegularExpression(
  434. this->PatternToRegex(expr).c_str()));
  435. }
  436. //----------------------------------------------------------------------------
  437. void Glob::SetRelative(const char* dir)
  438. {
  439. if ( !dir )
  440. {
  441. this->Relative = "";
  442. return;
  443. }
  444. this->Relative = dir;
  445. }
  446. //----------------------------------------------------------------------------
  447. const char* Glob::GetRelative()
  448. {
  449. if ( this->Relative.empty() )
  450. {
  451. return 0;
  452. }
  453. return this->Relative.c_str();
  454. }
  455. //----------------------------------------------------------------------------
  456. void Glob::AddFile(kwsys_stl::vector<kwsys_stl::string>& files, const char* file)
  457. {
  458. if ( !this->Relative.empty() )
  459. {
  460. files.push_back(kwsys::SystemTools::RelativePath(this->Relative.c_str(), file));
  461. }
  462. else
  463. {
  464. files.push_back(file);
  465. }
  466. }
  467. } // namespace KWSYS_NAMESPACE