Commit f29fec7c authored by Ben Boeckel's avatar Ben Boeckel

Directory: accept strings in methods

Windows required non-NULL anyways (via strlen) where *nix checked it
after passing it to opendir; just assume non-NULL everywhere and take a
string where possible.

Change-Id: I85c6dc71dd11be58ba61e269c4b40b009924d967
parent ffe94132
......@@ -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) )
......
......@@ -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
......@@ -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;
}
......
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment