diff --git a/dtool/Config.Irix.pp b/dtool/Config.Irix.pp index c292e40f4c..515b29d3e2 100644 --- a/dtool/Config.Irix.pp +++ b/dtool/Config.Irix.pp @@ -84,6 +84,9 @@ // Do we have ? #define HAVE_UTIME_H 1 +// Do we have ? +#define HAVE_DIRENT_H 1 + // Do we have (and presumably a Linux-style audio // interface)? #define HAVE_SYS_SOUNDCARD_H diff --git a/dtool/Config.Linux.pp b/dtool/Config.Linux.pp index 081fb907ca..6355817bc4 100644 --- a/dtool/Config.Linux.pp +++ b/dtool/Config.Linux.pp @@ -84,6 +84,9 @@ // Do we have ? #define HAVE_UTIME_H 1 +// Do we have ? +#define HAVE_DIRENT_H 1 + // Do we have (and presumably a Linux-style audio // interface)? #define HAVE_SYS_SOUNDCARD_H 1 diff --git a/dtool/Config.Win32.pp b/dtool/Config.Win32.pp index be8f5a7ff3..b4b9bfbabc 100644 --- a/dtool/Config.Win32.pp +++ b/dtool/Config.Win32.pp @@ -84,6 +84,9 @@ // Do we have ? #define HAVE_UTIME_H +// Do we have ? +#define HAVE_DIRENT_H + // Do we have (and presumably a Linux-style audio // interface)? #define HAVE_SYS_SOUNDCARD_H diff --git a/dtool/LocalSetup.pp b/dtool/LocalSetup.pp index 4f81d1a4c3..d0392b197b 100644 --- a/dtool/LocalSetup.pp +++ b/dtool/LocalSetup.pp @@ -142,6 +142,9 @@ $[cdefine HAVE_UNISTD_H] /* Define if you have the header file. */ $[cdefine HAVE_UTIME_H] +/* Define if you have the header file. */ +$[cdefine HAVE_DIRENT_H] + /* Do we have (and presumably a Linux-style audio interface)? */ $[cdefine HAVE_SYS_SOUNDCARD_H] diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index bf17ca8406..04191ffbaf 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/dtool/src/dtoolutil/filename.cxx @@ -18,6 +18,7 @@ #include // For rename() #include +#include #ifdef HAVE_UTIME_H #include @@ -27,6 +28,10 @@ #include #endif +#ifdef HAVE_DIRENT_H +#include +#endif + #if defined(WIN32) /* begin Win32-specific code */ @@ -932,6 +937,54 @@ find_on_searchpath(const DSearchPath &searchpath) { return -1; } +//////////////////////////////////////////////////////////////////// +// Function: Filename::scan_directory +// Access: Public +// Description: Attempts to open the named filename as if it were a +// directory and looks for the non-hidden files within +// the directory. Fills the given vector up with the +// sorted list of filenames that are local to this +// directory. +// +// It is the user's responsibility to ensure that the +// contents vector is empty before making this call; +// otherwise, the new files will be appended to it. +// +// Returns true on success, false if the directory could +// not be read for some reason. +//////////////////////////////////////////////////////////////////// +bool Filename:: +scan_directory(vector_string &contents) const { +#if defined(HAVE_DIRENT_H) + size_t orig_size = contents.size(); + + DIR *root = opendir(_filename.c_str()); + if (root == (DIR *)NULL) { + return false; + } + + struct dirent *d; + d = readdir(root); + while (d != (struct dirent *)NULL) { + if (d->d_name[0] != '.') { + contents.push_back(d->d_name); + } + d = readdir(root); + } + closedir(root); + + sort(contents.begin() + orig_size, contents.end()); + return true; + +#elif defined(WIN32_VC) + return false; + +#else + // Don't know how to scan directories! + return false; +#endif +} + //////////////////////////////////////////////////////////////////// // Function: Filename::open_read // Access: Public diff --git a/dtool/src/dtoolutil/filename.h b/dtool/src/dtoolutil/filename.h index 02d8dffc01..56db7e8186 100644 --- a/dtool/src/dtoolutil/filename.h +++ b/dtool/src/dtoolutil/filename.h @@ -17,6 +17,8 @@ #include +#include "vector_string.h" + #include class DSearchPath; @@ -135,6 +137,8 @@ PUBLISHED: bool make_relative_to(Filename directory, bool allow_backups = true); int find_on_searchpath(const DSearchPath &searchpath); + bool scan_directory(vector_string &contents) const; + bool open_read(ifstream &stream) const; bool open_write(ofstream &stream) const; bool open_append(ofstream &stream) const;