1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
// **********************************************************************
//
// Copyright (c) 2003-2013 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/Ice.h>
#include <TestCommon.h>
#include <Test.h>
#if defined(ICONV_ON_WINDOWS)
//
// On Windows, Ice/IcongStringConverter.h is not included by Ice/Ice.h
//
#include <Ice/IconvStringConverter.h>
#endif
#include <iostream>
#include <locale.h>
using namespace std;
class Client : public Ice::Application
{
public:
virtual int run(int, char*[]);
};
//
// Server side is pure unicode
//
class MyObjectI : public Test::MyObject
{
public:
virtual wstring widen(const string& msg, const Ice::Current&)
{
const Ice::Byte* cmsg = reinterpret_cast<const Ice::Byte*>(msg.c_str());
if(!IceUtil::isLegalUTF8Sequence(cmsg, cmsg + msg.size()))
{
throw Test::BadEncodingException();
}
return IceUtil::stringToWstring(msg);
}
virtual string narrow(const wstring& wmsg, const Ice::Current&)
{
return IceUtil::wstringToString(wmsg);
}
};
static bool useLocale = false;
static bool useIconv = true;
int
main(int argc, char* argv[])
{
Client app;
#ifndef _WIN32
//
// Switch to French locale
// (we just used the codeset for as default internal code for
// initData.stringConverter below)
//
useLocale = (setlocale(LC_ALL, "fr_FR.ISO8859-15") != 0
|| setlocale(LC_ALL, "fr_FR.iso885915@euro") != 0);
#endif
Ice::InitializationData initData;
#if defined(_WIN32) && !defined(ICONV_ON_WINDOWS)
//
// 28605 == ISO 8859-15 codepage
//
initData.stringConverter = new Ice::WindowsStringConverter(28605);
useIconv = false;
#elif defined(__hpux)
if(useLocale)
{
initData.stringConverter = new Ice::IconvStringConverter<char>;
}
else
{
initData.stringConverter = new Ice::IconvStringConverter<char>("iso815");
}
initData.wstringConverter = new Ice::IconvStringConverter<wchar_t>("ucs4");
#else
if(useLocale)
{
#ifndef _WIN32
initData.stringConverter = new Ice::IconvStringConverter<char>;
#endif
}
else
{
initData.stringConverter = new Ice::IconvStringConverter<char>("ISO8859-15");
}
if(sizeof(wchar_t) == 4)
{
#ifdef ICE_BIG_ENDIAN
initData.wstringConverter = new Ice::IconvStringConverter<wchar_t>("UTF-32BE");
#else
initData.wstringConverter = new Ice::IconvStringConverter<wchar_t>("UTF-32LE");
#endif
}
else
{
#ifdef ICE_BIG_ENDIAN
initData.wstringConverter = new Ice::IconvStringConverter<wchar_t>("UTF-16BE");
#else
initData.wstringConverter = new Ice::IconvStringConverter<wchar_t>("UTF-16LE");
#endif
}
#endif
return app.main(argc, argv, initData);
}
int
Client::run(int, char*[])
{
//
// Create server communicator and OA
//
Ice::InitializationData initData;
initData.properties = communicator()->getProperties()->clone();
Ice::CommunicatorPtr serverCommunicator = Ice::initialize(initData);
Ice::ObjectAdapterPtr oa = serverCommunicator->createObjectAdapterWithEndpoints("MyOA", "tcp -h localhost");
Ice::ObjectPtr servant = new MyObjectI;
Test::MyObjectPrx serverPrx = Test::MyObjectPrx::uncheckedCast(oa->addWithUUID(servant));
oa->activate();
//
// Get a prx in the client's communicator
//
Test::MyObjectPrx clientPrx =
Test::MyObjectPrx::uncheckedCast(communicator()->stringToProxy(serverPrx->ice_toString()));
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 = clientPrx->widen(msg);
test(clientPrx->narrow(wmsg) == msg);
test(wmsg.size() == msg.size());
cout << "ok" << endl;
//
// destroy server communicator
//
serverCommunicator->destroy();
return EXIT_SUCCESS;
}
|