cmGlob.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. };
  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. bool last = ( start == m_Internals->Expressions.size()-1 );
  179. if ( last && m_Recurse )
  180. {
  181. this->RecurseDirectory(dir, dir_only);
  182. return;
  183. }
  184. cmsys::Directory d;
  185. if ( !d.Load(dir.c_str()) )
  186. {
  187. return;
  188. }
  189. unsigned long cc;
  190. std::string fullname;
  191. std::string fname;
  192. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  193. {
  194. fname = d.GetFile(cc);
  195. if ( strcmp(fname.c_str(), ".") == 0 ||
  196. strcmp(fname.c_str(), "..") == 0 )
  197. {
  198. continue;
  199. }
  200. #if defined( CM_GLOB_CASE_INDEPENDENT )
  201. // On Windows and apple, no difference between lower and upper case
  202. fname = cmsys::SystemTools::LowerCase(fname);
  203. #endif
  204. if ( start == 0 )
  205. {
  206. fullname = dir + fname;
  207. }
  208. else
  209. {
  210. fullname = dir + "/" + fname;
  211. }
  212. if ( (!dir_only || !last) && !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
  213. {
  214. continue;
  215. }
  216. if ( m_Internals->Expressions[start].find(fname.c_str()) )
  217. {
  218. if ( last )
  219. {
  220. m_Internals->Files.push_back(fullname);
  221. }
  222. else
  223. {
  224. this->ProcessDirectory(start+1, fullname, dir_only);
  225. }
  226. }
  227. }
  228. }
  229. bool cmGlob::FindFiles(const std::string& inexpr)
  230. {
  231. std::string cexpr;
  232. std::string::size_type cc;
  233. std::string expr = inexpr;
  234. m_Internals->Expressions.empty();
  235. m_Internals->Files.empty();
  236. if ( !cmsys::SystemTools::FileIsFullPath(expr.c_str()) )
  237. {
  238. expr = cmsys::SystemTools::GetCurrentWorkingDirectory();
  239. expr += "/" + inexpr;
  240. }
  241. #if defined( CM_GLOB_SUPPORT_NETWORK_PATHS )
  242. int skip = 0;
  243. // Handle network paths
  244. if ( expr[0] == '/' && expr[1] == '/' )
  245. {
  246. int cnt = 0;
  247. for ( cc = 2; cc < expr.size(); cc ++ )
  248. {
  249. if ( expr[cc] == '/' )
  250. {
  251. cnt ++;
  252. if ( cnt == 2 )
  253. {
  254. break;
  255. }
  256. }
  257. }
  258. skip = cc + 1;
  259. expr = expr.substr(skip);
  260. }
  261. else
  262. #endif
  263. if ( expr[1] == ':' && expr[0] != '/' )
  264. {
  265. expr = expr.substr(2);
  266. }
  267. cexpr = "";
  268. for ( cc = 0; cc < expr.size(); cc ++ )
  269. {
  270. int ch = expr[cc];
  271. if ( ch == '/' )
  272. {
  273. if ( cexpr.size() > 0 )
  274. {
  275. this->AddExpression(cexpr.c_str());
  276. }
  277. cexpr = "";
  278. }
  279. else
  280. {
  281. cexpr.append(1, (char)ch);
  282. }
  283. }
  284. if ( cexpr.size() > 0 )
  285. {
  286. this->AddExpression(cexpr.c_str());
  287. }
  288. #ifdef _WIN32
  289. // Handle network paths
  290. if ( skip > 0 )
  291. {
  292. this->ProcessDirectory(0, inexpr.substr(0, skip),
  293. true);
  294. }
  295. else
  296. #endif
  297. if ( inexpr[1] == ':' && inexpr[0] != '/' )
  298. {
  299. std::string startdir = "A:/";
  300. if ( inexpr[0] >= 'a' && inexpr[0] <= 'z' ||
  301. inexpr[0] >= 'A' && inexpr[0] <= 'Z')
  302. {
  303. startdir[0] = inexpr[0];
  304. this->ProcessDirectory(0, startdir, true);
  305. }
  306. else
  307. {
  308. return false;
  309. }
  310. }
  311. else
  312. {
  313. this->ProcessDirectory(0, "/", true);
  314. }
  315. return true;
  316. }
  317. void cmGlob::AddExpression(const char* expr)
  318. {
  319. m_Internals->Expressions.push_back(
  320. cmsys::RegularExpression(
  321. this->ConvertExpression(expr).c_str()));
  322. }