Quellcode durchsuchen

scan_directory() for python

David Rose vor 16 Jahren
Ursprung
Commit
be494155cf
2 geänderte Dateien mit 30 neuen und 0 gelöschten Zeilen
  1. 27 0
      dtool/src/dtoolutil/filename.cxx
  2. 3 0
      dtool/src/dtoolutil/filename.h

+ 27 - 0
dtool/src/dtoolutil/filename.cxx

@@ -1886,6 +1886,33 @@ scan_directory(vector_string &contents) const {
 #endif
 #endif
 }
 }
 
 
+#ifdef HAVE_PYTHON
+////////////////////////////////////////////////////////////////////
+//     Function: Filename::scan_directory
+//       Access: Published
+//  Description: This variant on scan_directory returns a Python list
+//               of strings on success, or None on failure.
+////////////////////////////////////////////////////////////////////
+PyObject *Filename::
+scan_directory() const {
+  vector_string contents;
+  if (!scan_directory(contents)) {
+    PyObject *result = Py_None;
+    Py_INCREF(result);
+    return result;
+  }
+
+  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: Filename::open_read
 //     Function: Filename::open_read
 //       Access: Published
 //       Access: Published

+ 3 - 0
dtool/src/dtoolutil/filename.h

@@ -181,6 +181,9 @@ PUBLISHED:
   int find_on_searchpath(const DSearchPath &searchpath);
   int find_on_searchpath(const DSearchPath &searchpath);
   
   
   bool scan_directory(vector_string &contents) const;
   bool scan_directory(vector_string &contents) const;
+#ifdef HAVE_PYTHON
+  PyObject *scan_directory() const;
+#endif 
 
 
   bool open_read(ifstream &stream) const;
   bool open_read(ifstream &stream) const;
   bool open_write(ofstream &stream, bool truncate = true) const;
   bool open_write(ofstream &stream, bool truncate = true) const;