cmGlob.cxx 8.1 KB

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