diff options
author | Jose <jose@zeroc.com> | 2016-08-01 19:32:10 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2016-08-01 19:32:10 +0200 |
commit | 7465fde1299e4fc3c2df92d304737f80bca92325 (patch) | |
tree | fecbcbf8b481f7a7ce6abf4d14f29558979ce228 | |
parent | Move StringConverter API to namespace Ice (diff) | |
download | ice-7465fde1299e4fc3c2df92d304737f80bca92325.tar.bz2 ice-7465fde1299e4fc3c2df92d304737f80bca92325.tar.xz ice-7465fde1299e4fc3c2df92d304737f80bca92325.zip |
Fixed ICE-7259 Resurrect default String Converter plugin
-rw-r--r-- | cpp/src/Ice/StringConverterPlugin.cpp | 185 | ||||
-rw-r--r-- | cpp/test/Ice/stringConverter/Client.cpp | 140 |
2 files changed, 265 insertions, 60 deletions
diff --git a/cpp/src/Ice/StringConverterPlugin.cpp b/cpp/src/Ice/StringConverterPlugin.cpp new file mode 100644 index 00000000000..7450ebda82e --- /dev/null +++ b/cpp/src/Ice/StringConverterPlugin.cpp @@ -0,0 +1,185 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2015 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. +// +// ********************************************************************** + +#include <Ice/Config.h> + +#include <IceUtil/IceUtil.h> +#include <Ice/StringConverter.h> +#include <Ice/IconvStringConverter.h> +#include <IceUtil/StringUtil.h> + +#include <Ice/Communicator.h> +#include <Ice/Initialize.h> +#include <Ice/LocalException.h> +#include <Ice/LoggerUtil.h> + +using namespace IceUtilInternal; +using namespace Ice; +using namespace std; + +class ICE_API StringConverterPlugin : public Plugin +{ +public: + + StringConverterPlugin(const CommunicatorPtr&, + const IceUtil::StringConverterPtr&, const IceUtil::WstringConverterPtr& = 0); + + virtual void initialize(); + + virtual void destroy(); +}; + +StringConverterPlugin::StringConverterPlugin(const CommunicatorPtr& /*notused*/, + const IceUtil::StringConverterPtr& stringConverter, + const IceUtil::WstringConverterPtr& wstringConverter) +{ + IceUtil::setProcessStringConverter(stringConverter); + IceUtil::setProcessWstringConverter(wstringConverter); +} + +void StringConverterPlugin::initialize() +{ + // no op +} + +void StringConverterPlugin::destroy() +{ + // no op +} + +// +// The entry point for the string converter plugin built-in the Ice library +// +extern "C" +{ + +ICE_API Plugin* +createStringConverter(const CommunicatorPtr& communicator, const string& name, const StringSeq& args) +{ + IceUtil::StringConverterPtr stringConverter; + IceUtil::WstringConverterPtr wstringConverter; + + if(args.size() > 2) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": too many arguments"; + return 0; + } + + try + { + +#ifdef _WIN32 + int cp = -1; + + for(size_t i = 0; i < args.size(); ++i) + { + if(args[i].find("windows=") == 0) + { + cp = atoi(args[i].substr(strlen("windows=")).c_str()); + } + else if(args[i].find("iconv=") != 0) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": invalid \"" << args[i] << "\" argument"; + return 0; + } + } + + if(cp == -1) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": missing windows=<code page> argument"; + return 0; + } + + if(cp == 0 || cp == INT_MAX || cp < 0) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": invalid Windows code page"; + return 0; + } + + stringConverter = createWindowsStringConverter(static_cast<unsigned int>(cp)); +#else + StringSeq iconvArgs; + + for(size_t i = 0; i < args.size(); ++i) + { + if(args[i].find("iconv=") == 0) + { + if(!IceUtilInternal::splitString(args[i].substr(strlen("iconv=")), ", \t\r\n", iconvArgs)) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": invalid iconv argument"; + return 0; + } + } + else if(args[i].find("windows=") != 0) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": invalid \"" << args[i] << "\" argument"; + return 0; + } + } + + switch(iconvArgs.size()) + { + case 0: + { + stringConverter = createIconvStringConverter<char>(""); + break; + } + case 1: + { + stringConverter = createIconvStringConverter<char>(iconvArgs[0].c_str()); + break; + } + case 2: + { + stringConverter = createIconvStringConverter<char>(iconvArgs[0].c_str()); + wstringConverter = createIconvStringConverter<wchar_t>(iconvArgs[1].c_str()); + break; + } + default: + { + assert(0); + } + } + +#endif + + return new StringConverterPlugin(communicator, stringConverter, wstringConverter); + } + catch(const std::exception& ex) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": creation failed with " << ex.what(); + return 0; + } + catch(...) + { + Error out(communicator->getLogger()); + out << "Plugin " << name << ": creation failed with unknown exception"; + return 0; + } +} + +} + +namespace Ice +{ + +ICE_API void +registerIceStringConverter(bool loadOnInitialize) +{ + Ice::registerPluginFactory("IceStringConverter", createStringConverter, loadOnInitialize); +} + +} diff --git a/cpp/test/Ice/stringConverter/Client.cpp b/cpp/test/Ice/stringConverter/Client.cpp index e0f82d4130b..7009dd7b3e9 100644 --- a/cpp/test/Ice/stringConverter/Client.cpp +++ b/cpp/test/Ice/stringConverter/Client.cpp @@ -16,13 +16,6 @@ using namespace std; -class Client : public Ice::Application -{ -public: - - virtual int run(int, char*[]); -}; - static bool useLocale = false; static bool useIconv = true; @@ -32,8 +25,9 @@ main(int argc, char* argv[]) #ifdef ICE_STATIC_LIBS Ice::registerIceSSL(); #endif - - Client app; + + string narrowEncoding; + string wideEncoding; #ifndef _WIN32 // @@ -45,98 +39,124 @@ main(int argc, char* argv[]) #endif #if defined(_WIN32) - // - // 28605 == ISO 8859-15 codepage - // - setProcessStringConverter(Ice::createWindowsStringConverter(28605)); useIconv = false; #elif defined(__hpux) - if(useLocale) + if(!useLocale) { - setProcessStringConverter(Ice::createIconvStringConverter<char>); + narrowEncoding = "iso815"; } - else - { - setProcessStringConverter(Ice::createIconvStringConverter<char>("iso815")); - } - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("ucs4")); + wideEncoding = "ucs4"; #elif defined(_AIX) // Always big-endian - if(useLocale) - { - setProcessStringConverter(Ice::createIconvStringConverter<char>()); - } - else + if(!useLocale) { - setProcessStringConverter(Ice::createIconvStringConverter<char>("ISO8859-15")); + narrowEncoding = "ISO8859-15"; } if(sizeof(wchar_t) == 4) { - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-32")); + wideEncoding = "UTF-32"; } else { - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-16")); + wideEncoding = "UTF-16"; } #else - if(useLocale) - { - setProcessStringConverter(Ice::createIconvStringConverter<char>()); - } - else + if(!useLocale) { - setProcessStringConverter(Ice::createIconvStringConverter<char>("ISO8859-15")); + narrowEncoding = "ISO8859-15"; } if(sizeof(wchar_t) == 4) { # ifdef ICE_BIG_ENDIAN - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-32BE")); + wideEncoding = "UTF-32BE"; # else - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-32LE")); + wideEncoding = "UTF-32LE"; # endif } else { # ifdef ICE_BIG_ENDIAN - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-16BE")); + wideEncoding = "UTF-16BE"; # else - setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>("UTF-16LE")); + wideEncoding = "UTF-16LE"; # endif } #endif - return app.main(argc, argv); -} -int -Client::run(int, char*[]) -{ - Test::MyObjectPrxPtr proxy = - ICE_UNCHECKED_CAST(Test::MyObjectPrx, - communicator()->stringToProxy("test:" + getTestEndpoint(communicator(), 0))); - - char oe = char(0xBD); // A single character in ISO Latin 9 - string msg = string("tu me fends le c") + oe + "ur!"; - cout << "testing string converter"; - if(useLocale) { - cout << " (using locale)"; +#ifdef _WIN32 + // + // 28605 == ISO 8859-15 codepage + // + setProcessStringConverter(Ice::createWindowsStringConverter(28605)); +#else + setProcessStringConverter(Ice::createIconvStringConverter<char>(narrowEncoding.c_str())); + setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>(wideEncoding.c_str())); +#endif + Ice::CommunicatorPtr communicator = Ice::initialize(); + Test::MyObjectPrxPtr proxy = + ICE_UNCHECKED_CAST(Test::MyObjectPrx, + communicator->stringToProxy("test:" + getTestEndpoint(communicator, 0))); + + char oe = char(0xBD); // A single character in ISO Latin 9 + string msg = string("tu me fends le c") + oe + "ur!"; + cout << "testing string converter"; + if(useLocale) + { + cout << " (using locale)"; + } + if(useIconv) + { + cout << " (using iconv)"; + } + cout << "... " << flush; + wstring wmsg = proxy->widen(msg); + test(proxy->narrow(wmsg) == msg); + test(wmsg.size() == msg.size()); + communicator->destroy(); + cout << "ok" << endl; } - if(useIconv) + + IceUtil::setProcessStringConverter(ICE_NULLPTR); + IceUtil::setProcessWstringConverter(ICE_NULLPTR); + { - cout << " (using iconv)"; + Ice::InitializationData initData; + initData.properties = Ice::createProperties(); + initData.properties->setProperty("Ice.Plugin.IceStringConverter", + string("Ice:createStringConverter iconv=") + narrowEncoding + "," + wideEncoding + " windows=28605"); + + Ice::CommunicatorPtr communicator = Ice::initialize(initData); + Test::MyObjectPrxPtr proxy = + ICE_UNCHECKED_CAST(Test::MyObjectPrx, + communicator->stringToProxy("test:" + getTestEndpoint(communicator, 0))); + + char oe = char(0xBD); // A single character in ISO Latin 9 + string msg = string("tu me fends le c") + oe + "ur!"; + cout << "testing string converter plug-in"; + if(useLocale) + { + cout << " (using locale)"; + } + if(useIconv) + { + cout << " (using iconv)"; + } + cout << "... " << flush; + wstring wmsg = proxy->widen(msg); + test(proxy->narrow(wmsg) == msg); + test(wmsg.size() == msg.size()); + cout << "ok" << endl; + + proxy->shutdown(); + communicator->destroy(); } - cout << "... " << flush; - wstring wmsg = proxy->widen(msg); - test(proxy->narrow(wmsg) == msg); - test(wmsg.size() == msg.size()); - cout << "ok" << endl; - proxy->shutdown(); return EXIT_SUCCESS; } |