diff --git a/Directory.cxx b/Directory.cxx index b305fd7a10bb3793d3d29f8080ddb89d10dc7086..741bcba2712c60de1f56b20f76ef2ab1cf86036e 100644 --- a/Directory.cxx +++ b/Directory.cxx @@ -103,7 +103,7 @@ void Directory::Clear() namespace KWSYS_NAMESPACE { -bool Directory::Load(const char* name) +bool Directory::Load(const kwsys_stl::string& name) { this->Clear(); #if _MSC_VER < 1300 @@ -112,24 +112,24 @@ bool Directory::Load(const char* name) intptr_t srchHandle; #endif char* buf; - size_t n = strlen(name); - if ( name[n - 1] == '/' || name[n - 1] == '\\' ) + size_t n = name.size(); + if ( *name.rbegin() == '/' || *name.rbegin() == '\\' ) { buf = new char[n + 1 + 1]; - sprintf(buf, "%s*", name); + sprintf(buf, "%s*", name.c_str()); } else { // Make sure the slashes in the wildcard suffix are consistent with the // rest of the path buf = new char[n + 2 + 1]; - if ( strchr(name, '\\') ) + if ( name.find('\\') != name.npos ) { - sprintf(buf, "%s\\*", name); + sprintf(buf, "%s\\*", name.c_str()); } else { - sprintf(buf, "%s/*", name); + sprintf(buf, "%s/*", name.c_str()); } } struct _wfinddata_t data; // data of current file @@ -153,7 +153,7 @@ bool Directory::Load(const char* name) return _findclose(srchHandle) != -1; } -unsigned long Directory::GetNumberOfFilesInDirectory(const char* name) +unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name) { #if _MSC_VER < 1300 long srchHandle; @@ -161,16 +161,16 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const char* name) intptr_t srchHandle; #endif char* buf; - size_t n = strlen(name); - if ( name[n - 1] == '/' ) + size_t n = name.size(); + if ( *name.rbegin() == '/' ) { buf = new char[n + 1 + 1]; - sprintf(buf, "%s*", name); + sprintf(buf, "%s*", name.c_str()); } else { buf = new char[n + 2 + 1]; - sprintf(buf, "%s/*", name); + sprintf(buf, "%s/*", name.c_str()); } struct _wfinddata_t data; // data of current file @@ -215,15 +215,11 @@ p=1992&sid=f16167f51964f1a68fe5041b8eb213b6 namespace KWSYS_NAMESPACE { -bool Directory::Load(const char* name) +bool Directory::Load(const kwsys_stl::string& name) { this->Clear(); - if (!name) - { - return 0; - } - DIR* dir = opendir(name); + DIR* dir = opendir(name.c_str()); if (!dir) { @@ -239,14 +235,9 @@ bool Directory::Load(const char* name) return 1; } -unsigned long Directory::GetNumberOfFilesInDirectory(const char* name) +unsigned long Directory::GetNumberOfFilesInDirectory(const kwsys_stl::string& name) { - DIR* dir = opendir(name); - - if (!dir) - { - return 0; - } + DIR* dir = opendir(name.c_str()); unsigned long count = 0; for (dirent* d = readdir(dir); d; d = readdir(dir) ) diff --git a/Directory.hxx.in b/Directory.hxx.in index 05217c46d798cb3dc54f736a08363cc74fc8b1fc..0acb1913f4de0279165f3a7be401e26acb1acf08 100644 --- a/Directory.hxx.in +++ b/Directory.hxx.in @@ -13,6 +13,13 @@ #define @KWSYS_NAMESPACE@_Directory_hxx #include <@KWSYS_NAMESPACE@/Configure.h> +#include <@KWSYS_NAMESPACE@/stl/string> + +/* Define these macros temporarily to keep the code readable. */ +#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS +# define kwsys_stl @KWSYS_NAMESPACE@_stl +# define kwsys_ios @KWSYS_NAMESPACE@_ios +#endif namespace @KWSYS_NAMESPACE@ { @@ -38,7 +45,7 @@ public: * in that directory. 0 is returned if the directory can not be * opened, 1 if it is opened. */ - bool Load(const char*); + bool Load(const kwsys_stl::string&); /** * Return the number of files in the current directory. @@ -49,7 +56,7 @@ public: * Return the number of files in the specified directory. * A higher performance static method. */ - static unsigned long GetNumberOfFilesInDirectory(const char*); + static unsigned long GetNumberOfFilesInDirectory(const kwsys_stl::string&); /** * Return the file at the given index, the indexing is 0 based @@ -77,4 +84,10 @@ private: } // namespace @KWSYS_NAMESPACE@ +/* Undefine temporary macros. */ +#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS +# undef kwsys_stl +# undef kwsys_ios +#endif + #endif diff --git a/Glob.cxx b/Glob.cxx index 352df5ed46b7aaccb2770af19893487a953a6f49..0916d2e6f26c1f27df7e6b33356932d5df80b51d 100644 --- a/Glob.cxx +++ b/Glob.cxx @@ -218,7 +218,7 @@ void Glob::RecurseDirectory(kwsys_stl::string::size_type start, const kwsys_stl::string& dir) { kwsys::Directory d; - if ( !d.Load(dir.c_str()) ) + if ( !d.Load(dir) ) { return; } @@ -297,7 +297,7 @@ void Glob::ProcessDirectory(kwsys_stl::string::size_type start, } kwsys::Directory d; - if ( !d.Load(dir.c_str()) ) + if ( !d.Load(dir) ) { return; } diff --git a/SystemTools.cxx b/SystemTools.cxx index 7c48f39c2b32d407b7c7fe039c1d161da207e7ba..dcfd49c890129c89799a1a887c676369949914e2 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -2332,9 +2332,9 @@ bool SystemTools::CopyADirectory(const kwsys_stl::string& source, const kwsys_st Directory dir; #ifdef _WIN32 dir.Load(Encoding::ToNarrow( - SystemTools::ConvertToWindowsExtendedPath(source)).c_str()); + SystemTools::ConvertToWindowsExtendedPath(source))); #else - dir.Load(source.c_str()); + dir.Load(source); #endif size_t fileNum; if ( !SystemTools::MakeDirectory(destination) ) @@ -2613,9 +2613,9 @@ bool SystemTools::RemoveADirectory(const kwsys_stl::string& source) Directory dir; #ifdef _WIN32 dir.Load(Encoding::ToNarrow( - SystemTools::ConvertToWindowsExtendedPath(source)).c_str()); + SystemTools::ConvertToWindowsExtendedPath(source))); #else - dir.Load(source.c_str()); + dir.Load(source); #endif size_t fileNum; for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)