diff options
author | Bernard Normier <bernard@zeroc.com> | 2014-05-16 12:30:43 -0400 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2014-05-16 12:30:43 -0400 |
commit | 3f3450439b7558443a58279980aadaf069f5a086 (patch) | |
tree | e3d5a066653b410e668b0f71f5cfde42e07177c4 /cpp | |
parent | Fixed build (diff) | |
download | ice-3f3450439b7558443a58279980aadaf069f5a086.tar.bz2 ice-3f3450439b7558443a58279980aadaf069f5a086.tar.xz ice-3f3450439b7558443a58279980aadaf069f5a086.zip |
Updated nativeToUTF8 and UTF8ToNative for consistency with stringToWstring/wstringToString
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/Ice/Ice.h | 3 | ||||
-rw-r--r-- | cpp/include/IceUtil/StringConverter.h | 45 | ||||
-rw-r--r-- | cpp/include/IceUtil/Unicode.h | 49 | ||||
-rw-r--r-- | cpp/src/Freeze/EvictorI.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Freeze/IndexI.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Freeze/MapDb.cpp | 4 | ||||
-rw-r--r-- | cpp/src/Freeze/MapI.cpp | 4 | ||||
-rw-r--r-- | cpp/src/Freeze/ObjectStore.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Freeze/SharedDbEnv.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Ice/Instance.cpp | 8 | ||||
-rw-r--r-- | cpp/src/Ice/LoggerI.cpp | 6 | ||||
-rw-r--r-- | cpp/src/Ice/Makefile | 1 | ||||
-rw-r--r-- | cpp/src/Ice/Makefile.mak | 1 | ||||
-rw-r--r-- | cpp/src/Ice/PropertiesI.cpp | 4 | ||||
-rw-r--r-- | cpp/src/Ice/Reference.cpp | 4 | ||||
-rw-r--r-- | cpp/src/Ice/ReferenceFactory.cpp | 4 | ||||
-rw-r--r-- | cpp/src/Ice/ServantManager.cpp | 4 | ||||
-rw-r--r-- | cpp/src/IceUtil/StringConverter.cpp | 322 |
18 files changed, 209 insertions, 258 deletions
diff --git a/cpp/include/Ice/Ice.h b/cpp/include/Ice/Ice.h index d5eac1b3acb..627425777c6 100644 --- a/cpp/include/Ice/Ice.h +++ b/cpp/include/Ice/Ice.h @@ -10,7 +10,8 @@ #ifndef ICE_ICE_H #define ICE_ICE_H -#include <IceUtil/Config.h> +#include <Ice/Config.h> +#include <Ice/DeprecatedStringConverter.h> #include <Ice/Initialize.h> #include <Ice/LocalException.h> #include <Ice/PropertiesAdmin.h> diff --git a/cpp/include/IceUtil/StringConverter.h b/cpp/include/IceUtil/StringConverter.h index 8f82cbd984a..1849412427d 100644 --- a/cpp/include/IceUtil/StringConverter.h +++ b/cpp/include/IceUtil/StringConverter.h @@ -109,48 +109,27 @@ private: #endif // -// Retrive the per process narrow string converter. Access to the -// converter is protected by a static mutex. +// Retrieve the per process narrow string converter. // ICE_UTIL_API StringConverterPtr getProcessStringConverter(); // -// Set the per process narrow string converter. Access to the -// converter is protected by a static mutex. +// Set the per process narrow string converter. // ICE_UTIL_API void setProcessStringConverter(const StringConverterPtr&); // -// Retrive the per process wide string converter. Access to the -// converter is protected by a static mutex. +// Retrieve the per process wide string converter. // ICE_UTIL_API WstringConverterPtr getProcessWstringConverter(); // -// Set the per process wide string converter. Access to the -// converter is protected by a static mutex. +// Set the per process wide string converter. // ICE_UTIL_API void setProcessWstringConverter(const WstringConverterPtr&); // -// Converts the given string from the native narrow string encoding to -// UTF-8 using the given converter. If the converter is null, returns -// the given string. -// -ICE_UTIL_API std::string -nativeToUTF8(const StringConverterPtr&, const std::string&); - -// -// Converts the given string from UTF-88 to the native narrow string -// encoding using the given converter. If the converter is null, -// returns the given string. -// -ICE_UTIL_API std::string -UTF8ToNative(const StringConverterPtr&, const std::string&); - - -// // Converts the given wide string to a narrow string // // If the StringConverter parameter is null, the result's narrow @@ -175,6 +154,22 @@ stringToWstring(const std::string&, const StringConverterPtr& = 0, const WstringConverterPtr& = 0, ConversionFlags = lenientConversion); +// +// Converts the given string from the native narrow string encoding to +// UTF-8 using the given converter. If the converter is null, returns +// the given string. +// +ICE_UTIL_API std::string +nativeToUTF8(const std::string&, const StringConverterPtr&); + +// +// Converts the given string from UTF-8 to the native narrow string +// encoding using the given converter. If the converter is null, +// returns the given string. +// +ICE_UTIL_API std::string +UTF8ToNative(const std::string&, const StringConverterPtr&); + } #endif diff --git a/cpp/include/IceUtil/Unicode.h b/cpp/include/IceUtil/Unicode.h deleted file mode 100644 index 7108467fc16..00000000000 --- a/cpp/include/IceUtil/Unicode.h +++ /dev/null @@ -1,49 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -#ifndef ICE_UTIL_UNICODE_H -#define ICE_UTIL_UNICODE_H - -#include <IceUtil/Config.h> -#include <IceUtil/StringConverter.h> - -namespace IceUtilInternal -{ - -// -// Converts UTF-8 byte-sequences to and from UTF-16 or UTF-32 (with native -// endianness) depending on sizeof(wchar_t). -// -// These are thin wrappers over the UTF8/16/32 converters provided by -// unicode.org -// - -enum ConversionResult -{ - conversionOK, /* conversion successful */ - sourceExhausted, /* partial character in source, but hit end */ - targetExhausted, /* insuff. room in target for conversion */ - sourceIllegal /* source sequence is illegal/malformed */ -}; - -ConversionResult -convertUTFWstringToUTF8(const wchar_t*& sourceStart, const wchar_t* sourceEnd, - IceUtil::Byte*& targetStart, IceUtil::Byte* targetEnd, IceUtil::ConversionFlags flags); - -ConversionResult -convertUTF8ToUTFWstring(const IceUtil::Byte*& sourceStart, const IceUtil::Byte* sourceEnd, - wchar_t*& targetStart, wchar_t* targetEnd, IceUtil::ConversionFlags flags); - -ConversionResult -convertUTF8ToUTFWstring(const IceUtil::Byte*& sourceStart, const IceUtil::Byte* sourceEnd, - std::wstring& target, IceUtil::ConversionFlags flags); - -} - -#endif diff --git a/cpp/src/Freeze/EvictorI.cpp b/cpp/src/Freeze/EvictorI.cpp index c7caa0b9f1d..626fbd47231 100644 --- a/cpp/src/Freeze/EvictorI.cpp +++ b/cpp/src/Freeze/EvictorI.cpp @@ -321,7 +321,7 @@ Freeze::EvictorIBase::allDbs() const // // Berkeley DB expects file paths to be UTF8 encoded. // - db.open(0, nativeToUTF8(IceUtil::getProcessStringConverter(), _filename).c_str(), 0, DB_UNKNOWN, + db.open(0, nativeToUTF8(_filename, IceUtil::getProcessStringConverter()).c_str(), 0, DB_UNKNOWN, DB_RDONLY, 0); Dbc* dbc = 0; diff --git a/cpp/src/Freeze/IndexI.cpp b/cpp/src/Freeze/IndexI.cpp index 21231e707ca..9b1cb2c9b7f 100644 --- a/cpp/src/Freeze/IndexI.cpp +++ b/cpp/src/Freeze/IndexI.cpp @@ -387,7 +387,7 @@ Freeze::IndexI::associate(ObjectStoreBase* store, DbTxn* txn, // with deployed databases. // _db->open(txn, - IceUtil::nativeToUTF8(IceUtil::getProcessStringConverter(), store->evictor()->filename()).c_str(), + IceUtil::nativeToUTF8(store->evictor()->filename(), IceUtil::getProcessStringConverter()).c_str(), _dbName.c_str(), DB_BTREE, flags, FREEZE_DB_MODE); flags = 0; diff --git a/cpp/src/Freeze/MapDb.cpp b/cpp/src/Freeze/MapDb.cpp index cb06a456596..897813d4148 100644 --- a/cpp/src/Freeze/MapDb.cpp +++ b/cpp/src/Freeze/MapDb.cpp @@ -188,7 +188,7 @@ Freeze::MapDb::MapDb(const ConnectionIPtr& connection, // // Berkeley DB expects file paths to be UTF8 encoded. // - open(txn, nativeToUTF8(getProcessStringConverter(), _dbName).c_str(), 0, DB_BTREE, + open(txn, nativeToUTF8(_dbName, getProcessStringConverter()).c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); StringSeq oldIndices; @@ -442,7 +442,7 @@ Freeze::MapDb::MapDb(const Ice::CommunicatorPtr& communicator, // // Berkeley DB expects file paths to be UTF8 encoded. // - open(0, nativeToUTF8(getProcessStringConverter(), _dbName).c_str(), 0, DB_BTREE, flags, + open(0, nativeToUTF8(_dbName, getProcessStringConverter()).c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); } catch(const ::DbException& dx) diff --git a/cpp/src/Freeze/MapI.cpp b/cpp/src/Freeze/MapI.cpp index 15e24028837..effaa167982 100644 --- a/cpp/src/Freeze/MapI.cpp +++ b/cpp/src/Freeze/MapI.cpp @@ -205,7 +205,7 @@ Freeze::MapHelper::recreate(const Freeze::ConnectionPtr& connection, // Berkeley DB expects file paths to be UTF8 encoded. // oldDb.open(txn, - IceUtil::nativeToUTF8(IceUtil::getProcessStringConverter(), oldDbName).c_str(), + IceUtil::nativeToUTF8(oldDbName, IceUtil::getProcessStringConverter()).c_str(), 0, DB_BTREE, DB_THREAD, FREEZE_DB_MODE); IceUtil::UniquePtr<MapDb> newDb(new MapDb(connectionI, dbName, key, value, keyCompare, indices, true)); @@ -1780,7 +1780,7 @@ Freeze::MapIndexI::MapIndexI(const ConnectionIPtr& connection, MapDb& db, // Berkeley DB expects file paths to be UTF8 encoded. // _db->open(txn, - IceUtil::nativeToUTF8(IceUtil::getProcessStringConverter(), _dbName).c_str(), + IceUtil::nativeToUTF8(_dbName, IceUtil::getProcessStringConverter()).c_str(), 0, DB_BTREE, flags, FREEZE_DB_MODE); // diff --git a/cpp/src/Freeze/ObjectStore.cpp b/cpp/src/Freeze/ObjectStore.cpp index 90756adba5e..1eaea6e2c92 100644 --- a/cpp/src/Freeze/ObjectStore.cpp +++ b/cpp/src/Freeze/ObjectStore.cpp @@ -139,7 +139,7 @@ Freeze::ObjectStoreBase::ObjectStoreBase(const string& facet, const string& face // with deployed databases. // _db->open(txn, - IceUtil::nativeToUTF8(IceUtil::getProcessStringConverter(), evictor->filename()).c_str(), + IceUtil::nativeToUTF8(evictor->filename(), IceUtil::getProcessStringConverter()).c_str(), _dbName.c_str(), DB_BTREE, flags, FREEZE_DB_MODE); for(size_t i = 0; i < _indices.size(); ++i) diff --git a/cpp/src/Freeze/SharedDbEnv.cpp b/cpp/src/Freeze/SharedDbEnv.cpp index 394d03b850d..85bd743c290 100644 --- a/cpp/src/Freeze/SharedDbEnv.cpp +++ b/cpp/src/Freeze/SharedDbEnv.cpp @@ -594,7 +594,7 @@ Freeze::SharedDbEnv::SharedDbEnv(const std::string& envName, // // Berkeley DB expects file paths to be UTF8 encoded. // - _env->open(nativeToUTF8(getProcessStringConverter(), dbHome).c_str(), flags, FREEZE_DB_MODE); + _env->open(nativeToUTF8(dbHome, getProcessStringConverter()).c_str(), flags, FREEZE_DB_MODE); // // Default checkpoint period is every 120 seconds diff --git a/cpp/src/Ice/Instance.cpp b/cpp/src/Ice/Instance.cpp index 20744535880..aa9f8247e3b 100644 --- a/cpp/src/Ice/Instance.cpp +++ b/cpp/src/Ice/Instance.cpp @@ -513,8 +513,8 @@ IceInternal::Instance::stringToIdentity(const string& s) const } } - ident.name = UTF8ToNative(_stringConverter, ident.name); - ident.category = UTF8ToNative(_stringConverter, ident.category); + ident.name = UTF8ToNative(ident.name, _stringConverter); + ident.category = UTF8ToNative(ident.category, _stringConverter); return ident; } @@ -526,8 +526,8 @@ IceInternal::Instance::identityToString(const Identity& ident) const // This method returns the stringified identity. The returned string only // contains printable ascii. It can contain UTF8 in the escaped form. // - string name = nativeToUTF8(_stringConverter, ident.name); - string category = nativeToUTF8(_stringConverter, ident.category); + string name = nativeToUTF8(ident.name, _stringConverter); + string category = nativeToUTF8(ident.category, _stringConverter); if(category.empty()) { diff --git a/cpp/src/Ice/LoggerI.cpp b/cpp/src/Ice/LoggerI.cpp index f0215401657..430d9fd49f6 100644 --- a/cpp/src/Ice/LoggerI.cpp +++ b/cpp/src/Ice/LoggerI.cpp @@ -154,9 +154,9 @@ Ice::LoggerI::write(const string& message, bool indent) // // Use fprintf_s to avoid encoding conversion when stderr is connected // to Windows console. When _convert is set to false we always output - // UTF8 encoded messages. + // UTF-8 encoded messages. // - fprintf_s(stderr, "%s\n", IceUtil::nativeToUTF8(_converter, s).c_str()); + fprintf_s(stderr, "%s\n", IceUtil::nativeToUTF8(s, _converter).c_str()); fflush(stderr); } else @@ -164,7 +164,7 @@ Ice::LoggerI::write(const string& message, bool indent) try { // Convert message to UTF-8 - string u8s = IceUtil::nativeToUTF8(_converter, s); + string u8s = IceUtil::nativeToUTF8(s, _converter); // Then from UTF-8 to console CP string consoleString; diff --git a/cpp/src/Ice/Makefile b/cpp/src/Ice/Makefile index 8e2835b9545..a3f34cbb0c5 100644 --- a/cpp/src/Ice/Makefile +++ b/cpp/src/Ice/Makefile @@ -31,6 +31,7 @@ OBJS = Acceptor.o \ Connector.o \ Current.o \ DefaultsAndOverrides.o \ + DeprecatedStringConverter.o \ Direct.o \ DispatchInterceptor.o \ DynamicLibrary.o \ diff --git a/cpp/src/Ice/Makefile.mak b/cpp/src/Ice/Makefile.mak index f20352780c7..ad6e5e9afb5 100644 --- a/cpp/src/Ice/Makefile.mak +++ b/cpp/src/Ice/Makefile.mak @@ -31,6 +31,7 @@ OBJS = Acceptor.obj \ ConnectionRequestHandler.obj \ Current.obj \ DefaultsAndOverrides.obj \ + DeprecatedStringConverter.obj \ Direct.obj \ DispatchInterceptor.obj \ DLLMain.obj \ diff --git a/cpp/src/Ice/PropertiesI.cpp b/cpp/src/Ice/PropertiesI.cpp index b466a2cf105..121e6ed8889 100644 --- a/cpp/src/Ice/PropertiesI.cpp +++ b/cpp/src/Ice/PropertiesI.cpp @@ -702,8 +702,8 @@ Ice::PropertiesI::parseLine(const string& line, const IceUtil::StringConverterPt return; } - key = IceUtil::UTF8ToNative(converter, key); - value = IceUtil::UTF8ToNative(converter, value); + key = IceUtil::UTF8ToNative(key, converter); + value = IceUtil::UTF8ToNative(value, converter); setProperty(key, value); } diff --git a/cpp/src/Ice/Reference.cpp b/cpp/src/Ice/Reference.cpp index be40f3f0df9..a80de29ed09 100644 --- a/cpp/src/Ice/Reference.cpp +++ b/cpp/src/Ice/Reference.cpp @@ -238,7 +238,7 @@ IceInternal::Reference::toString() const // the reference parser uses as separators, then we enclose // the facet string in quotes. // - string fs = nativeToUTF8(_instance->getStringConverter(), _facet); + string fs = nativeToUTF8(_facet, _instance->getStringConverter()); fs = IceUtilInternal::escapeString(fs, ""); if(fs.find_first_of(" :@") != string::npos) { @@ -1207,7 +1207,7 @@ IceInternal::RoutableReference::toString() const // reference parser uses as separators, then we enclose the // adapter id string in quotes. // - string a = nativeToUTF8(getInstance()->getStringConverter(), _adapterId); + string a = nativeToUTF8(_adapterId, getInstance()->getStringConverter()); a = IceUtilInternal::escapeString(a, ""); if(a.find_first_of(" :@") != string::npos) { diff --git a/cpp/src/Ice/ReferenceFactory.cpp b/cpp/src/Ice/ReferenceFactory.cpp index a96fd7630cd..37c061a4296 100644 --- a/cpp/src/Ice/ReferenceFactory.cpp +++ b/cpp/src/Ice/ReferenceFactory.cpp @@ -288,7 +288,7 @@ IceInternal::ReferenceFactory::create(const string& str, const string& propertyP throw ex; } - facet = UTF8ToNative(_instance->getStringConverter(), facet); + facet = UTF8ToNative(facet, _instance->getStringConverter()); break; } @@ -569,7 +569,7 @@ IceInternal::ReferenceFactory::create(const string& str, const string& propertyP throw ex; } - adapter = UTF8ToNative(_instance->getStringConverter(), adapter); + adapter = UTF8ToNative(adapter, _instance->getStringConverter()); return create(ident, facet, mode, secure, protocol, encoding, endpoints, adapter, propertyPrefix); break; diff --git a/cpp/src/Ice/ServantManager.cpp b/cpp/src/Ice/ServantManager.cpp index 45f9e8dbb76..e4e50a7375f 100644 --- a/cpp/src/Ice/ServantManager.cpp +++ b/cpp/src/Ice/ServantManager.cpp @@ -47,7 +47,7 @@ IceInternal::ServantManager::addServant(const ObjectPtr& object, const Identity& ex.id = _instance->identityToString(ident); if(!facet.empty()) { - string fs = nativeToUTF8(_instance->getStringConverter(), facet); + string fs = nativeToUTF8(facet, _instance->getStringConverter()); ex.id += " -f " + IceUtilInternal::escapeString(fs, ""); } throw ex; @@ -107,7 +107,7 @@ IceInternal::ServantManager::removeServant(const Identity& ident, const string& ex.id = _instance->identityToString(ident); if(!facet.empty()) { - string fs = nativeToUTF8(_instance->getStringConverter(), facet); + string fs = nativeToUTF8(facet, _instance->getStringConverter()); ex.id += " -f " + IceUtilInternal::escapeString(fs, ""); } throw ex; diff --git a/cpp/src/IceUtil/StringConverter.cpp b/cpp/src/IceUtil/StringConverter.cpp index c76a636ac17..1a6fe6e8405 100644 --- a/cpp/src/IceUtil/StringConverter.cpp +++ b/cpp/src/IceUtil/StringConverter.cpp @@ -37,7 +37,6 @@ Init init; } - namespace { @@ -96,166 +95,7 @@ private: } -StringConverterPtr -IceUtil::getProcessStringConverter() -{ - IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); - return processStringConverter; -} - -void -IceUtil::setProcessStringConverter(const StringConverterPtr& converter) -{ - IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); - processStringConverter = converter; -} - -WstringConverterPtr -IceUtil::getProcessWstringConverter() -{ - IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); - return processWstringConverter; -} - -void -IceUtil::setProcessWstringConverter(const WstringConverterPtr& converter) -{ - IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); - processWstringConverter = converter; -} - -string -IceUtil::nativeToUTF8(const IceUtil::StringConverterPtr& converter, const string& str) -{ - if(!converter || str.empty()) - { - return str; - } - UTF8BufferI buffer; - Byte* last = converter->toUTF8(str.data(), str.data() + str.size(), buffer); - return string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); -} - -string -IceUtil::UTF8ToNative(const IceUtil::StringConverterPtr& converter, const string& str) -{ - if(!converter || str.empty()) - { - return str; - } - string tmp; - converter->fromUTF8(reinterpret_cast<const Byte*>(str.data()), - reinterpret_cast<const Byte*>(str.data() + str.size()), tmp); - return tmp; -} - -string -IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, - IceUtil::ConversionFlags flags) -{ - string target; - if(!v.empty()) - { - // - // First convert to UTF8 narrow string. - // - if(wConverter) - { - UTF8BufferI buffer; - Byte* last = wConverter->toUTF8(v.data(), v.data() + v.size(), buffer); - target = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); - } - else - { - size_t size = v.size() * 4 * sizeof(char); - Byte* outBuf = new Byte[size]; - Byte* targetStart = outBuf; - Byte* targetEnd = outBuf + size; - - const wchar_t* sourceStart = v.data(); - - ConversionResult cr = - convertUTFWstringToUTF8( - sourceStart, sourceStart + v.size(), - targetStart, targetEnd, flags); - - if(cr != conversionOK) - { - delete[] outBuf; - assert(cr == sourceExhausted || cr == sourceIllegal); - throw IllegalConversionException(__FILE__, __LINE__, - cr == sourceExhausted ? "partial character" : "bad encoding"); - } - - target = string(reinterpret_cast<char*>(outBuf), static_cast<size_t>(targetStart - outBuf)); - delete[] outBuf; - } - - // - // If narrow string converter is present convert to the native narrow string encoding, otherwise - // native narrow string encoding is UTF8 and we are done. - // - if(converter) - { - string tmp; - converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()), - reinterpret_cast<const Byte*>(target.data() + target.size()), tmp); - tmp.swap(target); - } - } - return target; -} - -wstring -IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter, - const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) -{ - wstring target; - if(!v.empty()) - { - // - // If there is a narrow string converter use it to convert to UTF8, otherwise the narrow - // string is already UTF8 encoded. - // - string tmp; - if(converter) - { - UTF8BufferI buffer; - Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer); - tmp = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); - } - else - { - tmp = v; - } - - // - // If there is a wide string converter use fromUTF8 to convert to the wide string native encoding. - // - if(wConverter) - { - wConverter->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()), - reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target); - } - else - { - const Byte* sourceStart = reinterpret_cast<const Byte*>(tmp.data()); - - ConversionResult cr = - convertUTF8ToUTFWstring(sourceStart, sourceStart + tmp.size(), target, flags); - - if(cr != conversionOK) - { - assert(cr == sourceExhausted || cr == sourceIllegal); - - throw IllegalConversionException(__FILE__, __LINE__, - cr == sourceExhausted ? "partial character" : "bad encoding"); - } - } - } - return target; -} UnicodeWstringConverter::UnicodeWstringConverter(ConversionFlags flags) : _conversionFlags(flags) @@ -438,3 +278,165 @@ WindowsStringConverter::fromUTF8(const Byte* sourceStart, const Byte* sourceEnd, } #endif + + +StringConverterPtr +IceUtil::getProcessStringConverter() +{ + IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); + return processStringConverter; +} + +void +IceUtil::setProcessStringConverter(const StringConverterPtr& converter) +{ + IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); + processStringConverter = converter; +} + +WstringConverterPtr +IceUtil::getProcessWstringConverter() +{ + IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); + return processWstringConverter; +} + +void +IceUtil::setProcessWstringConverter(const WstringConverterPtr& converter) +{ + IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(processStringConverterMutex); + processWstringConverter = converter; +} + +string +IceUtil::wstringToString(const wstring& v, const StringConverterPtr& converter, const WstringConverterPtr& wConverter, + IceUtil::ConversionFlags flags) +{ + string target; + if(!v.empty()) + { + // + // First convert to UTF8 narrow string. + // + if(wConverter) + { + UTF8BufferI buffer; + Byte* last = wConverter->toUTF8(v.data(), v.data() + v.size(), buffer); + target = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); + } + else + { + size_t size = v.size() * 4 * sizeof(char); + + Byte* outBuf = new Byte[size]; + Byte* targetStart = outBuf; + Byte* targetEnd = outBuf + size; + + const wchar_t* sourceStart = v.data(); + + ConversionResult cr = + convertUTFWstringToUTF8( + sourceStart, sourceStart + v.size(), + targetStart, targetEnd, flags); + + if(cr != conversionOK) + { + delete[] outBuf; + assert(cr == sourceExhausted || cr == sourceIllegal); + throw IllegalConversionException(__FILE__, __LINE__, + cr == sourceExhausted ? "partial character" : "bad encoding"); + } + + target = string(reinterpret_cast<char*>(outBuf), static_cast<size_t>(targetStart - outBuf)); + delete[] outBuf; + } + + // + // If narrow string converter is present convert to the native narrow string encoding, otherwise + // native narrow string encoding is UTF8 and we are done. + // + if(converter) + { + string tmp; + converter->fromUTF8(reinterpret_cast<const Byte*>(target.data()), + reinterpret_cast<const Byte*>(target.data() + target.size()), tmp); + tmp.swap(target); + } + } + return target; +} + +wstring +IceUtil::stringToWstring(const string& v, const StringConverterPtr& converter, + const WstringConverterPtr& wConverter, IceUtil::ConversionFlags flags) +{ + wstring target; + if(!v.empty()) + { + // + // If there is a narrow string converter use it to convert to UTF8, otherwise the narrow + // string is already UTF8 encoded. + // + string tmp; + if(converter) + { + UTF8BufferI buffer; + Byte* last = converter->toUTF8(v.data(), v.data() + v.size(), buffer); + tmp = string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); + } + else + { + tmp = v; + } + + // + // If there is a wide string converter use fromUTF8 to convert to the wide string native encoding. + // + if(wConverter) + { + wConverter->fromUTF8(reinterpret_cast<const Byte*>(tmp.data()), + reinterpret_cast<const Byte*>(tmp.data() + tmp.size()), target); + } + else + { + const Byte* sourceStart = reinterpret_cast<const Byte*>(tmp.data()); + + ConversionResult cr = + convertUTF8ToUTFWstring(sourceStart, sourceStart + tmp.size(), target, flags); + + if(cr != conversionOK) + { + assert(cr == sourceExhausted || cr == sourceIllegal); + + throw IllegalConversionException(__FILE__, __LINE__, + cr == sourceExhausted ? "partial character" : "bad encoding"); + } + } + } + return target; +} + +string +IceUtil::nativeToUTF8(const string& str, const IceUtil::StringConverterPtr& converter) +{ + if(!converter || str.empty()) + { + return str; + } + UTF8BufferI buffer; + Byte* last = converter->toUTF8(str.data(), str.data() + str.size(), buffer); + return string(reinterpret_cast<const char*>(buffer.getBuffer()), last - buffer.getBuffer()); +} + +string +IceUtil::UTF8ToNative(const string& str, const IceUtil::StringConverterPtr& converter) +{ + if(!converter || str.empty()) + { + return str; + } + string tmp; + converter->fromUTF8(reinterpret_cast<const Byte*>(str.data()), + reinterpret_cast<const Byte*>(str.data() + str.size()), tmp); + return tmp; +} |