Glob.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // Glob.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Glob.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: Glob
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Glob.h"
  16. #include "Poco/Path.h"
  17. #include "Poco/Exception.h"
  18. #include "Poco/DirectoryIterator.h"
  19. #include "Poco/File.h"
  20. #include "Poco/UTF8Encoding.h"
  21. #include "Poco/Unicode.h"
  22. namespace Poco {
  23. Glob::Glob(const std::string& pattern, int options)
  24. : _pattern(pattern), _options(options)
  25. {
  26. }
  27. Glob::~Glob()
  28. {
  29. }
  30. bool Glob::match(const std::string& subject)
  31. {
  32. UTF8Encoding utf8;
  33. TextIterator itp(_pattern, utf8);
  34. TextIterator endp(_pattern);
  35. TextIterator its(subject, utf8);
  36. TextIterator ends(subject);
  37. if ((_options & GLOB_DOT_SPECIAL) && its != ends && *its == '.' && (*itp == '?' || *itp == '*'))
  38. return false;
  39. else
  40. return match(itp, endp, its, ends);
  41. }
  42. void Glob::glob(const std::string& pathPattern, std::set<std::string>& files, int options)
  43. {
  44. glob(Path(Path::expand(pathPattern), Path::PATH_GUESS), files, options);
  45. }
  46. void Glob::glob(const char* pathPattern, std::set<std::string>& files, int options)
  47. {
  48. glob(Path(Path::expand(pathPattern), Path::PATH_GUESS), files, options);
  49. }
  50. void Glob::glob(const Path& pathPattern, std::set<std::string>& files, int options)
  51. {
  52. Path pattern(pathPattern);
  53. pattern.makeDirectory(); // to simplify pattern handling later on
  54. Path base(pattern);
  55. Path absBase(base);
  56. absBase.makeAbsolute();
  57. // In case of UNC paths we must not pop the topmost directory
  58. // (which must not contain wildcards), otherwise collect() will fail
  59. // as one cannot create a DirectoryIterator with only a node name ("\\srv\").
  60. int minDepth = base.getNode().empty() ? 0 : 1;
  61. while (base.depth() > minDepth && base[base.depth() - 1] != "..")
  62. {
  63. base.popDirectory();
  64. absBase.popDirectory();
  65. }
  66. if (pathPattern.isDirectory())
  67. options |= GLOB_DIRS_ONLY;
  68. collect(pattern, absBase, base, pathPattern[base.depth()], files, options);
  69. }
  70. void Glob::glob(const Path& pathPattern, const Path& basePath, std::set<std::string>& files, int options)
  71. {
  72. Path pattern(pathPattern);
  73. pattern.makeDirectory(); // to simplify pattern handling later on
  74. Path absBase(basePath);
  75. absBase.makeAbsolute();
  76. if (pathPattern.isDirectory())
  77. options |= GLOB_DIRS_ONLY;
  78. collect(pattern, absBase, basePath, pathPattern[basePath.depth()], files, options);
  79. }
  80. bool Glob::match(TextIterator& itp, const TextIterator& endp, TextIterator& its, const TextIterator& ends)
  81. {
  82. while (itp != endp)
  83. {
  84. if (its == ends)
  85. {
  86. while (itp != endp && *itp == '*') ++itp;
  87. break;
  88. }
  89. switch (*itp)
  90. {
  91. case '?':
  92. ++itp; ++its;
  93. break;
  94. case '*':
  95. if (++itp != endp)
  96. {
  97. while (its != ends && !matchAfterAsterisk(itp, endp, its, ends)) ++its;
  98. return its != ends;
  99. }
  100. return true;
  101. case '[':
  102. if (++itp != endp)
  103. {
  104. bool invert = *itp == '!';
  105. if (invert) ++itp;
  106. if (itp != endp)
  107. {
  108. bool mtch = matchSet(itp, endp, *its++);
  109. if ((invert && mtch) || (!invert && !mtch)) return false;
  110. break;
  111. }
  112. }
  113. throw SyntaxException("bad range syntax in glob pattern");
  114. case '\\':
  115. if (++itp == endp) throw SyntaxException("backslash must be followed by character in glob pattern");
  116. // fallthrough
  117. default:
  118. if (_options & GLOB_CASELESS)
  119. {
  120. if (Unicode::toLower(*itp) != Unicode::toLower(*its)) return false;
  121. }
  122. else
  123. {
  124. if (*itp != *its) return false;
  125. }
  126. ++itp; ++its;
  127. }
  128. }
  129. return itp == endp && its == ends;
  130. }
  131. bool Glob::matchAfterAsterisk(TextIterator itp, const TextIterator& endp, TextIterator its, const TextIterator& ends)
  132. {
  133. return match(itp, endp, its, ends);
  134. }
  135. bool Glob::matchSet(TextIterator& itp, const TextIterator& endp, int c)
  136. {
  137. if (_options & GLOB_CASELESS)
  138. c = Unicode::toLower(c);
  139. while (itp != endp)
  140. {
  141. switch (*itp)
  142. {
  143. case ']':
  144. ++itp;
  145. return false;
  146. case '\\':
  147. if (++itp == endp) throw SyntaxException("backslash must be followed by character in glob pattern");
  148. }
  149. int first = *itp;
  150. int last = first;
  151. if (++itp != endp && *itp == '-')
  152. {
  153. if (++itp != endp)
  154. last = *itp++;
  155. else
  156. throw SyntaxException("bad range syntax in glob pattern");
  157. }
  158. if (_options & GLOB_CASELESS)
  159. {
  160. first = Unicode::toLower(first);
  161. last = Unicode::toLower(last);
  162. }
  163. if (first <= c && c <= last)
  164. {
  165. while (itp != endp)
  166. {
  167. switch (*itp)
  168. {
  169. case ']':
  170. ++itp;
  171. return true;
  172. case '\\':
  173. if (++itp == endp) break;
  174. default:
  175. ++itp;
  176. }
  177. }
  178. throw SyntaxException("range must be terminated by closing bracket in glob pattern");
  179. }
  180. }
  181. return false;
  182. }
  183. void Glob::collect(const Path& pathPattern, const Path& base, const Path& current, const std::string& pattern, std::set<std::string>& files, int options)
  184. {
  185. try
  186. {
  187. std::string pp = pathPattern.toString();
  188. std::string basep = base.toString();
  189. std::string curp = current.toString();
  190. Glob g(pattern, options);
  191. DirectoryIterator it(base);
  192. DirectoryIterator end;
  193. while (it != end)
  194. {
  195. const std::string& name = it.name();
  196. if (g.match(name))
  197. {
  198. Path p(current);
  199. if (p.depth() < pathPattern.depth() - 1)
  200. {
  201. p.pushDirectory(name);
  202. collect(pathPattern, it.path(), p, pathPattern[p.depth()], files, options);
  203. }
  204. else
  205. {
  206. p.setFileName(name);
  207. if (isDirectory(p, (options & GLOB_FOLLOW_SYMLINKS) != 0))
  208. {
  209. p.makeDirectory();
  210. files.insert(p.toString());
  211. }
  212. else if (!(options & GLOB_DIRS_ONLY))
  213. {
  214. files.insert(p.toString());
  215. }
  216. }
  217. }
  218. ++it;
  219. }
  220. }
  221. catch (Exception&)
  222. {
  223. }
  224. }
  225. bool Glob::isDirectory(const Path& path, bool followSymlink)
  226. {
  227. File f(path);
  228. bool isDir = false;
  229. try
  230. {
  231. isDir = f.isDirectory();
  232. }
  233. catch (Poco::Exception&)
  234. {
  235. return false;
  236. }
  237. if (isDir)
  238. {
  239. return true;
  240. }
  241. else if (followSymlink && f.isLink())
  242. {
  243. try
  244. {
  245. // Test if link resolves to a directory.
  246. DirectoryIterator it(f);
  247. return true;
  248. }
  249. catch (Exception&)
  250. {
  251. }
  252. }
  253. return false;
  254. }
  255. } // namespace Poco