summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/Directory.cxx
diff options
context:
space:
mode:
authorKen Martin <ken.martin@kitware.com>2006-06-22 14:35:37 (GMT)
committerKen Martin <ken.martin@kitware.com>2006-06-22 14:35:37 (GMT)
commit847c3a58494665dd47fe57a027df00ad7d16159d (patch)
treeb6082911c7769c6c788c2a7e3231eea00d26d45c /Source/kwsys/Directory.cxx
parent55b0935a79cf3c32637b9a4d2c39da7322870042 (diff)
downloadCMake-847c3a58494665dd47fe57a027df00ad7d16159d.zip
CMake-847c3a58494665dd47fe57a027df00ad7d16159d.tar.gz
CMake-847c3a58494665dd47fe57a027df00ad7d16159d.tar.bz2
ENH: add a higher performance method to get the number of files in a directory
Diffstat (limited to 'Source/kwsys/Directory.cxx')
-rw-r--r--Source/kwsys/Directory.cxx59
1 files changed, 59 insertions, 0 deletions
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