cmGlob.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. m_Internals = new cmGlobInternal;
  36. m_Recurse = false;
  37. }
  38. cmGlob::~cmGlob()
  39. {
  40. delete m_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 m_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(const std::string& dir, bool dir_only)
  140. {
  141. cmsys::Directory d;
  142. if ( !d.Load(dir.c_str()) )
  143. {
  144. return;
  145. }
  146. unsigned long cc;
  147. std::string fullname;
  148. std::string realname;
  149. std::string fname;
  150. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  151. {
  152. fname = d.GetFile(cc);
  153. if ( strcmp(fname.c_str(), ".") == 0 ||
  154. strcmp(fname.c_str(), "..") == 0 )
  155. {
  156. continue;
  157. }
  158. realname = dir + "/" + fname;
  159. #if defined( CM_GLOB_CASE_INDEPENDENT )
  160. // On Windows and apple, no difference between lower and upper case
  161. fname = cmsys::SystemTools::LowerCase(fname);
  162. #endif
  163. fullname = dir + "/" + fname;
  164. if ( !dir_only || !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
  165. {
  166. if ( m_Internals->Expressions[m_Internals->Expressions.size()-1].find(fname.c_str()) )
  167. {
  168. m_Internals->Files.push_back(fullname);
  169. }
  170. }
  171. if ( cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
  172. {
  173. this->RecurseDirectory(fullname, dir_only);
  174. }
  175. }
  176. }
  177. void cmGlob::ProcessDirectory(std::string::size_type start,
  178. const std::string& dir, bool dir_only)
  179. {
  180. //std::cout << "ProcessDirectory: " << dir << std::endl;
  181. bool last = ( start == m_Internals->Expressions.size()-1 );
  182. if ( last && m_Recurse )
  183. {
  184. this->RecurseDirectory(dir, dir_only);
  185. return;
  186. }
  187. cmsys::Directory d;
  188. if ( !d.Load(dir.c_str()) )
  189. {
  190. return;
  191. }
  192. unsigned long cc;
  193. std::string fullname;
  194. std::string realname;
  195. std::string fname;
  196. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  197. {
  198. fname = d.GetFile(cc);
  199. if ( strcmp(fname.c_str(), ".") == 0 ||
  200. strcmp(fname.c_str(), "..") == 0 )
  201. {
  202. continue;
  203. }
  204. if ( start == 0 )
  205. {
  206. realname = dir + fname;
  207. }
  208. else
  209. {
  210. realname = dir + "/" + fname;
  211. }
  212. #if defined( CM_GLOB_CASE_INDEPENDENT )
  213. // On Windows and apple, no difference between lower and upper case
  214. fname = cmsys::SystemTools::LowerCase(fname);
  215. #endif
  216. if ( start == 0 )
  217. {
  218. fullname = dir + fname;
  219. }
  220. else
  221. {
  222. fullname = dir + "/" + fname;
  223. }
  224. //std::cout << "Look at file: " << fname << std::endl;
  225. //std::cout << "Match: " << m_Internals->TextExpressions[start].c_str() << std::endl;
  226. //std::cout << "Full name: " << fullname << std::endl;
  227. if ( (!dir_only || !last) && !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
  228. {
  229. continue;
  230. }
  231. if ( m_Internals->Expressions[start].find(fname.c_str()) )
  232. {
  233. if ( last )
  234. {
  235. m_Internals->Files.push_back(realname);
  236. }
  237. else
  238. {
  239. this->ProcessDirectory(start+1, realname + "/", dir_only);
  240. }
  241. }
  242. }
  243. }
  244. bool cmGlob::FindFiles(const std::string& inexpr)
  245. {
  246. std::string cexpr;
  247. std::string::size_type cc;
  248. std::string expr = inexpr;
  249. m_Internals->Expressions.clear();
  250. m_Internals->Files.clear();
  251. if ( !cmsys::SystemTools::FileIsFullPath(expr.c_str()) )
  252. {
  253. expr = cmsys::SystemTools::GetCurrentWorkingDirectory();
  254. expr += "/" + inexpr;
  255. }
  256. std::string fexpr = expr;
  257. int skip = 0;
  258. int last_slash = 0;
  259. for ( cc = 0; cc < expr.size(); cc ++ )
  260. {
  261. if ( cc > 0 && expr[cc] == '/' && expr[cc-1] != '\\' )
  262. {
  263. last_slash = cc;
  264. }
  265. if ( cc > 0 &&
  266. (expr[cc] == '[' || expr[cc] == '?' || expr[cc] == '*') &&
  267. expr[cc-1] != '\\' )
  268. {
  269. break;
  270. }
  271. }
  272. if ( last_slash > 0 )
  273. {
  274. //std::cout << "I can skip: " << fexpr.substr(0, last_slash) << std::endl;
  275. skip = last_slash;
  276. }
  277. if ( skip == 0 )
  278. {
  279. #if defined( CM_GLOB_SUPPORT_NETWORK_PATHS )
  280. // Handle network paths
  281. if ( expr[0] == '/' && expr[1] == '/' )
  282. {
  283. int cnt = 0;
  284. for ( cc = 2; cc < expr.size(); cc ++ )
  285. {
  286. if ( expr[cc] == '/' )
  287. {
  288. cnt ++;
  289. if ( cnt == 2 )
  290. {
  291. break;
  292. }
  293. }
  294. }
  295. skip = cc + 1;
  296. }
  297. else
  298. #endif
  299. // Handle drive letters on Windows
  300. if ( expr[1] == ':' && expr[0] != '/' )
  301. {
  302. skip = 2;
  303. }
  304. }
  305. if ( skip > 0 )
  306. {
  307. expr = expr.substr(skip);
  308. }
  309. cexpr = "";
  310. for ( cc = 0; cc < expr.size(); cc ++ )
  311. {
  312. int ch = expr[cc];
  313. if ( ch == '/' )
  314. {
  315. if ( cexpr.size() > 0 )
  316. {
  317. this->AddExpression(cexpr.c_str());
  318. }
  319. cexpr = "";
  320. }
  321. else
  322. {
  323. cexpr.append(1, (char)ch);
  324. }
  325. }
  326. if ( cexpr.size() > 0 )
  327. {
  328. this->AddExpression(cexpr.c_str());
  329. }
  330. // Handle network paths
  331. if ( skip > 0 )
  332. {
  333. this->ProcessDirectory(0, fexpr.substr(0, skip) + "/",
  334. true);
  335. }
  336. else
  337. {
  338. this->ProcessDirectory(0, "/", true);
  339. }
  340. return true;
  341. }
  342. void cmGlob::AddExpression(const char* expr)
  343. {
  344. m_Internals->Expressions.push_back(
  345. cmsys::RegularExpression(
  346. this->ConvertExpression(expr).c_str()));
  347. m_Internals->TextExpressions.push_back(this->ConvertExpression(expr));
  348. }