diff --git a/Directory.cxx b/Directory.cxx index a7d10993307812b432b27a30cb1c58198ca7db22..3fb4d6d6d06865da0059203700286d23a74db4f8 100644 --- a/Directory.cxx +++ b/Directory.cxx @@ -143,6 +143,47 @@ bool Directory::Load(const char* name) return _findclose(srchHandle) != -1; } +unsigned long Directory::GetNumberOfFilesInDirectory(const char* name) +{ +#if _MSC_VER < 1300 + long srchHandle; +#else + intptr_t srchHandle; +#endif + char* buf; + size_t n = strlen(name); + if ( name[n - 1] == '/' ) + { + buf = new char[n + 1 + 1]; + sprintf(buf, "%s*", name); + } + else + { + buf = new char[n + 2 + 1]; + sprintf(buf, "%s/*", name); + } + struct _finddata_t data; // data of current file + + // Now put them into the file array + srchHandle = _findfirst(buf, &data); + delete [] buf; + + if ( srchHandle == -1 ) + { + return 0; + } + + // Loop through names + unsigned long count = 0; + do + { + count++; + } + while ( _findnext(srchHandle, &data) != -1 ); + _findclose(srchHandle); + return count; +} + } // namespace KWSYS_NAMESPACE #else @@ -174,6 +215,24 @@ bool Directory::Load(const char* name) return 1; } +unsigned long Directory::GetNumberOfFilesInDirectory(const char* name) +{ + DIR* dir = opendir(name); + + if (!dir) + { + return 0; + } + + unsigned long count = 0; + for (dirent* d = readdir(dir); d; d = readdir(dir) ) + { + count++; + } + closedir(dir); + return count; +} + } // namespace KWSYS_NAMESPACE #endif diff --git a/Directory.hxx.in b/Directory.hxx.in index d4b9bab94b8d3f875c684493bce3bffa0ae03b82..a46d76e6abe7ab925d85f8e50a2ac83ab08558d8 100644 --- a/Directory.hxx.in +++ b/Directory.hxx.in @@ -47,6 +47,12 @@ public: */ unsigned long GetNumberOfFiles() const; + /** + * Return the number of files in the specified directory. + * A higher performance static method. + */ + static unsigned long GetNumberOfFilesInDirectory(const char*); + /** * Return the file at the given index, the indexing is 0 based */