From 847c3a58494665dd47fe57a027df00ad7d16159d Mon Sep 17 00:00:00 2001 From: Ken Martin Date: Thu, 22 Jun 2006 10:35:37 -0400 Subject: ENH: add a higher performance method to get the number of files in a directory --- Source/cmake.cxx | 5 ++-- Source/kwsys/Directory.cxx | 59 +++++++++++++++++++++++++++++++++++++++++++ Source/kwsys/Directory.hxx.in | 6 +++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 2f4100f..71af58c 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1024,9 +1024,8 @@ int cmake::ExecuteCMakeCommand(std::vector& args) fclose(progFile); } } - cmsys::Directory dir; - dir.Load(dirName.c_str()); - int fileNum = static_cast(dir.GetNumberOfFiles()); + int fileNum = static_cast + (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str())); // read the count fName = dirName; fName += "/count.txt"; diff --git a/Source/kwsys/Directory.cxx b/Source/kwsys/Directory.cxx index 3f7e6ad..f7fb5e9 100644 --- a/Source/kwsys/Directory.cxx +++ b/Source/kwsys/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/Source/kwsys/Directory.hxx.in b/Source/kwsys/Directory.hxx.in index 22aafcc..ddb9104 100644 --- a/Source/kwsys/Directory.hxx.in +++ b/Source/kwsys/Directory.hxx.in @@ -48,6 +48,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 */ const char* GetFile(unsigned long) const; -- cgit v0.12