Commit 158c6d1c authored by Kitware Robot's avatar Kitware Robot Committed by Brad King
Browse files

KWSys 2014-08-04 (e787837a)

Extract upstream KWSys using the following shell commands.

$ git archive --prefix=upstream-kwsys/ e787837a | tar x
$ git shortlog --no-merges --abbrev=8 --format='%h %s' 65b36ede..e787837a
Ben Boeckel (4):
      9927862c SystemTools: more string replacements
      b3d598b0 strings: remove unnecessary c_str calls
      ffe94132 SystemTools: use char instead of const char*
      f29fec7c Directory: accept strings in methods

Rashad M (1):
      e787837a SharedForward: Cast away const to call execvp on MinGW 64-bit

Change-Id: I96437b332971670cfcd953717c5563e9ba0f2b99
parent d4d1b7f7
......@@ -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;
}
......@@ -257,8 +257,8 @@ void Glob::RecurseDirectory(kwsys_stl::string::size_type start,
fullname = dir + "/" + fname;
}
bool isDir = kwsys::SystemTools::FileIsDirectory(realname.c_str());
bool isSymLink = kwsys::SystemTools::FileIsSymlink(realname.c_str());
bool isDir = kwsys::SystemTools::FileIsDirectory(realname);
bool isSymLink = kwsys::SystemTools::FileIsSymlink(realname);
if ( isDir && (!isSymLink || this->RecurseThroughSymlinks) )
{
......@@ -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;
}
......@@ -342,12 +342,12 @@ void Glob::ProcessDirectory(kwsys_stl::string::size_type start,
//kwsys_ios::cout << "Full name: " << fullname << kwsys_ios::endl;
if ( !last &&
!kwsys::SystemTools::FileIsDirectory(realname.c_str()) )
!kwsys::SystemTools::FileIsDirectory(realname) )
{
continue;
}
if ( this->Internals->Expressions[start].find(fname.c_str()) )
if ( this->Internals->Expressions[start].find(fname) )
{
if ( last )
{
......@@ -371,7 +371,7 @@ bool Glob::FindFiles(const kwsys_stl::string& inexpr)
this->Internals->Expressions.clear();
this->Internals->Files.clear();
if ( !kwsys::SystemTools::FileIsFullPath(expr.c_str()) )
if ( !kwsys::SystemTools::FileIsFullPath(expr) )
{
expr = kwsys::SystemTools::GetCurrentWorkingDirectory();
expr += "/" + inexpr;
......
......@@ -512,7 +512,7 @@ static void kwsys_shared_forward_execvp(const char* cmd,
/* Invoke the child process. */
#if defined(_MSC_VER)
_execvp(cmd, argv);
#elif defined(__MINGW32__)
#elif defined(__MINGW32__) && !defined(__MINGW64__)
execvp(cmd, argv);
#else
execvp(cmd, (char* const*)argv);
......
......@@ -206,12 +206,12 @@ static time_t windows_filetime_to_posix_time(const FILETIME& ft)
#include <wctype.h>
inline int Mkdir(const char* dir)
inline int Mkdir(const kwsys_stl::string& dir)
{
return _wmkdir(
KWSYS_NAMESPACE::SystemTools::ConvertToWindowsExtendedPath(dir).c_str());
}
inline int Rmdir(const char* dir)
inline int Rmdir(const kwsys_stl::string& dir)
{
return _wrmdir(
KWSYS_NAMESPACE::SystemTools::ConvertToWindowsExtendedPath(dir).c_str());
......@@ -232,15 +232,15 @@ inline const char* Getcwd(char* buf, unsigned int len)
}
return 0;
}
inline int Chdir(const char* dir)
inline int Chdir(const kwsys_stl::string& dir)
{
#if defined(__BORLANDC__)
return chdir(dir);
return chdir(dir.c_str());
#else
return _wchdir(KWSYS_NAMESPACE::Encoding::ToWide(dir).c_str());
#endif
}
inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
inline void Realpath(const kwsys_stl::string& path, kwsys_stl::string & resolved_path)
{
kwsys_stl::wstring tmp = KWSYS_NAMESPACE::Encoding::ToWide(path);
wchar_t *ptemp;
......@@ -260,28 +260,28 @@ inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
inline int Mkdir(const char* dir)
inline int Mkdir(const kwsys_stl::string& dir)
{
return mkdir(dir, 00777);
return mkdir(dir.c_str(), 00777);
}
inline int Rmdir(const char* dir)
inline int Rmdir(const kwsys_stl::string& dir)
{
return rmdir(dir);
return rmdir(dir.c_str());
}
inline const char* Getcwd(char* buf, unsigned int len)
{
return getcwd(buf, len);
}
inline int Chdir(const char* dir)
inline int Chdir(const kwsys_stl::string& dir)
{
return chdir(dir);
return chdir(dir.c_str());
}
inline void Realpath(const char *path, kwsys_stl::string & resolved_path)
inline void Realpath(const kwsys_stl::string& path, kwsys_stl::string & resolved_path)
{
char resolved_name[KWSYS_SYSTEMTOOLS_MAXPATH];
char *ret = realpath(path, resolved_name);
char *ret = realpath(path.c_str(), resolved_name);
if(ret)
{
resolved_path = ret;
......@@ -334,9 +334,9 @@ class SystemToolsTranslationMap :
void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char* env)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
const char* pathSep = ";";
const char pathSep = ';';
#else
const char* pathSep = ":";
const char pathSep = ':';
#endif
if(!env)
{
......@@ -351,7 +351,7 @@ void SystemTools::GetPath(kwsys_stl::vector<kwsys_stl::string>& path, const char
kwsys_stl::string pathEnv = cpathEnv;
// A hack to make the below algorithm work.
if(!pathEnv.empty() && pathEnv[pathEnv.length()-1] != pathSep[0])
if(!pathEnv.empty() && *pathEnv.rbegin() != pathSep)
{
pathEnv += pathSep;
}
......@@ -623,13 +623,13 @@ const char* SystemTools::GetExecutableExtension()
#endif
}
FILE* SystemTools::Fopen(const char* file, const char* mode)
FILE* SystemTools::Fopen(const kwsys_stl::string& file, const char* mode)
{
#ifdef _WIN32
return _wfopen(SystemTools::ConvertToWindowsExtendedPath(file).c_str(),
Encoding::ToWide(mode).c_str());
#else
return fopen(file, mode);
return fopen(file.c_str(), mode);
#endif
}
......@@ -639,15 +639,20 @@ bool SystemTools::MakeDirectory(const char* path)
{
return false;
}
return SystemTools::MakeDirectory(kwsys_stl::string(path));
}
bool SystemTools::MakeDirectory(const kwsys_stl::string& path)
{
if(SystemTools::FileExists(path))
{
return SystemTools::FileIsDirectory(path);
}
kwsys_stl::string dir = path;
if(dir.empty())
if(path.empty())
{
return false;
}
kwsys_stl::string dir = path;
SystemTools::ConvertToUnixSlashes(dir);
kwsys_stl::string::size_type pos = 0;
......@@ -655,11 +660,11 @@ bool SystemTools::MakeDirectory(const char* path)
while((pos = dir.find('/', pos)) != kwsys_stl::string::npos)
{
topdir = dir.substr(0, pos);
Mkdir(topdir.c_str());
Mkdir(topdir);
pos++;
}
topdir = dir;
if(Mkdir(topdir.c_str()) != 0)
if(Mkdir(topdir) != 0)
{
// There is a bug in the Borland Run time library which makes MKDIR
// return EACCES when it should return EEXISTS
......@@ -1021,7 +1026,7 @@ bool SystemTools::DeleteRegistryValue(const char *, KeyWOW64)
}
#endif
bool SystemTools::SameFile(const char* file1, const char* file2)
bool SystemTools::SameFile(const kwsys_stl::string& file1, const kwsys_stl::string& file2)
{
#ifdef _WIN32
HANDLE hFile1, hFile2;
......@@ -1066,7 +1071,7 @@ bool SystemTools::SameFile(const char* file1, const char* file2)
fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
#else
struct stat fileStat1, fileStat2;
if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
if (stat(file1.c_str(), &fileStat1) == 0 && stat(file2.c_str(), &fileStat2) == 0)
{
// see if the files are the same file
// check the device inode and size
......@@ -1085,24 +1090,34 @@ bool SystemTools::SameFile(const char* file1, const char* file2)
//----------------------------------------------------------------------------
bool SystemTools::FileExists(const char* filename)
{
if(!(filename && *filename))
if(!filename)
{
return false;
}
return SystemTools::FileExists(kwsys_stl::string(filename));
}
//----------------------------------------------------------------------------
bool SystemTools::FileExists(const kwsys_stl::string& filename)
{
if(filename.empty())
{
return false;
}
#if defined(__CYGWIN__)
// Convert filename to native windows path if possible.
char winpath[MAX_PATH];
if(SystemTools::PathCygwinToWin32(filename, winpath))
if(SystemTools::PathCygwinToWin32(filename.c_str(), winpath))
{
return (GetFileAttributesA(winpath) != INVALID_FILE_ATTRIBUTES);
}
return access(filename, R_OK) == 0;
return access(filename.c_str(), R_OK) == 0;
#elif defined(_WIN32)
return (GetFileAttributesW(
SystemTools::ConvertToWindowsExtendedPath(filename).c_str())
!= INVALID_FILE_ATTRIBUTES);
#else
return access(filename, R_OK) == 0;
return access(filename.c_str(), R_OK) == 0;
#endif
}
......@@ -1118,6 +1133,18 @@ bool SystemTools::FileExists(const char* filename, bool isFile)
return false;
}
//----------------------------------------------------------------------------
bool SystemTools::FileExists(const kwsys_stl::string& filename, bool isFile)
{
if(SystemTools::FileExists(filename))
{
// If isFile is set return not FileIsDirectory,
// so this will only be true if it is a file
return !isFile || !SystemTools::FileIsDirectory(filename);
}
return false;
}
//----------------------------------------------------------------------------
#ifdef __CYGWIN__
bool SystemTools::PathCygwinToWin32(const char *path, char *win32_path)
......@@ -1142,7 +1169,7 @@ bool SystemTools::PathCygwinToWin32(const char *path, char *win32_path)
}
#endif
bool SystemTools::Touch(const char* filename, bool create)
bool SystemTools::Touch(const kwsys_stl::string& filename, bool create)
{
if(create && !SystemTools::FileExists(filename))
{
......@@ -1174,13 +1201,13 @@ bool SystemTools::Touch(const char* filename, bool create)
CloseHandle(h);
#elif KWSYS_CXX_HAS_UTIMENSAT
struct timespec times[2] = {{0,UTIME_OMIT},{0,UTIME_NOW}};
if(utimensat(AT_FDCWD, filename, times, 0) < 0)
if(utimensat(AT_FDCWD, filename.c_str(), times, 0) < 0)
{
return false;
}
#else
struct stat st;
if(stat(filename, &st) < 0)
if(stat(filename.c_str(), &st) < 0)
{
return false;
}
......@@ -1196,13 +1223,13 @@ bool SystemTools::Touch(const char* filename, bool create)
# endif
mtime
};
if(utimes(filename, times) < 0)
if(utimes(filename.c_str(), times) < 0)
{
return false;
}
# else
struct utimbuf times = {st.st_atime, mtime.tv_sec};
if(utime(filename, &times) < 0)
if(utime(filename.c_str(), &times) < 0)
{
return false;
}
......@@ -1211,7 +1238,8 @@ bool SystemTools::Touch(const char* filename, bool create)
return true;
}
bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
bool SystemTools::FileTimeCompare(const kwsys_stl::string& f1,
const kwsys_stl::string& f2,
int* result)
{
// Default to same time.
......@@ -1219,12 +1247,12 @@ bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
#if !defined(_WIN32) || defined(__CYGWIN__)
// POSIX version. Use stat function to get file modification time.
struct stat s1;
if(stat(f1, &s1) != 0)
if(stat(f1.c_str(), &s1) != 0)
{
return false;
}
struct stat s2;
if(stat(f2, &s2) != 0)
if(stat(f2.c_str(), &s2) != 0)
{
return false;
}
......@@ -1535,6 +1563,17 @@ bool SystemTools::StringStartsWith(const char* str1, const char* str2)
return len1 >= len2 && !strncmp(str1, str2, len2) ? true : false;
}
// Returns if string starts with another string
bool SystemTools::StringStartsWith(const kwsys_stl::string& str1, const char* str2)
{
if (!str2)
{
return false;
}
size_t len1 = str1.size(), len2 = strlen(str2);
return len1 >= len2 && !strncmp(str1.c_str(), str2, len2) ? true : false;
}
// Returns if string ends with another string
bool SystemTools::StringEndsWith(const char* str1, const char* str2)
{
......@@ -1546,6 +1585,17 @@ bool SystemTools::StringEndsWith(const char* str1, const char* str2)
return len1 >= len2 && !strncmp(str1 + (len1 - len2), str2, len2) ? true : false;
}
// Returns if string ends with another string
bool SystemTools::StringEndsWith(const kwsys_stl::string& str1, const char* str2)
{
if (!str2)
{
return false;
}
size_t len1 = str1.size(), len2 = strlen(str2);
return len1 >= len2 && !strncmp(str1.c_str() + (len1 - len2), str2, len2) ? true : false;
}
// Returns a pointer to the last occurence of str2 in str1
const char* SystemTools::FindLastString(const char* str1, const char* str2)
{
......@@ -1615,7 +1665,7 @@ kwsys_stl::string SystemTools::CropString(const kwsys_stl::string& s,
}
//----------------------------------------------------------------------------
kwsys_stl::vector<kwsys::String> SystemTools::SplitString(const char* p, char sep, bool isPath)
kwsys_stl::vector<kwsys::String> SystemTools::SplitString(const kwsys_stl::string& p, char sep, bool isPath)
{
kwsys_stl::string path = p;
kwsys_stl::vector<kwsys::String> paths;
......@@ -1917,7 +1967,7 @@ SystemTools::ConvertToWindowsExtendedPath(const kwsys_stl::string &source)
#endif
// change // to /, and escape any spaces in the path
kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const kwsys_stl::string& path)
{
kwsys_stl::string ret = path;
......@@ -1947,7 +1997,7 @@ kwsys_stl::string SystemTools::ConvertToUnixOutputPath(const char* path)
return ret;
}
kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
kwsys_stl::string SystemTools::ConvertToOutputPath(const kwsys_stl::string& path)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
return SystemTools::ConvertToWindowsOutputPath(path);
......@@ -1957,13 +2007,12 @@ kwsys_stl::string SystemTools::ConvertToOutputPath(const char* path)
}
// remove double slashes not at the start
kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const kwsys_stl::string& path)
{
kwsys_stl::string ret;
// make it big enough for all of path and double quotes
ret.reserve(strlen(path)+3);
ret.reserve(path.size()+3);
// put path into the string
ret.assign(path);
ret = path;
kwsys_stl::string::size_type pos = 0;
// first convert all of the slashes
......@@ -2005,8 +2054,8 @@ kwsys_stl::string SystemTools::ConvertToWindowsOutputPath(const char* path)
return ret;
}
bool SystemTools::CopyFileIfDifferent(const char* source,
const char* destination)
bool SystemTools::CopyFileIfDifferent(const kwsys_stl::string& source,
const kwsys_stl::string& destination)
{
// special check for a destination that is a directory
// FilesDiffer does not handle file to directory compare
......@@ -2017,7 +2066,7 @@ bool SystemTools::CopyFileIfDifferent(const char* source,
new_destination += '/';
kwsys_stl::string source_name = source;
new_destination += SystemTools::GetFilenameName(source_name);
if(SystemTools::FilesDiffer(source, new_destination.c_str()))
if(SystemTools::FilesDiffer(source, new_destination))
{
return SystemTools::CopyFileAlways(source, destination);
}
......@@ -2040,8 +2089,8 @@ bool SystemTools::CopyFileIfDifferent(const char* source,
#define KWSYS_ST_BUFFER 4096
bool SystemTools::FilesDiffer(const char* source,
const char* destination)
bool SystemTools::FilesDiffer(const kwsys_stl::string& source,
const kwsys_stl::string& destination)
{
#if defined(_WIN32)
......@@ -2079,13 +2128,13 @@ bool SystemTools::FilesDiffer(const char* source,
#else
struct stat statSource;
if (stat(source, &statSource) != 0)
if (stat(source.c_str(), &statSource) != 0)
{
return true;
}
struct stat statDestination;
if (stat(destination, &statDestination) != 0)
if (stat(destination.c_str(), &statDestination) != 0)
{
return true;
}
......@@ -2103,15 +2152,15 @@ bool SystemTools::FilesDiffer(const char* source,
#endif
#if defined(_WIN32)
kwsys::ifstream finSource(source,
kwsys::ifstream finSource(source.c_str(),
(kwsys_ios::ios::binary |
kwsys_ios::ios::in));
kwsys::ifstream finDestination(destination,
kwsys::ifstream finDestination(destination.c_str(),
(kwsys_ios::ios::binary |
kwsys_ios::ios::in));
#else
kwsys::ifstream finSource(source);
kwsys::ifstream finDestination(destination);
kwsys::ifstream finSource(source.c_str());
kwsys::ifstream finDestination(destination.c_str());
#endif