From 72b5b48040ed65fdb54b8ffeb3326b456586b8c8 Mon Sep 17 00:00:00 2001 From: KWSys Robot Date: Fri, 31 Oct 2014 13:09:11 -0400 Subject: [PATCH] KWSys 2014-10-31 (88c8cc7f) Extract upstream KWSys using the following shell commands. $ git archive --prefix=upstream-kwsys/ 88c8cc7f | tar x $ git shortlog --no-merges --abbrev=8 --format='%h %s' 29ffaf43..88c8cc7f Ben Boeckel (11): 9bf03363 Avoid if() quoted auto-dereference 771e0f22 DynamicLoader: use strings for arguments 86e20d68 Directory: remove extra define 6cc24510 SystemTools: use strings in environment functions bab5b1f2 SystemTools: add string overload for ReplaceString f3fb01cf SystemTools: use strings for path-related APIs 0dfbe56d SystemTools: use strings for making C identifiers 4690fc8d SystemTools: use strings in registry function 1b762a41 SystemTools: search for characters 4a060347 SystemInformation: use %ls for WCHAR* format specifiers d31f7b12 SystemTools: remove unused variable Brad King (1): 88c8cc7f Fix configure_file call to use COPYONLY, not COPY_ONLY Clinton Stimpson (1): 5bf91dda SystemTools: Use extended length path for copying files. Change-Id: I16e8e55dea1c171c04f9c7d04ae3c575531097c3 --- CMakeLists.txt | 6 +- Directory.hxx.in | 2 - DynamicLoader.cxx | 66 ++++++------ DynamicLoader.hxx.in | 15 ++- Glob.cxx | 2 +- SystemInformation.cxx | 6 +- SystemTools.cxx | 233 +++++++++++++++++++++++++++++------------- SystemTools.hxx.in | 51 +++++---- testSystemTools.cxx | 4 +- 9 files changed, 250 insertions(+), 135 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ca43608c..2292d6461 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,7 +265,7 @@ STRING(COMPARE EQUAL "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}" KWSYS_IN_SOURCE_BUILD) IF(NOT KWSYS_IN_SOURCE_BUILD) CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/kwsysPrivate.h - ${PROJECT_BINARY_DIR}/kwsysPrivate.h COPY_ONLY IMMEDIATE) + ${PROJECT_BINARY_DIR}/kwsysPrivate.h COPYONLY IMMEDIATE) ENDIF(NOT KWSYS_IN_SOURCE_BUILD) # Select plugin module file name convention. @@ -1075,7 +1075,7 @@ ENDIF(KWSYS_ENABLE_C AND KWSYS_C_SRCS) ADD_DEFINITIONS("-DKWSYS_NAMESPACE=${KWSYS_NAMESPACE}") # Disable deprecation warnings for standard C functions. -IF(MSVC OR (WIN32 AND "${CMAKE_C_COMPILER_ID}" MATCHES "^(Intel)$")) +IF(MSVC OR (WIN32 AND CMAKE_C_COMPILER_ID STREQUAL "Intel")) ADD_DEFINITIONS( -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_DEPRECATE @@ -1232,7 +1232,7 @@ IF(KWSYS_STANDALONE OR CMake_SOURCE_DIR) ENDFOREACH(n) # Some Apple compilers produce bad optimizations in this source. - IF(APPLE AND "${CMAKE_C_COMPILER_ID}" MATCHES "^(GNU|LLVM)$") + IF(APPLE AND CMAKE_C_COMPILER_ID MATCHES "^(GNU|LLVM)$") SET_SOURCE_FILES_PROPERTIES(testProcess.c PROPERTIES COMPILE_FLAGS -O0) ELSEIF(CMAKE_C_COMPILER_ID STREQUAL "XL") # Tell IBM XL not to warn about our test infinite loop diff --git a/Directory.hxx.in b/Directory.hxx.in index 0acb1913f..1bcf90e18 100644 --- a/Directory.hxx.in +++ b/Directory.hxx.in @@ -18,7 +18,6 @@ /* 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@ @@ -87,7 +86,6 @@ private: /* Undefine temporary macros. */ #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS # undef kwsys_stl -# undef kwsys_ios #endif #endif diff --git a/DynamicLoader.cxx b/DynamicLoader.cxx index 44cf6af7e..66c7d5767 100644 --- a/DynamicLoader.cxx +++ b/DynamicLoader.cxx @@ -40,9 +40,9 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { - return shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L); + return shl_load(libname.c_str(), BIND_DEFERRED | DYNAMIC_PATH, 0L); } //---------------------------------------------------------------------------- @@ -53,7 +53,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer -DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const char* sym) +DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { void* addr; int status; @@ -62,7 +62,7 @@ DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const char* sy * TYPE_DATA Look for a symbol in the data segment (for example, variables). * TYPE_UNDEFINED Look for any symbol. */ - status = shl_findsym (&lib, sym, TYPE_UNDEFINED, &addr); + status = shl_findsym (&lib, sym.c_str(), TYPE_UNDEFINED, &addr); void* result = (status < 0) ? (void*)0 : addr; // Hack to cast pointer-to-data to pointer-to-function. @@ -111,18 +111,18 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { NSObjectFileImageReturnCode rc; NSObjectFileImage image = 0; - rc = NSCreateObjectFileImageFromFile(libname, &image); + rc = NSCreateObjectFileImageFromFile(libname.c_str(), &image); // rc == NSObjectFileImageInappropriateFile when trying to load a dylib file if( rc != NSObjectFileImageSuccess ) { return 0; } - NSModule handle = NSLinkModule(image, libname, + NSModule handle = NSLinkModule(image, libname.c_str(), NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR); NSDestroyObjectFileImage(image); return handle; @@ -142,14 +142,14 @@ int DynamicLoader::CloseLibrary( DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { void *result=0; // Need to prepend symbols with '_' on Apple-gcc compilers - size_t len = strlen(sym); + size_t len = sym.size(); char *rsym = new char[len + 1 + 1]; strcpy(rsym, "_"); - strcat(rsym+1, sym); + strcat(rsym+1, sym.c_str()); NSSymbol symbol = NSLookupSymbolInModule(lib, rsym); if(symbol) @@ -183,13 +183,13 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname) { DynamicLoader::LibraryHandle lh; - int length = MultiByteToWideChar(CP_UTF8, 0, libname, -1, NULL, 0); + int length = MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, NULL, 0); wchar_t* wchars = new wchar_t[length+1]; wchars[0] = '\0'; - MultiByteToWideChar(CP_UTF8, 0, libname, -1, wchars, length); + MultiByteToWideChar(CP_UTF8, 0, libname.c_str(), -1, wchars, length); lh = LoadLibraryW(wchars); delete [] wchars; return lh; @@ -203,7 +203,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { // TODO: The calling convention affects the name of the symbol. We // should have a tool to help get the symbol with the desired @@ -230,12 +230,12 @@ DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( void *result; #if defined(__BORLANDC__) || defined(__WATCOMC__) // Need to prepend symbols with '_' - size_t len = strlen(sym); + size_t len = sym.size(); char *rsym = new char[len + 1 + 1]; strcpy(rsym, "_"); - strcat(rsym, sym); + strcat(rsym, sym.c_str()); #else - const char *rsym = sym; + const char *rsym = sym.c_str(); #endif result = (void*)GetProcAddress(lib, rsym); #if defined(__BORLANDC__) || defined(__WATCOMC__) @@ -298,11 +298,11 @@ namespace KWSYS_NAMESPACE static image_id last_dynamic_err = B_OK; //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { // image_id's are integers, errors are negative. Add one just in case we // get a valid image_id of zero (is that even possible?). - image_id rc = load_add_on(libname); + image_id rc = load_add_on(libname.c_str()); if (rc < 0) { last_dynamic_err = rc; @@ -336,7 +336,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { // Hack to cast pointer-to-data to pointer-to-function. union @@ -356,7 +356,7 @@ DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( // !!! FIXME: BeOS can do function-only lookups...does this ever // !!! FIXME: actually _want_ a data symbol lookup, or was this union // !!! FIXME: a leftover of dlsym()? (s/ANY/TEXT for functions only). - status_t rc = get_image_symbol(lib-1,sym,B_SYMBOL_TYPE_ANY,&result.pvoid); + status_t rc = get_image_symbol(lib-1,sym.c_str(),B_SYMBOL_TYPE_ANY,&result.pvoid); if (rc != B_OK) { last_dynamic_err = rc; @@ -389,7 +389,7 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { return 0; } @@ -407,7 +407,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { return 0; } @@ -433,12 +433,12 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { - char *name = (char *)calloc(1, strlen(libname) + 1); + char *name = (char *)calloc(1, libname.size() + 1); dld_init(program_invocation_name); - strncpy(name, libname, strlen(libname)); - dld_link(libname); + strncpy(name, libname.c_str(), libname.size()); + dld_link(libname.c_str()); return (void *)name; } @@ -452,7 +452,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { // Hack to cast pointer-to-data to pointer-to-function. union @@ -460,7 +460,7 @@ DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( void* pvoid; DynamicLoader::SymbolPointer psym; } result; - result.pvoid = dld_get_symbol(sym); + result.pvoid = dld_get_symbol(sym.c_str()); return result.psym; } @@ -485,9 +485,9 @@ namespace KWSYS_NAMESPACE { //---------------------------------------------------------------------------- -DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname ) +DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const kwsys_stl::string& libname ) { - return dlopen(libname, RTLD_LAZY); + return dlopen(libname.c_str(), RTLD_LAZY); } //---------------------------------------------------------------------------- @@ -504,7 +504,7 @@ int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib) //---------------------------------------------------------------------------- DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( - DynamicLoader::LibraryHandle lib, const char* sym) + DynamicLoader::LibraryHandle lib, const kwsys_stl::string& sym) { // Hack to cast pointer-to-data to pointer-to-function. union @@ -512,7 +512,7 @@ DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress( void* pvoid; DynamicLoader::SymbolPointer psym; } result; - result.pvoid = dlsym(lib, sym); + result.pvoid = dlsym(lib, sym.c_str()); return result.psym; } diff --git a/DynamicLoader.hxx.in b/DynamicLoader.hxx.in index 64468ecd3..75811ab85 100644 --- a/DynamicLoader.hxx.in +++ b/DynamicLoader.hxx.in @@ -13,6 +13,7 @@ #define @KWSYS_NAMESPACE@_DynamicLoader_hxx #include <@KWSYS_NAMESPACE@/Configure.h> +#include <@KWSYS_NAMESPACE@/stl/string> #if defined(__hpux) #include @@ -27,6 +28,11 @@ #include #endif +/* Define these macros temporarily to keep the code readable. */ +#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS +# define kwsys_stl @KWSYS_NAMESPACE@_stl +#endif + namespace @KWSYS_NAMESPACE@ { /** \class DynamicLoader @@ -77,14 +83,14 @@ public: /** Load a dynamic library into the current process. * The returned LibraryHandle can be used to access the symbols in the * library. */ - static LibraryHandle OpenLibrary(const char*); + static LibraryHandle OpenLibrary(const kwsys_stl::string&); /** Attempt to detach a dynamic library from the * process. A value of true is returned if it is sucessful. */ static int CloseLibrary(LibraryHandle); /** Find the address of the symbol in the given library. */ - static SymbolPointer GetSymbolAddress(LibraryHandle, const char*); + static SymbolPointer GetSymbolAddress(LibraryHandle, const kwsys_stl::string&); /** Return the default module prefix for the current platform. */ static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; } @@ -98,4 +104,9 @@ public: } // namespace @KWSYS_NAMESPACE@ +/* Undefine temporary macros. */ +#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS +# undef kwsys_stl +#endif + #endif diff --git a/Glob.cxx b/Glob.cxx index 0916d2e6f..5a96aed43 100644 --- a/Glob.cxx +++ b/Glob.cxx @@ -501,7 +501,7 @@ void Glob::AddFile(kwsys_stl::vector& files, const kwsys_stl: { if ( !this->Relative.empty() ) { - files.push_back(kwsys::SystemTools::RelativePath(this->Relative.c_str(), file.c_str())); + files.push_back(kwsys::SystemTools::RelativePath(this->Relative, file)); } else { diff --git a/SystemInformation.cxx b/SystemInformation.cxx index 84b5f39f3..d23c248b7 100644 --- a/SystemInformation.cxx +++ b/SystemInformation.cxx @@ -5156,7 +5156,7 @@ bool SystemInformationImplementation::QueryOSInformation() } } - sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); + sprintf (operatingSystem, "%ls (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); this->OSVersion = operatingSystem; } else @@ -5205,7 +5205,7 @@ bool SystemInformationImplementation::QueryOSInformation() if (osvi.dwMajorVersion <= 4) { // NB: NT 4.0 and earlier. - sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)", + sprintf (operatingSystem, "version %ld.%ld %ls (Build %ld)", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion, @@ -5236,7 +5236,7 @@ bool SystemInformationImplementation::QueryOSInformation() else { // Windows 2000 and everything else. - sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); + sprintf (operatingSystem,"%ls (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); this->OSVersion = operatingSystem; } break; diff --git a/SystemTools.cxx b/SystemTools.cxx index b1221e3a8..e4c82d8a8 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -222,7 +222,7 @@ inline int Rmdir(const kwsys_stl::string& dir) inline const char* Getcwd(char* buf, unsigned int len) { std::vector w_buf(len); - if(const wchar_t* ret = _wgetcwd(&w_buf[0], len)) + if(_wgetcwd(&w_buf[0], len)) { // make sure the drive letter is capital if(wcslen(&w_buf[0]) > 1 && w_buf[1] == L':') @@ -385,6 +385,11 @@ const char* SystemTools::GetEnv(const char* key) return getenv(key); } +const char* SystemTools::GetEnv(const kwsys_stl::string& key) +{ + return SystemTools::GetEnv(key.c_str()); +} + bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result) { const char* v = getenv(key); @@ -399,6 +404,11 @@ bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result) } } +bool SystemTools::GetEnv(const kwsys_stl::string& key, kwsys_stl::string& result) +{ + return SystemTools::GetEnv(key.c_str(), result); +} + //---------------------------------------------------------------------------- #if defined(__CYGWIN__) || defined(__GLIBC__) @@ -410,27 +420,28 @@ bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result) #if KWSYS_CXX_HAS_UNSETENV /* unsetenv("A") removes A from the environment. On older platforms it returns void instead of int. */ -static int kwsysUnPutEnv(const char* env) +static int kwsysUnPutEnv(const kwsys_stl::string& env) { - if(const char* eq = strchr(env, '=')) + size_t pos = env.find('='); + if(pos != env.npos) { - std::string name(env, eq-env); + std::string name = env.substr(0, pos); unsetenv(name.c_str()); } else { - unsetenv(env); + unsetenv(env.c_str()); } return 0; } #elif defined(KWSYS_PUTENV_EMPTY) || defined(KWSYS_PUTENV_NAME) /* putenv("A=") or putenv("A") removes A from the environment. */ -static int kwsysUnPutEnv(const char* env) +static int kwsysUnPutEnv(const kwsys_stl::string& env) { int err = 0; - const char* eq = strchr(env, '='); - size_t const len = eq? (size_t)(eq-env) : strlen(env); + size_t pos = env.find('='); + size_t const len = pos == env.npos ? env.size() : pos; # ifdef KWSYS_PUTENV_EMPTY size_t const sz = len + 2; # else @@ -442,7 +453,7 @@ static int kwsysUnPutEnv(const char* env) { return -1; } - strncpy(buf, env, len); + strncpy(buf, env.c_str(), len); # ifdef KWSYS_PUTENV_EMPTY buf[len] = '='; buf[len+1] = 0; @@ -471,17 +482,17 @@ static int kwsysUnPutEnv(const char* env) #else /* Manipulate the "environ" global directly. */ -static int kwsysUnPutEnv(const char* env) +static int kwsysUnPutEnv(const kwsys_stl::string& env) { - const char* eq = strchr(env, '='); - size_t const len = eq? (size_t)(eq-env) : strlen(env); + size_t pos = env.find('='); + size_t const len = pos == env.npos ? env.size() : pos; int in = 0; int out = 0; while(environ[in]) { if(strlen(environ[in]) > len && environ[in][len] == '=' && - strncmp(env, environ[in], len) == 0) + strncmp(env.c_str(), environ[in], len) == 0) { ++in; } @@ -504,12 +515,13 @@ static int kwsysUnPutEnv(const char* env) /* setenv("A", "B", 1) will set A=B in the environment and makes its own copies of the strings. */ -bool SystemTools::PutEnv(const char* env) +bool SystemTools::PutEnv(const kwsys_stl::string& env) { - if(const char* eq = strchr(env, '=')) + size_t pos = env.find('='); + if(pos != env.npos) { - std::string name(env, eq-env); - return setenv(name.c_str(), eq+1, 1) == 0; + std::string name = env.substr(0, pos); + return setenv(name.c_str(), env.c_str() + pos + 1, 1) == 0; } else { @@ -517,7 +529,7 @@ bool SystemTools::PutEnv(const char* env) } } -bool SystemTools::UnPutEnv(const char* env) +bool SystemTools::UnPutEnv(const kwsys_stl::string& env) { return kwsysUnPutEnv(env) == 0; } @@ -603,14 +615,14 @@ public: static kwsysEnv kwsysEnvInstance; -bool SystemTools::PutEnv(const char* env) +bool SystemTools::PutEnv(const kwsys_stl::string& env) { - return kwsysEnvInstance.Put(env); + return kwsysEnvInstance.Put(env.c_str()); } -bool SystemTools::UnPutEnv(const char* env) +bool SystemTools::UnPutEnv(const kwsys_stl::string& env) { - return kwsysEnvInstance.UnPut(env); + return kwsysEnvInstance.UnPut(env.c_str()); } #endif @@ -689,8 +701,35 @@ bool SystemTools::MakeDirectory(const kwsys_stl::string& path) // replace replace with with as many times as it shows up in source. // write the result into source. void SystemTools::ReplaceString(kwsys_stl::string& source, - const char* replace, - const char* with) + const kwsys_stl::string& replace, + const kwsys_stl::string& with) +{ + // do while hangs if replaceSize is 0 + if (replace.empty()) + { + return; + } + + SystemTools::ReplaceString(source, replace.c_str(), replace.size(), with); +} + +void SystemTools::ReplaceString(kwsys_stl::string& source, + const char* replace, + const char* with) +{ + // do while hangs if replaceSize is 0 + if (!*replace) + { + return; + } + + SystemTools::ReplaceString(source, replace, strlen(replace), with ? with : ""); +} + +void SystemTools::ReplaceString(kwsys_stl::string& source, + const char* replace, + size_t replaceSize, + const kwsys_stl::string& with) { const char *src = source.c_str(); char *searchPos = const_cast(strstr(src,replace)); @@ -702,12 +741,6 @@ void SystemTools::ReplaceString(kwsys_stl::string& source, } // perform replacements until done - size_t replaceSize = strlen(replace); - // do while hangs if replaceSize is 0 - if(replaceSize == 0) - { - return; - } char *orig = strdup(src); char *currentPos = orig; searchPos = searchPos - src + orig; @@ -739,20 +772,20 @@ void SystemTools::ReplaceString(kwsys_stl::string& source, #endif #if defined(_WIN32) && !defined(__CYGWIN__) -static bool SystemToolsParseRegistryKey(const char* key, +static bool SystemToolsParseRegistryKey(const kwsys_stl::string& key, HKEY& primaryKey, kwsys_stl::string& second, kwsys_stl::string& valuename) { kwsys_stl::string primary = key; - size_t start = primary.find("\\"); + size_t start = primary.find('\\'); if (start == kwsys_stl::string::npos) { return false; } - size_t valuenamepos = primary.find(";"); + size_t valuenamepos = primary.find(';'); if (valuenamepos != kwsys_stl::string::npos) { valuename = primary.substr(valuenamepos+1); @@ -810,7 +843,7 @@ static DWORD SystemToolsMakeRegistryMode(DWORD mode, #if defined(_WIN32) && !defined(__CYGWIN__) bool -SystemTools::GetRegistrySubKeys(const char *key, +SystemTools::GetRegistrySubKeys(const kwsys_stl::string& key, kwsys_stl::vector& subkeys, KeyWOW64 view) { @@ -849,7 +882,7 @@ SystemTools::GetRegistrySubKeys(const char *key, return true; } #else -bool SystemTools::GetRegistrySubKeys(const char *, +bool SystemTools::GetRegistrySubKeys(const kwsys_stl::string&, kwsys_stl::vector&, KeyWOW64) { @@ -865,7 +898,7 @@ bool SystemTools::GetRegistrySubKeys(const char *, // => will return the data of the "Root" value of the key #if defined(_WIN32) && !defined(__CYGWIN__) -bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value, +bool SystemTools::ReadRegistryValue(const kwsys_stl::string& key, kwsys_stl::string &value, KeyWOW64 view) { bool valueset = false; @@ -922,7 +955,7 @@ bool SystemTools::ReadRegistryValue(const char *key, kwsys_stl::string &value, return valueset; } #else -bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &, +bool SystemTools::ReadRegistryValue(const kwsys_stl::string&, kwsys_stl::string &, KeyWOW64) { return false; @@ -938,7 +971,8 @@ bool SystemTools::ReadRegistryValue(const char *, kwsys_stl::string &, // => will set the data of the "Root" value of the key #if defined(_WIN32) && !defined(__CYGWIN__) -bool SystemTools::WriteRegistryValue(const char *key, const char *value, +bool SystemTools::WriteRegistryValue(const kwsys_stl::string& key, + const kwsys_stl::string& value, KeyWOW64 view) { HKEY primaryKey = HKEY_CURRENT_USER; @@ -978,7 +1012,7 @@ bool SystemTools::WriteRegistryValue(const char *key, const char *value, return false; } #else -bool SystemTools::WriteRegistryValue(const char *, const char *, KeyWOW64) +bool SystemTools::WriteRegistryValue(const kwsys_stl::string&, const kwsys_stl::string&, KeyWOW64) { return false; } @@ -992,7 +1026,7 @@ bool SystemTools::WriteRegistryValue(const char *, const char *, KeyWOW64) // => will delete the data of the "Root" value of the key #if defined(_WIN32) && !defined(__CYGWIN__) -bool SystemTools::DeleteRegistryValue(const char *key, KeyWOW64 view) +bool SystemTools::DeleteRegistryValue(const kwsys_stl::string& key, KeyWOW64 view) { HKEY primaryKey = HKEY_CURRENT_USER; kwsys_stl::string second; @@ -1023,7 +1057,7 @@ bool SystemTools::DeleteRegistryValue(const char *key, KeyWOW64 view) return false; } #else -bool SystemTools::DeleteRegistryValue(const char *, KeyWOW64) +bool SystemTools::DeleteRegistryValue(const kwsys_stl::string&, KeyWOW64) { return false; } @@ -2245,12 +2279,13 @@ bool SystemTools::CopyFileAlways(const kwsys_stl::string& source, const kwsys_st SystemTools::MakeDirectory(destination_dir); // Open files - -#if defined(_WIN32) || defined(__CYGWIN__) - kwsys::ifstream fin(source.c_str(), - kwsys_ios::ios::binary | kwsys_ios::ios::in); +#if defined(_WIN32) + kwsys::ifstream fin(Encoding::ToNarrow( + SystemTools::ConvertToWindowsExtendedPath(source)).c_str(), + kwsys_ios::ios::in | kwsys_ios_binary); #else - kwsys::ifstream fin(source.c_str()); + kwsys::ifstream fin(source.c_str(), + kwsys_ios::ios::in | kwsys_ios_binary); #endif if(!fin) { @@ -2263,12 +2298,13 @@ bool SystemTools::CopyFileAlways(const kwsys_stl::string& source, const kwsys_st // that do not allow file removal can be modified. SystemTools::RemoveFile(real_destination); -#if defined(_WIN32) || defined(__CYGWIN__) - kwsys::ofstream fout(real_destination.c_str(), - kwsys_ios::ios::binary | kwsys_ios::ios::out | kwsys_ios::ios::trunc); +#if defined(_WIN32) + kwsys::ofstream fout(Encoding::ToNarrow( + SystemTools::ConvertToWindowsExtendedPath(real_destination)).c_str(), + kwsys_ios::ios::out | kwsys_ios::ios::trunc | kwsys_ios_binary); #else kwsys::ofstream fout(real_destination.c_str(), - kwsys_ios::ios::out | kwsys_ios::ios::trunc); + kwsys_ios::ios::out | kwsys_ios::ios::trunc | kwsys_ios_binary); #endif if(!fout) { @@ -2379,7 +2415,7 @@ bool SystemTools::CopyADirectory(const kwsys_stl::string& source, const kwsys_st // return size of file; also returns zero if no file exists -unsigned long SystemTools::FileLength(const char* filename) +unsigned long SystemTools::FileLength(const kwsys_stl::string& filename) { unsigned long length = 0; #ifdef _WIN32 @@ -2397,7 +2433,7 @@ unsigned long SystemTools::FileLength(const char* filename) } #else struct stat fs; - if (stat(filename, &fs) == 0) + if (stat(filename.c_str(), &fs) == 0) { length = static_cast(fs.st_size); } @@ -2663,7 +2699,7 @@ size_t SystemTools::GetMaximumFilePathLength() * found. Otherwise, the empty string is returned. */ kwsys_stl::string SystemTools -::FindName(const char* name, +::FindName(const kwsys_stl::string& name, const kwsys_stl::vector& userPaths, bool no_system_path) { @@ -2716,7 +2752,7 @@ kwsys_stl::string SystemTools * found. Otherwise, the empty string is returned. */ kwsys_stl::string SystemTools -::FindFile(const char* name, +::FindFile(const kwsys_stl::string& name, const kwsys_stl::vector& userPaths, bool no_system_path) { @@ -2735,7 +2771,7 @@ kwsys_stl::string SystemTools * found. Otherwise, the empty string is returned. */ kwsys_stl::string SystemTools -::FindDirectory(const char* name, +::FindDirectory(const kwsys_stl::string& name, const kwsys_stl::vector& userPaths, bool no_system_path) { @@ -3078,29 +3114,29 @@ bool SystemTools::FileIsSymlink(const kwsys_stl::string& name) } #if defined(_WIN32) && !defined(__CYGWIN__) -bool SystemTools::CreateSymlink(const char*, const char*) +bool SystemTools::CreateSymlink(const kwsys_stl::string&, const kwsys_stl::string&) { return false; } #else -bool SystemTools::CreateSymlink(const char* origName, const char* newName) +bool SystemTools::CreateSymlink(const kwsys_stl::string& origName, const kwsys_stl::string& newName) { - return symlink(origName, newName) >= 0; + return symlink(origName.c_str(), newName.c_str()) >= 0; } #endif #if defined(_WIN32) && !defined(__CYGWIN__) -bool SystemTools::ReadSymlink(const char*, kwsys_stl::string&) +bool SystemTools::ReadSymlink(const kwsys_stl::string&, kwsys_stl::string&) { return false; } #else -bool SystemTools::ReadSymlink(const char* newName, +bool SystemTools::ReadSymlink(const kwsys_stl::string& newName, kwsys_stl::string& origName) { char buf[KWSYS_SYSTEMTOOLS_MAXPATH+1]; int count = - static_cast(readlink(newName, buf, KWSYS_SYSTEMTOOLS_MAXPATH)); + static_cast(readlink(newName.c_str(), buf, KWSYS_SYSTEMTOOLS_MAXPATH)); if(count >= 0) { // Add null-terminator. @@ -3136,14 +3172,14 @@ kwsys_stl::string SystemTools::GetCurrentWorkingDirectory(bool collapse) return path; } -kwsys_stl::string SystemTools::GetProgramPath(const char* in_name) +kwsys_stl::string SystemTools::GetProgramPath(const kwsys_stl::string& in_name) { kwsys_stl::string dir, file; SystemTools::SplitProgramPath(in_name, dir, file); return dir; } -bool SystemTools::SplitProgramPath(const char* in_name, +bool SystemTools::SplitProgramPath(const kwsys_stl::string& in_name, kwsys_stl::string& dir, kwsys_stl::string& file, bool) @@ -3409,7 +3445,62 @@ kwsys_stl::string SystemTools::CollapseFullPath(const kwsys_stl::string& in_path SystemTools::CheckTranslationPath(newPath); #ifdef _WIN32 - newPath = SystemTools::GetActualCaseForPath(newPath.c_str()); + newPath = SystemTools::GetActualCaseForPath(newPath); + SystemTools::ConvertToUnixSlashes(newPath); +#endif + // Return the reconstructed path. + return newPath; +} + +kwsys_stl::string SystemTools::CollapseFullPath(const kwsys_stl::string& in_path, + const kwsys_stl::string& in_base) +{ + // Collect the output path components. + kwsys_stl::vector out_components; + + // Split the input path components. + kwsys_stl::vector path_components; + SystemTools::SplitPath(in_path, path_components); + + // If the input path is relative, start with a base path. + if(path_components[0].length() == 0) + { + kwsys_stl::vector base_components; + // Use the given base path. + SystemTools::SplitPath(in_base, base_components); + + // Append base path components to the output path. + out_components.push_back(base_components[0]); + SystemToolsAppendComponents(out_components, + base_components.begin()+1, + base_components.end()); + } + + // Append input path components to the output path. + SystemToolsAppendComponents(out_components, + path_components.begin(), + path_components.end()); + + // Transform the path back to a string. + kwsys_stl::string newPath = SystemTools::JoinPath(out_components); + + // Update the translation table with this potentially new path. I am not + // sure why this line is here, it seems really questionable, but yet I + // would put good money that if I remove it something will break, basically + // from what I can see it created a mapping from the collapsed path, to be + // replaced by the input path, which almost completely does the opposite of + // this function, the only thing preventing this from happening a lot is + // that if the in_path has a .. in it, then it is not added to the + // translation table. So for most calls this either does nothing due to the + // .. or it adds a translation between identical paths as nothing was + // collapsed, so I am going to try to comment it out, and see what hits the + // fan, hopefully quickly. + // Commented out line below: + //SystemTools::AddTranslationPath(newPath, in_path); + + SystemTools::CheckTranslationPath(newPath); +#ifdef _WIN32 + newPath = SystemTools::GetActualCaseForPath(newPath); SystemTools::ConvertToUnixSlashes(newPath); #endif // Return the reconstructed path. @@ -3569,7 +3660,7 @@ static int GetCasePathName(const kwsys_stl::string & pathIn, //---------------------------------------------------------------------------- -kwsys_stl::string SystemTools::GetActualCaseForPath(const char* p) +kwsys_stl::string SystemTools::GetActualCaseForPath(const kwsys_stl::string& p) { #ifndef _WIN32 return p; @@ -3930,7 +4021,7 @@ kwsys_stl::string SystemTools::GetFilenameName(const kwsys_stl::string& filename kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& filename) { kwsys_stl::string name = SystemTools::GetFilenameName(filename); - kwsys_stl::string::size_type dot_pos = name.find("."); + kwsys_stl::string::size_type dot_pos = name.find('.'); if(dot_pos != kwsys_stl::string::npos) { return name.substr(dot_pos); @@ -3948,7 +4039,7 @@ kwsys_stl::string SystemTools::GetFilenameExtension(const kwsys_stl::string& fil kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& filename) { kwsys_stl::string name = SystemTools::GetFilenameName(filename); - kwsys_stl::string::size_type dot_pos = name.rfind("."); + kwsys_stl::string::size_type dot_pos = name.rfind('.'); if(dot_pos != kwsys_stl::string::npos) { return name.substr(dot_pos); @@ -3966,7 +4057,7 @@ kwsys_stl::string SystemTools::GetFilenameLastExtension(const kwsys_stl::string& kwsys_stl::string SystemTools::GetFilenameWithoutExtension(const kwsys_stl::string& filename) { kwsys_stl::string name = SystemTools::GetFilenameName(filename); - kwsys_stl::string::size_type dot_pos = name.find("."); + kwsys_stl::string::size_type dot_pos = name.find('.'); if(dot_pos != kwsys_stl::string::npos) { return name.substr(0, dot_pos); @@ -3987,7 +4078,7 @@ kwsys_stl::string SystemTools::GetFilenameWithoutLastExtension(const kwsys_stl::string& filename) { kwsys_stl::string name = SystemTools::GetFilenameName(filename); - kwsys_stl::string::size_type dot_pos = name.rfind("."); + kwsys_stl::string::size_type dot_pos = name.rfind('.'); if(dot_pos != kwsys_stl::string::npos) { return name.substr(0, dot_pos); @@ -4276,7 +4367,7 @@ bool SystemTools::GetShortPath(const kwsys_stl::string& path, kwsys_stl::string& #endif } -void SystemTools::SplitProgramFromArgs(const char* path, +void SystemTools::SplitProgramFromArgs(const kwsys_stl::string& path, kwsys_stl::string& program, kwsys_stl::string& args) { // see if this is a full path to a program @@ -4352,7 +4443,7 @@ kwsys_stl::string SystemTools::GetCurrentDateTime(const char* format) return kwsys_stl::string(buf); } -kwsys_stl::string SystemTools::MakeCidentifier(const char* s) +kwsys_stl::string SystemTools::MakeCidentifier(const kwsys_stl::string& s) { kwsys_stl::string str(s); if (str.find_first_of("0123456789") == 0) diff --git a/SystemTools.hxx.in b/SystemTools.hxx.in index e88bc8f3c..beb2a7e12 100644 --- a/SystemTools.hxx.in +++ b/SystemTools.hxx.in @@ -89,9 +89,9 @@ public: * then an underscore is prepended. Note that this can produce * identifiers that the standard reserves (_[A-Z].* and __.*). */ - static kwsys_stl::string MakeCidentifier(const char* s); + static kwsys_stl::string MakeCidentifier(const kwsys_stl::string& s); - static kwsys_stl::string MakeCindentifier(const char* s) + static kwsys_stl::string MakeCindentifier(const kwsys_stl::string& s) { return MakeCidentifier(s); } @@ -102,6 +102,9 @@ public: static void ReplaceString(kwsys_stl::string& source, const char* replace, const char* with); + static void ReplaceString(kwsys_stl::string& source, + const kwsys_stl::string& replace, + const kwsys_stl::string& with); /** * Return a capitalized string (i.e the first letter is uppercased, @@ -306,7 +309,7 @@ public: /** * Return file length */ - static unsigned long FileLength(const char *filename); + static unsigned long FileLength(const kwsys_stl::string& filename); /** Change the modification time or create a file @@ -335,15 +338,15 @@ public: * does not exist path is returned unchanged. This does nothing * on unix but return path. */ - static kwsys_stl::string GetActualCaseForPath(const char* path); + static kwsys_stl::string GetActualCaseForPath(const kwsys_stl::string& path); /** * Given the path to a program executable, get the directory part of * the path with the file stripped off. If there is no directory * part, the empty string is returned. */ - static kwsys_stl::string GetProgramPath(const char*); - static bool SplitProgramPath(const char* in_name, + static kwsys_stl::string GetProgramPath(const kwsys_stl::string&); + static bool SplitProgramPath(const kwsys_stl::string& in_name, kwsys_stl::string& dir, kwsys_stl::string& file, bool errorReport = true); @@ -376,6 +379,8 @@ public: static kwsys_stl::string CollapseFullPath(const kwsys_stl::string& in_relative); static kwsys_stl::string CollapseFullPath(const kwsys_stl::string& in_relative, const char* in_base); + static kwsys_stl::string CollapseFullPath(const kwsys_stl::string& in_relative, + const kwsys_stl::string& in_base); /** * Get the real path for a given path, removing all symlinks. In @@ -446,7 +451,7 @@ public: * Split a program from its arguments and handle spaces in the paths */ static void SplitProgramFromArgs( - const char* path, + const kwsys_stl::string& path, kwsys_stl::string& program, kwsys_stl::string& args); /** @@ -582,7 +587,7 @@ public: * Find a file in the system PATH, with optional extra paths */ static kwsys_stl::string FindFile( - const char* name, + const kwsys_stl::string& name, const kwsys_stl::vector& path = kwsys_stl::vector(), bool no_system_path = false); @@ -591,7 +596,7 @@ public: * Find a directory in the system PATH, with optional extra paths */ static kwsys_stl::string FindDirectory( - const char* name, + const kwsys_stl::string& name, const kwsys_stl::vector& path = kwsys_stl::vector(), bool no_system_path = false); @@ -662,13 +667,13 @@ public: * Create a symbolic link if the platform supports it. Returns whether * creation succeded. */ - static bool CreateSymlink(const char* origName, const char* newName); + static bool CreateSymlink(const kwsys_stl::string& origName, const kwsys_stl::string& newName); /** * Read the contents of a symbolic link. Returns whether reading * succeded. */ - static bool ReadSymlink(const char* newName, kwsys_stl::string& origName); + static bool ReadSymlink(const kwsys_stl::string& newName, kwsys_stl::string& origName); /** * Try to locate the file 'filename' in the directory 'dir'. @@ -750,26 +755,26 @@ public: /** * Get a list of subkeys. */ - static bool GetRegistrySubKeys(const char *key, + static bool GetRegistrySubKeys(const kwsys_stl::string& key, kwsys_stl::vector& subkeys, KeyWOW64 view = KeyWOW64_Default); /** * Read a registry value */ - static bool ReadRegistryValue(const char *key, kwsys_stl::string &value, + static bool ReadRegistryValue(const kwsys_stl::string& key, kwsys_stl::string &value, KeyWOW64 view = KeyWOW64_Default); /** * Write a registry value */ - static bool WriteRegistryValue(const char *key, const char *value, + static bool WriteRegistryValue(const kwsys_stl::string& key, const kwsys_stl::string& value, KeyWOW64 view = KeyWOW64_Default); /** * Delete a registry value */ - static bool DeleteRegistryValue(const char *key, + static bool DeleteRegistryValue(const kwsys_stl::string& key, KeyWOW64 view = KeyWOW64_Default); /** ----------------------------------------------------------------- @@ -789,15 +794,17 @@ public: * Read an environment variable */ static const char* GetEnv(const char* key); + static const char* GetEnv(const kwsys_stl::string& key); static bool GetEnv(const char* key, kwsys_stl::string& result); + static bool GetEnv(const kwsys_stl::string& key, kwsys_stl::string& result); /** Put a string into the environment of the form var=value */ - static bool PutEnv(const char* env); + static bool PutEnv(const kwsys_stl::string& env); /** Remove a string from the environment. Input is of the form "var" or "var=value" (value is ignored). */ - static bool UnPutEnv(const char* env); + static bool UnPutEnv(const kwsys_stl::string& env); /** * Get current working directory CWD @@ -905,6 +912,14 @@ private: return &SystemToolsManagerInstance; } + /** + * Actual implementation of ReplaceString. + */ + static void ReplaceString(kwsys_stl::string& source, + const char* replace, + size_t replaceSize, + const kwsys_stl::string& with); + /** * Actual implementation of FileIsFullPath. */ @@ -915,7 +930,7 @@ private: * optional extra paths. */ static kwsys_stl::string FindName( - const char* name, + const kwsys_stl::string& name, const kwsys_stl::vector& path = kwsys_stl::vector(), bool no_system_path = false); diff --git a/testSystemTools.cxx b/testSystemTools.cxx index b41532b11..42b62497b 100644 --- a/testSystemTools.cxx +++ b/testSystemTools.cxx @@ -124,7 +124,7 @@ static bool CheckFileOperations() res = false; } - if (kwsys::SystemTools::FileLength(testBinFile.c_str()) != 766) + if (kwsys::SystemTools::FileLength(testBinFile) != 766) { kwsys_ios::cerr << "Problem with FileLength - incorrect length for: " @@ -512,7 +512,7 @@ static bool CheckStringOperations() //---------------------------------------------------------------------------- -static bool CheckPutEnv(const char* env, const char* name, const char* value) +static bool CheckPutEnv(const kwsys_stl::string& env, const char* name, const char* value) { if(!kwsys::SystemTools::PutEnv(env)) {