globPattern.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Filename: globPattern.cxx
  2. // Created by: drose (30May00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "globPattern.h"
  15. ////////////////////////////////////////////////////////////////////
  16. // Function: GlobPattern::has_glob_characters
  17. // Access: Public
  18. // Description: Returns true if the pattern includes any special
  19. // globbing characters, or false if it is just a literal
  20. // string.
  21. ////////////////////////////////////////////////////////////////////
  22. bool GlobPattern::
  23. has_glob_characters() const {
  24. string::const_iterator pi;
  25. pi = _pattern.begin();
  26. while (pi != _pattern.end()) {
  27. switch (*pi) {
  28. case '*':
  29. case '?':
  30. case '[':
  31. return true;
  32. case '\\':
  33. ++pi;
  34. if (pi == _pattern.end()) {
  35. return false;
  36. }
  37. }
  38. ++pi;
  39. }
  40. return false;
  41. }
  42. ////////////////////////////////////////////////////////////////////
  43. // Function: GlobPattern::match_files
  44. // Access: Public
  45. // Description: Treats the GlobPattern as a filename pattern, and
  46. // returns a list of any actual files that match the
  47. // pattern. This is the behavior of the standard Posix
  48. // glob() function. Any part of the filename may
  49. // contain glob characters, including intermediate
  50. // directory names.
  51. //
  52. // If cwd is specified, it is the directory that
  53. // relative filenames are taken to be relative to;
  54. // otherwise, the actual current working directory is
  55. // assumed.
  56. //
  57. // The return value is the number of files matched,
  58. // which are added to the results vector.
  59. ////////////////////////////////////////////////////////////////////
  60. int GlobPattern::
  61. match_files(vector_string &results, const Filename &cwd) {
  62. string prefix, pattern, suffix;
  63. string source = _pattern;
  64. if (!source.empty() && source[0] == '/') {
  65. // If the first character is a slash, that becomes the prefix.
  66. prefix = "/";
  67. source = source.substr(1);
  68. }
  69. size_t slash = source.find('/');
  70. if (slash == string::npos) {
  71. pattern = source;
  72. } else {
  73. pattern = source.substr(0, slash);
  74. suffix = source.substr(slash + 1);
  75. }
  76. GlobPattern glob(pattern);
  77. return glob.r_match_files(prefix, suffix, results, cwd);
  78. }
  79. ////////////////////////////////////////////////////////////////////
  80. // Function: GlobPattern::r_match_files
  81. // Access: Private
  82. // Description: The recursive implementation of match_files().
  83. ////////////////////////////////////////////////////////////////////
  84. int GlobPattern::
  85. r_match_files(const Filename &prefix, const string &suffix,
  86. vector_string &results, const Filename &cwd) {
  87. string next_pattern, next_suffix;
  88. size_t slash = suffix.find('/');
  89. if (slash == string::npos) {
  90. next_pattern = suffix;
  91. } else {
  92. next_pattern = suffix.substr(0, slash);
  93. next_suffix = suffix.substr(slash + 1);
  94. }
  95. Filename parent_dir;
  96. if (prefix.is_local() && !cwd.empty()) {
  97. parent_dir = Filename(cwd, prefix);
  98. } else {
  99. parent_dir = prefix;
  100. }
  101. GlobPattern next_glob(next_pattern);
  102. if (!has_glob_characters()) {
  103. // If there are no special characters in the pattern, it's a
  104. // literal match.
  105. if (suffix.empty()) {
  106. // Time to stop.
  107. Filename single_filename(parent_dir, _pattern);
  108. if (single_filename.exists()) {
  109. results.push_back(Filename(prefix, _pattern));
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. return next_glob.r_match_files(Filename(prefix, _pattern),
  115. next_suffix, results, cwd);
  116. }
  117. // If there *are* special glob characters, we must attempt to
  118. // match the pattern against the files in this directory.
  119. vector_string dir_files;
  120. if (!parent_dir.scan_directory(dir_files)) {
  121. // Not a directory, or unable to read directory; stop here.
  122. return 0;
  123. }
  124. // Now go through each file in the directory looking for one that
  125. // matches the pattern.
  126. int num_matched = 0;
  127. vector_string::const_iterator fi;
  128. for (fi = dir_files.begin(); fi != dir_files.end(); ++fi) {
  129. const string &local_file = (*fi);
  130. if (_pattern[0] == '.' || (local_file.empty() || local_file[0] != '.')) {
  131. if (matches(local_file)) {
  132. // We have a match; continue.
  133. if (suffix.empty()) {
  134. results.push_back(Filename(prefix, local_file));
  135. num_matched++;
  136. } else {
  137. num_matched += next_glob.r_match_files(Filename(prefix, local_file),
  138. next_suffix, results, cwd);
  139. }
  140. }
  141. }
  142. }
  143. return num_matched;
  144. }
  145. ////////////////////////////////////////////////////////////////////
  146. // Function: GlobPattern::matches_substr
  147. // Access: Private
  148. // Description: The recursive implementation of matches(). This
  149. // returns true if the pattern substring [pi, pend)
  150. // matches the candidate substring [ci, cend), false
  151. // otherwise.
  152. ////////////////////////////////////////////////////////////////////
  153. bool GlobPattern::
  154. matches_substr(string::const_iterator pi, string::const_iterator pend,
  155. string::const_iterator ci, string::const_iterator cend) const {
  156. // If we run out of pattern or candidate string, it's a match only
  157. // if they both ran out at the same time.
  158. if (pi == pend || ci == cend) {
  159. // A special exception: we allow ci to reach the end before pi,
  160. // only if pi is one character before the end and that last
  161. // character is '*'.
  162. if ((ci == cend) && (pi + 1 == pend) && (*pi) == '*') {
  163. return true;
  164. }
  165. return (pi == pend && ci == cend);
  166. }
  167. switch (*pi) {
  168. case '*':
  169. // A '*' in the pattern string means to match any sequence of zero
  170. // or more characters in the candidate string. This means we have
  171. // to recurse twice: either consume one character of the candidate
  172. // string and continue to try matching the *, or stop trying to
  173. // match the * here.
  174. return
  175. matches_substr(pi, pend, ci + 1, cend) ||
  176. matches_substr(pi + 1, pend, ci, cend);
  177. case '?':
  178. // A '?' in the pattern string means to match exactly one
  179. // character in the candidate string. That's easy.
  180. return matches_substr(pi + 1, pend, ci + 1, cend);
  181. case '[':
  182. // An open square bracket begins a set.
  183. ++pi;
  184. if ((*pi) == '!') {
  185. ++pi;
  186. if (matches_set(pi, pend, *ci)) {
  187. return false;
  188. }
  189. } else {
  190. if (!matches_set(pi, pend, *ci)) {
  191. return false;
  192. }
  193. }
  194. if (pi == pend) {
  195. // Oops, there wasn't a closing square bracket.
  196. return false;
  197. }
  198. return matches_substr(pi + 1, pend, ci + 1, cend);
  199. case '\\':
  200. // A backslash escapes the next special character.
  201. ++pi;
  202. if (pi == pend) {
  203. return false;
  204. }
  205. // fall through.
  206. default:
  207. // Anything else means to match exactly that.
  208. if ((*pi) != (*ci)) {
  209. return false;
  210. }
  211. return matches_substr(pi + 1, pend, ci + 1, cend);
  212. }
  213. }
  214. ////////////////////////////////////////////////////////////////////
  215. // Function: GlobPattern::matches_set
  216. // Access: Private
  217. // Description: Called when an unescaped open square bracked is
  218. // scanned, this is called with pi positioned after the
  219. // opening square bracket, scans the set sequence,
  220. // leaving pi positioned on the closing square bracket,
  221. // and returns true if the indicated character matches
  222. // the set of characters indicated, false otherwise.
  223. ////////////////////////////////////////////////////////////////////
  224. bool GlobPattern::
  225. matches_set(string::const_iterator &pi, string::const_iterator pend,
  226. char ch) const {
  227. bool matched = false;
  228. while (pi != pend && (*pi) != ']') {
  229. if ((*pi) == '\\') {
  230. // Backslash escapes the next character.
  231. ++pi;
  232. if (pi == pend) {
  233. return false;
  234. }
  235. }
  236. if (ch == (*pi)) {
  237. matched = true;
  238. }
  239. // Maybe it's an a-z style range?
  240. char start = (*pi);
  241. ++pi;
  242. if (pi != pend && (*pi) == '-') {
  243. ++pi;
  244. if (pi != pend && (*pi) != ']') {
  245. // Yes, we have a range: start-end.
  246. if ((*pi) == '\\') {
  247. // Backslash escapes.
  248. ++pi;
  249. if (pi == pend) {
  250. return false;
  251. }
  252. }
  253. char end = (*pi);
  254. ++pi;
  255. if (ch >= start && ch <= end) {
  256. matched = true;
  257. }
  258. } else {
  259. // This was a - at the end of the string.
  260. if (ch == '-') {
  261. matched = true;
  262. }
  263. }
  264. }
  265. }
  266. return matched;
  267. }