summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/StringConverterPlugin.cpp
diff options
context:
space:
mode:
authorJose <jose@zeroc.com>2014-05-02 19:56:38 +0200
committerJose <jose@zeroc.com>2014-05-02 19:56:38 +0200
commit1161c5817059464ab511632c0ce5d14593ced1a3 (patch)
tree51bbcdf2a4ea43c430312157350bb4271bc3f40d /cpp/src/Ice/StringConverterPlugin.cpp
parentUpdate .gitignore files (diff)
downloadice-1161c5817059464ab511632c0ce5d14593ced1a3.tar.bz2
ice-1161c5817059464ab511632c0ce5d14593ced1a3.tar.xz
ice-1161c5817059464ab511632c0ce5d14593ced1a3.zip
ICE-4851 - Use wstrings for input and output data that contain non-ASCII characters?
Diffstat (limited to 'cpp/src/Ice/StringConverterPlugin.cpp')
-rw-r--r--cpp/src/Ice/StringConverterPlugin.cpp179
1 files changed, 179 insertions, 0 deletions
diff --git a/cpp/src/Ice/StringConverterPlugin.cpp b/cpp/src/Ice/StringConverterPlugin.cpp
new file mode 100644
index 00000000000..5cd4526688b
--- /dev/null
+++ b/cpp/src/Ice/StringConverterPlugin.cpp
@@ -0,0 +1,179 @@
+// **********************************************************************
+//
+// 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.
+//
+// **********************************************************************
+
+#include <Ice/StringConverterPlugin.h>
+#include <IceUtil/IceUtil.h>
+#include <IceUtil/StringUtil.h>
+
+#include <Ice/Initialize.h>
+#include <Ice/Instance.h>
+#include <Ice/LocalException.h>
+#include <Ice/LoggerUtil.h>
+#include <Ice/Communicator.h>
+
+#ifndef _WIN32
+# include <IceUtil/IconvStringConverter.h>
+#endif
+
+#ifdef __MINGW32__
+# include <limits.h>
+#endif
+
+using namespace IceUtil;
+using namespace IceUtilInternal;
+using namespace std;
+
+
+Ice::StringConverterPlugin::StringConverterPlugin(const CommunicatorPtr& communicator,
+ const StringConverterPtr& stringConverter,
+ const WstringConverterPtr& wstringConverter)
+{
+ if(communicator == 0)
+ {
+ throw PluginInitializationException(__FILE__, __LINE__, "Communicator cannot be null");
+ }
+
+ IceInternal::InstancePtr instance = IceInternal::getInstance(communicator);
+
+ IceUtil::setProcessStringConverter(stringConverter);
+ instance->setStringConverter(stringConverter);
+ IceUtil::setProcessWstringConverter(wstringConverter);
+ instance->setWstringConverter(wstringConverter);
+}
+
+void
+Ice::StringConverterPlugin::initialize()
+{
+}
+
+void
+Ice::StringConverterPlugin::destroy()
+{
+}
+
+//
+// The entry point for the "string converter" plug-in built-in the Ice library
+//
+extern "C"
+{
+
+using namespace Ice;
+
+ICE_DECLSPEC_EXPORT Plugin*
+createStringConverter(const CommunicatorPtr& communicator, const string& name, const StringSeq& args)
+{
+ StringConverterPtr stringConverter;
+ 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 = new WindowsStringConverter(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 = new IconvStringConverter<char>;
+ break;
+ }
+ case 1:
+ {
+ stringConverter = new IconvStringConverter<char>(iconvArgs[0].c_str());
+ break;
+ }
+ case 2:
+ {
+ stringConverter = new IconvStringConverter<char>(iconvArgs[0].c_str());
+ wstringConverter = new IconvStringConverter<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;
+ }
+}
+}