diff options
author | Bernard Normier <bernard@zeroc.com> | 2016-08-01 10:09:48 -0400 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2016-08-01 10:09:48 -0400 |
commit | 4aa7ae5129a9cb66f5ea26165310e96603af576c (patch) | |
tree | f8ee6859ad547b59cc28eadb1212f403f51dfff7 /cpp/src/IceUtil | |
parent | Do not initialize OpenSSL locks if already done by other library. (diff) | |
download | ice-4aa7ae5129a9cb66f5ea26165310e96603af576c.tar.bz2 ice-4aa7ae5129a9cb66f5ea26165310e96603af576c.tar.xz ice-4aa7ae5129a9cb66f5ea26165310e96603af576c.zip |
Move StringConverter API to namespace Ice
Diffstat (limited to 'cpp/src/IceUtil')
-rw-r--r-- | cpp/src/IceUtil/FileUtil.cpp | 36 | ||||
-rw-r--r-- | cpp/src/IceUtil/StringConverter.cpp | 132 | ||||
-rw-r--r-- | cpp/src/IceUtil/UtilException.cpp | 45 |
3 files changed, 20 insertions, 193 deletions
diff --git a/cpp/src/IceUtil/FileUtil.cpp b/cpp/src/IceUtil/FileUtil.cpp index 0bb94620137..9da2b9bd3c5 100644 --- a/cpp/src/IceUtil/FileUtil.cpp +++ b/cpp/src/IceUtil/FileUtil.cpp @@ -101,7 +101,7 @@ bool IceUtilInternal::isEmptyDirectory(const string& path) { # ifdef _WIN32 - return PathIsDirectoryEmptyW(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); + return PathIsDirectoryEmptyW(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); # else struct dirent* d; DIR* dir = opendir(path.c_str()); @@ -154,8 +154,8 @@ IceUtilInternal::freopen(const std::string& path, const std::string& mode, FILE* // to Windows API. // const IceUtil::StringConverterPtr converter = IceUtil::getProcessStringConverter(); - return _wfreopen(IceUtil::stringToWstring(path, converter).c_str(), - IceUtil::stringToWstring(mode, converter).c_str(), stream); + return _wfreopen(stringToWstring(path, converter).c_str(), + stringToWstring(mode, converter).c_str(), stream); # else return freopen(path.c_str(), mode.c_str(), stream); # endif @@ -174,13 +174,13 @@ IceUtilInternal::stat(const string& path, structstat* buffer) // Don't need to use a wide string converter, the wide string is directly passed // to Windows API. // - return _wstat(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), buffer); + return _wstat(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), buffer); } int IceUtilInternal::remove(const string& path) { - return ::_wremove(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); + return ::_wremove(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); } int @@ -191,8 +191,8 @@ IceUtilInternal::rename(const string& from, const string& to) // to Windows API. // const IceUtil::StringConverterPtr converter = IceUtil::getProcessStringConverter(); - return ::_wrename(IceUtil::stringToWstring(from, converter).c_str(), - IceUtil::stringToWstring(to, converter).c_str()); + return ::_wrename(stringToWstring(from, converter).c_str(), + stringToWstring(to, converter).c_str()); } int @@ -202,7 +202,7 @@ IceUtilInternal::rmdir(const string& path) // Don't need to use a wide string converter, the wide string is directly passed // to Windows API. // - return ::_wrmdir(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); + return ::_wrmdir(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); } int @@ -212,7 +212,7 @@ IceUtilInternal::mkdir(const string& path, int) // Don't need to use a wide string converter, the wide string is directly passed // to Windows API. // - return ::_wmkdir(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); + return ::_wmkdir(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); } FILE* @@ -223,8 +223,8 @@ IceUtilInternal::fopen(const string& path, const string& mode) // to Windows API. // const IceUtil::StringConverterPtr converter = IceUtil::getProcessStringConverter(); - return ::_wfopen(IceUtil::stringToWstring(path, converter).c_str(), - IceUtil::stringToWstring(mode, converter).c_str()); + return ::_wfopen(stringToWstring(path, converter).c_str(), + stringToWstring(mode, converter).c_str()); } int @@ -236,12 +236,12 @@ IceUtilInternal::open(const string& path, int flags) // if(flags & _O_CREAT) { - return ::_wopen(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), + return ::_wopen(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), flags, _S_IREAD | _S_IWRITE); } else { - return ::_wopen(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), flags); + return ::_wopen(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), flags); } } @@ -258,7 +258,7 @@ IceUtilInternal::getcwd(string& cwd) { return -1; } - cwd = IceUtil::wstringToString(cwdbuf, IceUtil::getProcessStringConverter()); + cwd = wstringToString(cwdbuf, IceUtil::getProcessStringConverter()); return 0; } #endif @@ -270,7 +270,7 @@ IceUtilInternal::unlink(const string& path) // Don't need to use a wide string converter, the wide string is directly passed // to Windows API. // - return _wunlink(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); + return _wunlink(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str()); } int @@ -292,12 +292,12 @@ IceUtilInternal::FileLock::FileLock(const std::string& path) : // to Windows API. // #ifndef ICE_OS_WINRT - _fd = ::CreateFileW(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), + _fd = ::CreateFileW(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); #else CREATEFILE2_EXTENDED_PARAMETERS params; params.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; - _fd = ::CreateFile2(IceUtil::stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), + _fd = ::CreateFile2(stringToWstring(path, IceUtil::getProcessStringConverter()).c_str(), GENERIC_WRITE, 0, OPEN_ALWAYS, ¶ms); #endif _path = path; @@ -348,7 +348,7 @@ IceUtilInternal::FileLock::~FileLock() wstring IceUtilInternal::streamFilename(const string& filename) { - return IceUtil::stringToWstring(filename, IceUtil::getProcessStringConverter()); + return stringToWstring(filename, IceUtil::getProcessStringConverter()); } #endif diff --git a/cpp/src/IceUtil/StringConverter.cpp b/cpp/src/IceUtil/StringConverter.cpp index f122084eb68..dc029626c4f 100644 --- a/cpp/src/IceUtil/StringConverter.cpp +++ b/cpp/src/IceUtil/StringConverter.cpp @@ -238,27 +238,6 @@ public: #endif -#ifdef _WIN32 - -// -// Converts to/from UTF-8 using MultiByteToWideChar and WideCharToMultiByte -// -class WindowsStringConverter : public StringConverter -{ -public: - - explicit WindowsStringConverter(unsigned int); - - virtual Byte* toUTF8(const char*, const char*, UTF8Buffer&) const; - - virtual void fromUTF8(const Byte*, const Byte*, string& target) const; - -private: - unsigned int _cp; -}; -#endif - - class Init { public: @@ -324,108 +303,6 @@ private: string _buffer; }; -#ifdef _WIN32 -WindowsStringConverter::WindowsStringConverter(unsigned int cp) : - _cp(cp) -{ -} - -Byte* -WindowsStringConverter::toUTF8(const char* sourceStart, const char* sourceEnd, UTF8Buffer& buffer) const -{ - // - // First convert to UTF-16 - // - int sourceSize = static_cast<int>(sourceEnd - sourceStart); - if(sourceSize == 0) - { - return buffer.getMoreBytes(1, 0); - } - - int writtenWchar = 0; - wstring wbuffer; - - // - // The following code pages doesn't support MB_ERR_INVALID_CHARS flag - // see http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx - // - DWORD flags = - (_cp == 50220 || _cp == 50221 || _cp == 50222 || - _cp == 50225 || _cp == 50227 || _cp == 50229 || - _cp == 65000 || _cp == 42 || (_cp >= 57002 && _cp <= 57011)) ? 0 : MB_ERR_INVALID_CHARS; - - do - { - wbuffer.resize(wbuffer.size() == 0 ? sourceSize + 2 : 2 * wbuffer.size()); - writtenWchar = MultiByteToWideChar(_cp, flags, sourceStart, sourceSize, - const_cast<wchar_t*>(wbuffer.data()), static_cast<int>(wbuffer.size())); - } while(writtenWchar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER); - - if(writtenWchar == 0) - { - throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString()); - } - - wbuffer.resize(static_cast<size_t>(writtenWchar)); - - // - // Then convert this UTF-16 wbuffer into UTF-8 - // - return getUnicodeWstringConverter()->toUTF8(wbuffer.data(), wbuffer.data() + wbuffer.size(), buffer); -} - -void -WindowsStringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd, string& target) const -{ - if(sourceStart == sourceEnd) - { - target = ""; - return; - } - - if(_cp == CP_UTF8) - { - string tmp(reinterpret_cast<const char*>(sourceStart), sourceEnd - sourceStart); - tmp.swap(target); - return; - } - - // - // First convert to wstring (UTF-16) - // - wstring wtarget; - getUnicodeWstringConverter()->fromUTF8(sourceStart, sourceEnd, wtarget); - - // - // WC_ERR_INVALID_CHARS conversion flag is only supported with 65001 (UTF-8) and - // 54936 (GB18030 Simplified Chinese) - // - DWORD flags = (_cp == 65001 || _cp == 54936) ? WC_ERR_INVALID_CHARS : 0; - - // - // And then to a multi-byte narrow string - // - int writtenChar = -1; - do - { - target.resize(writtenChar == -1 ? - std::max<size_t>(sourceEnd - sourceStart + 2, target.size()) : - 2 * target.size()); - - writtenChar = WideCharToMultiByte(_cp, flags, wtarget.data(), static_cast<int>(wtarget.size()), - const_cast<char*>(target.data()), static_cast<int>(target.size()), - 0, 0); - } while(writtenChar == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER); - - if(writtenChar == 0) - { - throw IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString()); - } - - target.resize(static_cast<size_t>(writtenChar)); -} -#endif - } IceUtil::UTF8Buffer::~UTF8Buffer() @@ -439,15 +316,6 @@ IceUtil::createUnicodeWstringConverter() return getUnicodeWstringConverter(); } -#ifdef _WIN32 -StringConverterPtr -IceUtil::createWindowsStringConverter(unsigned int cp) -{ - return ICE_MAKE_SHARED(WindowsStringConverter, cp); -} -#endif - - StringConverterPtr IceUtil::getProcessStringConverter() { diff --git a/cpp/src/IceUtil/UtilException.cpp b/cpp/src/IceUtil/UtilException.cpp index 50b5b7ce0f6..46873339daa 100644 --- a/cpp/src/IceUtil/UtilException.cpp +++ b/cpp/src/IceUtil/UtilException.cpp @@ -473,7 +473,7 @@ getStackTrace(const vector<void*>& stackFrames) if(ok) { #ifdef DBGHELP_TRANSLATE_TCHAR - s << IceUtil::wstringToString(symbol->Name, converter); + s << wstringToString(symbol->Name, converter); #else s << symbol->Name; #endif @@ -482,7 +482,7 @@ getStackTrace(const vector<void*>& stackFrames) { s << " at " #ifdef DBGHELP_TRANSLATE_TCHAR - << IceUtil::wstringToString(line.FileName, converter) + << wstringToString(line.FileName, converter) #else << line.FileName #endif @@ -885,44 +885,3 @@ IceUtil::OptionalNotSetException::ice_clone() const return new OptionalNotSetException(*this); } #endif - -#ifndef _WIN32 -IceUtil::IconvInitializationException::IconvInitializationException(const char* file, int line, const string& reason) : - ExceptionHelper<IconvInitializationException>(file, line), - _reason(reason) -{ -} - -#ifndef ICE_CPP11_COMPILER -IceUtil::IconvInitializationException::~IconvInitializationException() throw() -{ -} -#endif - -void -IceUtil::IconvInitializationException::ice_print(ostream& out) const -{ - Exception::ice_print(out); - out << ": " << _reason; -} - -string -IceUtil::IconvInitializationException::ice_id() const -{ - return "::IceUtil::IconvInitializationException"; -} - -#ifndef ICE_CPP11_MAPPING -IceUtil::IconvInitializationException* -IceUtil::IconvInitializationException::ice_clone() const -{ - return new IconvInitializationException(*this); -} -#endif - -string -IceUtil::IconvInitializationException::reason() const -{ - return _reason; -} -#endif |