cmGlob.cxx 8.1 KB

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