summaryrefslogtreecommitdiff
path: root/cpp/test/Ice/stringConverter/Client.cpp
blob: 1dd7d40ac5aa6556f526b3931d2dc3bd90fbfcc8 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// **********************************************************************
//
// Copyright (c) 2003-present 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 <TestHelper.h>
#include <Test.h>

#include <iostream>
#include <locale.h>

#ifdef _MSC_VER
#   pragma warning(disable:4127) // conditional expression is constant
#   pragma warning(disable:4310) // cast truncates constant value
#endif

using namespace std;

static bool useLocale = false;
static bool useIconv = true;

class Client : public Test::TestHelper
{
public:

    void run(int, char**);
};

void
Client::run(int argc, char** argv)
{
    Ice::PropertiesPtr properties = createTestProperties(argc, argv);
#ifdef ICE_STATIC_LIBS
    Ice::registerIceStringConverter(false);
#endif

    string narrowEncoding;
    string wideEncoding;

#ifdef _WIN32
    useIconv = false;
#else
    //
    // Switch to French locale
    // (we just used the codeset for as default internal code for
    // stringConverter below)
    //
    useLocale = (setlocale(LC_ALL, "fr_FR.ISO8859-15") != 0 || setlocale(LC_ALL, "fr_FR.iso885915@euro") != 0);
#endif

    if(useIconv)
    {

#if defined(__hpux)
        narrowEncoding = "iso815";
        wideEncoding = "ucs4";

#elif defined(_AIX)

        // Always big-endian
        narrowEncoding = "ISO8859-15";

        if(sizeof(wchar_t) == 4)
        {
            wideEncoding = "UTF-32";
        }
        else
        {
            wideEncoding = "UTF-16";
        }
#else

        narrowEncoding = "ISO8859-15";

        if(sizeof(wchar_t) == 4)
        {
#  ifdef ICE_BIG_ENDIAN
            wideEncoding = "UTF-32BE";
#  else
            wideEncoding = "UTF-32LE";
#  endif
        }
        else
        {
#  ifdef ICE_BIG_ENDIAN
            wideEncoding = "UTF-16BE";
#  else
            wideEncoding = "UTF-16LE";
#  endif
        }
#endif
    }

    {
#ifdef _WIN32
        //
        // 28605 == ISO 8859-15 codepage
        //
        setProcessStringConverter(Ice::createWindowsStringConverter(28605));
#else
        if(useLocale)
        {
            setProcessStringConverter(Ice::createIconvStringConverter<char>(""));
        }
        else
        {
            setProcessStringConverter(Ice::createIconvStringConverter<char>(narrowEncoding));
        }

        setProcessWstringConverter(Ice::createIconvStringConverter<wchar_t>(wideEncoding));

#endif

        Ice::CommunicatorHolder communicator = initialize(argc, argv, properties);
        Test::MyObjectPrxPtr proxy =
            ICE_UNCHECKED_CAST(Test::MyObjectPrx, communicator->stringToProxy("test:" + getTestEndpoint()));

        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());

        // Test stringToIdentity and identityToString

        string identStr = "cat/" + msg;
        Ice::Identity ident = Ice::stringToIdentity(identStr);
        test(ident.name == msg);
        test(ident.category == "cat");
        test(identityToString(ident, Ice::ICE_ENUM(ToStringMode, Unicode)) == identStr);

        identStr = identityToString(ident, Ice::ICE_ENUM(ToStringMode, ASCII));
        test(identStr == "cat/tu me fends le c\\u0153ur!");
        test(Ice::stringToIdentity(identStr) == ident);
        identStr = identityToString(ident, Ice::ICE_ENUM(ToStringMode, Compat));
        test(identStr == "cat/tu me fends le c\\305\\223ur!");
        test(Ice::stringToIdentity(identStr) == ident);

        cout << "ok" << endl;
    }

    Ice::setProcessStringConverter(ICE_NULLPTR);
    Ice::setProcessWstringConverter(Ice::createUnicodeWstringConverter());

    string propValue = "Ice:createStringConverter";
    if(useIconv && !useLocale)
    {
        propValue +=  " iconv=" + narrowEncoding + "," + wideEncoding;
    }
    propValue += " windows=28605";

    properties->setProperty("Ice.Plugin.IceStringConverter", propValue);

    Ice::CommunicatorHolder communicator = initialize(argc, argv, properties);
    Test::MyObjectPrxPtr proxy =
        ICE_UNCHECKED_CAST(Test::MyObjectPrx, communicator->stringToProxy("test:" + getTestEndpoint()));

    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();
}

DEFINE_TEST(Client);