Browse Source

expose match_files() to python usefully

David Rose 14 years ago
parent
commit
f666607adc
2 changed files with 31 additions and 5 deletions
  1. 27 4
      dtool/src/dtoolutil/globPattern.cxx
  2. 4 1
      dtool/src/dtoolutil/globPattern.h

+ 27 - 4
dtool/src/dtoolutil/globPattern.cxx

@@ -17,7 +17,7 @@
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: GlobPattern::has_glob_characters
 //     Function: GlobPattern::has_glob_characters
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the pattern includes any special
 //  Description: Returns true if the pattern includes any special
 //               globbing characters, or false if it is just a literal
 //               globbing characters, or false if it is just a literal
 //               string.
 //               string.
@@ -46,7 +46,7 @@ has_glob_characters() const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: GlobPattern::get_const_prefix
 //     Function: GlobPattern::get_const_prefix
-//       Access: Public
+//       Access: Published
 //  Description: Returns the initial part of the pattern before the
 //  Description: Returns the initial part of the pattern before the
 //               first glob character.  Since many glob patterns begin
 //               first glob character.  Since many glob patterns begin
 //               with a sequence of static characters and end with one
 //               with a sequence of static characters and end with one
@@ -79,7 +79,7 @@ get_const_prefix() const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: GlobPattern::match_files
 //     Function: GlobPattern::match_files
-//       Access: Public
+//       Access: Published
 //  Description: Treats the GlobPattern as a filename pattern, and
 //  Description: Treats the GlobPattern as a filename pattern, and
 //               returns a list of any actual files that match the
 //               returns a list of any actual files that match the
 //               pattern.  This is the behavior of the standard Posix
 //               pattern.  This is the behavior of the standard Posix
@@ -96,7 +96,7 @@ get_const_prefix() const {
 //               which are added to the results vector.
 //               which are added to the results vector.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 int GlobPattern::
 int GlobPattern::
-match_files(vector_string &results, const Filename &cwd) {
+match_files(vector_string &results, const Filename &cwd) const {
   string prefix, pattern, suffix;
   string prefix, pattern, suffix;
 
 
   string source = _pattern;
   string source = _pattern;
@@ -118,6 +118,29 @@ match_files(vector_string &results, const Filename &cwd) {
   return glob.r_match_files(prefix, suffix, results, cwd);
   return glob.r_match_files(prefix, suffix, results, cwd);
 }
 }
 
 
+#ifdef HAVE_PYTHON
+////////////////////////////////////////////////////////////////////
+//     Function: GlobPattern::match_files
+//       Access: Published
+//  Description: This variant on match_files returns a Python list
+//               of strings.
+////////////////////////////////////////////////////////////////////
+PyObject *GlobPattern::
+match_files(const Filename &cwd) const {
+  vector_string contents;
+  match_files(contents, cwd);
+
+  PyObject *result = PyList_New(contents.size());
+  for (size_t i = 0; i < contents.size(); ++i) {
+    const string &filename = contents[i];
+    PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
+    PyList_SET_ITEM(result, i, str);
+  }
+
+  return result;
+}
+#endif  // HAVE_PYTHON
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: GlobPattern::r_match_files
 //     Function: GlobPattern::r_match_files
 //       Access: Private
 //       Access: Private

+ 4 - 1
dtool/src/dtoolutil/globPattern.h

@@ -59,7 +59,10 @@ PUBLISHED:
 
 
   bool has_glob_characters() const;
   bool has_glob_characters() const;
   string get_const_prefix() const;
   string get_const_prefix() const;
-  int match_files(vector_string &results, const Filename &cwd = Filename());
+  int match_files(vector_string &results, const Filename &cwd = Filename()) const;
+#ifdef HAVE_PYTHON
+  PyObject *match_files(const Filename &cwd = Filename()) const;
+#endif 
 
 
 private:
 private:
   bool matches_substr(string::const_iterator pi,
   bool matches_substr(string::const_iterator pi,