Glob.cxx 13 KB

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