cmGlob.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. class cmGlobInternal
  19. {
  20. public:
  21. std::vector<std::string> Files;
  22. std::vector<cmsys::RegularExpression> Expressions;
  23. };
  24. cmGlob::cmGlob()
  25. {
  26. m_Internals = new cmGlobInternal;
  27. }
  28. cmGlob::~cmGlob()
  29. {
  30. delete m_Internals;
  31. }
  32. void cmGlob::Escape(int ch, char* buffer)
  33. {
  34. if (! (
  35. 'a' <= ch && ch <= 'z' ||
  36. 'A' <= ch && ch <= 'Z' ||
  37. '0' <= ch && ch <= '9') )
  38. {
  39. sprintf(buffer, "\\%c", ch);
  40. }
  41. else
  42. {
  43. sprintf(buffer, "%c", ch);
  44. }
  45. }
  46. std::vector<std::string>& cmGlob::GetFiles()
  47. {
  48. return m_Internals->Files;
  49. }
  50. std::string cmGlob::ConvertExpression(const std::string& expr)
  51. {
  52. std::string::size_type i = 0;
  53. std::string::size_type n = expr.size();
  54. std::string res = "^";
  55. std::string stuff = "";
  56. while ( i < n )
  57. {
  58. int c = expr[i];
  59. i = i+1;
  60. if ( c == '*' )
  61. {
  62. res = res + ".*";
  63. }
  64. else if ( c == '?' )
  65. {
  66. res = res + ".";
  67. }
  68. else if ( c == '[' )
  69. {
  70. std::string::size_type j = i;
  71. if ( j < n && expr[j] == '!' )
  72. {
  73. j = j+1;
  74. }
  75. if ( j < n && expr[j] == ']' )
  76. {
  77. j = j+1;
  78. }
  79. while ( j < n && expr[j] != ']' )
  80. {
  81. j = j+1;
  82. }
  83. if ( j >= n )
  84. {
  85. res = res + "\\[";
  86. }
  87. else
  88. {
  89. stuff = "";
  90. std::string::size_type cc;
  91. for ( cc = i; cc < j; cc ++ )
  92. {
  93. if ( expr[cc] == '\\' )
  94. {
  95. stuff += "\\\\";
  96. }
  97. else
  98. {
  99. stuff += expr[cc];
  100. }
  101. }
  102. i = j+1;
  103. if ( stuff[0] == '!' )
  104. {
  105. stuff = '^' + stuff.substr(1);
  106. }
  107. else if ( stuff[0] == '^' )
  108. {
  109. stuff = '\\' + stuff;
  110. }
  111. res = res + "[" + stuff + "]";
  112. }
  113. }
  114. else
  115. {
  116. char buffer[100];
  117. buffer[0] = 0;
  118. this->Escape(c, buffer);
  119. res = res + buffer;
  120. }
  121. }
  122. return res + "$";
  123. }
  124. void cmGlob::ProcessDirectory(std::string::size_type start,
  125. const std::string& dir, bool dir_only)
  126. {
  127. cmsys::Directory d;
  128. if ( !d.Load(dir.c_str()) )
  129. {
  130. //std::cout << "Cannot open directory: " << dir.c_str() << std::endl;
  131. return;
  132. }
  133. unsigned long cc;
  134. std::string fullname;
  135. bool last = ( start == m_Internals->Expressions.size()-1 );
  136. //std::cout << "Last: " << last << " Dironly: " << dir_only << std::endl;
  137. for ( cc = 0; cc < d.GetNumberOfFiles(); cc ++ )
  138. {
  139. if ( strcmp(d.GetFile(cc), ".") == 0 ||
  140. strcmp(d.GetFile(cc), "..") == 0 )
  141. {
  142. continue;
  143. }
  144. if ( start == 0 )
  145. {
  146. fullname = dir + d.GetFile(cc);
  147. }
  148. else
  149. {
  150. fullname = dir + "/" + d.GetFile(cc);
  151. }
  152. if ( (!dir_only || !last) && !cmsys::SystemTools::FileIsDirectory(fullname.c_str()) )
  153. {
  154. //std::cout << " Ignore: " << fullname.c_str() << std::endl;
  155. continue;
  156. }
  157. if ( m_Internals->Expressions[start].find(d.GetFile(cc)) )
  158. {
  159. //std::cout << " Matches: " << fullname.c_str() << std::endl;
  160. if ( last )
  161. {
  162. //std::cout << "--- find file: " << fullname.c_str() << "---" << std::endl;
  163. m_Internals->Files.push_back(fullname);
  164. }
  165. else
  166. {
  167. this->ProcessDirectory(start+1, fullname, dir_only);
  168. }
  169. }
  170. else
  171. {
  172. //std::cout << " Not Matches: " << fullname.c_str() << std::endl;
  173. }
  174. }
  175. }
  176. bool cmGlob::FindFiles(const std::string& inexpr)
  177. {
  178. std::string cexpr;
  179. std::string::size_type cc;
  180. std::string expr = inexpr;
  181. m_Internals->Expressions.empty();
  182. m_Internals->Files.empty();
  183. if ( !cmsys::SystemTools::FileIsFullPath(expr.c_str()) )
  184. {
  185. expr = cmsys::SystemTools::GetCurrentWorkingDirectory();
  186. expr += "/" + inexpr;
  187. }
  188. std::cout << "Expr: " << expr << std::endl;
  189. for ( cc = 0; cc < expr.size(); cc ++ )
  190. {
  191. int ch = expr[cc];
  192. if ( ch == '/' )
  193. {
  194. if ( cexpr.size() > 0 )
  195. {
  196. this->AddExpression(cexpr.c_str());
  197. }
  198. cexpr = "";
  199. }
  200. else
  201. {
  202. cexpr.append(1, (char)ch);
  203. }
  204. }
  205. if ( cexpr.size() > 0 )
  206. {
  207. this->AddExpression(cexpr.c_str());
  208. }
  209. this->ProcessDirectory(0, "/", true);
  210. return true;
  211. }
  212. void cmGlob::AddExpression(const char* expr)
  213. {
  214. m_Internals->Expressions.push_back(
  215. cmsys::RegularExpression(
  216. this->ConvertExpression(expr).c_str()));
  217. }