Panda3D
 All Classes Functions Variables Enumerations
globPattern.h
1 // Filename: globPattern.h
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 
15 #ifndef GLOBPATTERN_H
16 #define GLOBPATTERN_H
17 
18 #include "dtoolbase.h"
19 #include "filename.h"
20 #include "vector_string.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Class : GlobPattern
24 // Description : This class can be used to test for string matches
25 // against standard Unix-shell filename globbing
26 // conventions. It serves as a portable standin for the
27 // Posix fnmatch() call.
28 //
29 // A GlobPattern is given a pattern string, which can
30 // contain operators like *, ?, and []. Then it can be
31 // tested against any number of candidate strings; for
32 // each candidate, it will indicate whether the string
33 // matches the pattern or not. It can be used, for
34 // example, to scan a directory for all files matching a
35 // particular pattern.
36 ////////////////////////////////////////////////////////////////////
37 class EXPCL_DTOOL GlobPattern {
38 PUBLISHED:
39  INLINE GlobPattern(const string &pattern = string());
40  INLINE GlobPattern(const GlobPattern &copy);
41  INLINE void operator = (const GlobPattern &copy);
42 
43  INLINE bool operator == (const GlobPattern &other) const;
44  INLINE bool operator != (const GlobPattern &other) const;
45  INLINE bool operator < (const GlobPattern &other) const;
46 
47  INLINE void set_pattern(const string &pattern);
48  INLINE const string &get_pattern() const;
49 
50  INLINE void set_case_sensitive(bool case_sensitive);
51  INLINE bool get_case_sensitive() const;
52 
53  INLINE void set_nomatch_chars(const string &nomatch_chars);
54  INLINE const string &get_nomatch_chars() const;
55 
56  INLINE bool matches(const string &candidate) const;
57 
58  INLINE void output(ostream &out) const;
59 
60  bool has_glob_characters() const;
61  string get_const_prefix() const;
62  int match_files(vector_string &results, const Filename &cwd = Filename()) const;
63 #ifdef HAVE_PYTHON
64  EXTENSION(PyObject *match_files(const Filename &cwd = Filename()) const);
65 #endif
66 
67 private:
68  bool matches_substr(string::const_iterator pi,
69  string::const_iterator pend,
70  string::const_iterator ci,
71  string::const_iterator cend) const;
72 
73  bool matches_set(string::const_iterator &pi,
74  string::const_iterator pend,
75  char ch) const;
76 
77  int r_match_files(const Filename &prefix, const string &suffix,
78  vector_string &results, const Filename &cwd);
79 
80  string _pattern;
81  bool _case_sensitive;
82  string _nomatch_chars;
83 };
84 
85 INLINE ostream &operator << (ostream &out, const GlobPattern &glob) {
86  glob.output(out);
87  return out;
88 }
89 
90 
91 #include "globPattern.I"
92 
93 #endif
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This class can be used to test for string matches against standard Unix-shell filename globbing conve...
Definition: globPattern.h:37