~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Character/NUnicode.h

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef NUNICODE_H
 
2
#define NUNICODE_H
 
3
 
 
4
 
 
5
NAMESPACE_BEGIN
 
6
 
 
7
// UTF-16 is the primary encoding mechanism used by Microsoft Windows 2000, Windows 2000 Server, Windows XP and Windows 2003 Server.
 
8
// Unicode Byte Order Mark (BOM)
 
9
enum {UNICODE_UTF32_BE   = 0x0000FEFF };
 
10
enum {UNICODE_UTF32_LE   = 0xFFFE0000 };
 
11
enum {UNICODE_UTF16_BE   = 0xFEFF };
 
12
enum {UNICODE_UTF16_LE   = 0xFFFE };
 
13
enum {UNICODE_UTF8       = 0xEFBBBF };
 
14
 
 
15
const BYTE UTF32_BE[]   = {0x04 /*size*/, 0x00, 0x00, 0xFE, 0xFF };
 
16
const BYTE UTF32_LE[]   = {0x04 /*size*/, 0xFF, 0xFE, 0x00, 0x00 };
 
17
const BYTE UTF16_BE[]   = {0x02 /*size*/, 0xFE, 0xFF };
 
18
const BYTE UTF16_LE[]   = {0x02 /*size*/, 0xFF, 0xFE };
 
19
const BYTE UTF8[]       = {0x03 /*size*/, 0xEF, 0xBB, 0xBF };
 
20
 
 
21
enum {UNICODE_BOM   = 0xfeff};
 
22
 
 
23
// UTF-16 is the default encoding form of the Unicode Standard
 
24
// On Linux and Mac OS X, wchar_t is 4 bytes!
 
25
// On windows wchar_t is 2 bytes!
 
26
 
 
27
#ifdef UNICODE
 
28
    inline TCHAR ConvertAnsiCharToTCHAR(ANSICHAR In)
 
29
    {
 
30
        TCHAR output;
 
31
        const t_UTF8 *source_start = &In;
 
32
        const t_UTF8 *source_end = source_start + 1;
 
33
        t_UTF16* target_start = reinterpret_cast<t_UTF16*>(&output);
 
34
        t_UTF16* target_end = target_start + sizeof(wchar_t);
 
35
 
 
36
        ConversionResult res = ConvertUTF8toUTF16(&source_start, source_end, &target_start, target_end, lenientConversion);
 
37
        if (res != conversionOK)
 
38
        {
 
39
            output = 0;
 
40
        }
 
41
        return output;
 
42
    }
 
43
    
 
44
    inline ANSICHAR ConvertTCHARToAnsiChar(TCHAR In)
 
45
    {
 
46
        ANSICHAR output;
 
47
        const t_UTF16 *source_start = &In;
 
48
        const t_UTF16 *source_end = source_start + 1;
 
49
        t_UTF8* target_start = reinterpret_cast<t_UTF8*>(&output);
 
50
        t_UTF8* target_end = target_start + sizeof(wchar_t);
 
51
 
 
52
        ConversionResult res = ConvertUTF16toUTF8(&source_start, source_end, &target_start, target_end, lenientConversion);
 
53
        if (res != conversionOK)
 
54
        {
 
55
            output = 0;
 
56
        }
 
57
        return output;
 
58
    }
 
59
    inline TCHAR ConvertUnicodeCharToTCHAR(UNICHAR In) {return In;}
 
60
    inline UNICHAR  ConvertTCHARToUnicodeChar(TCHAR In) {return In;}
 
61
#else
 
62
    inline TCHAR ConvertUnicodeCharToTCHAR(UNICHAR In)
 
63
    {
 
64
        TCHAR output;
 
65
        const t_UTF16 *source_start = &In;
 
66
        const t_UTF16 *source_end = source_start + 1;
 
67
        t_UTF8* target_start = reinterpret_cast<t_UTF8*>(&output);
 
68
        t_UTF8* target_end = target_start + sizeof(wchar_t);
 
69
 
 
70
        ConversionResult res = ConvertUTF16toUTF8(&source_start, source_end, &target_start, target_end, lenientConversion);
 
71
        if (res != conversionOK)
 
72
        {
 
73
            output = 0;
 
74
        }
 
75
        return output;
 
76
    }
 
77
    
 
78
    inline UNICHAR ConvertTCHARToUnicodeChar(TCHAR In)
 
79
    {
 
80
        UNICHAR output;
 
81
        const t_UTF8 *source_start = reinterpret_cast<const t_UTF8*>(&In);
 
82
        const t_UTF8 *source_end = source_start + 1;
 
83
        t_UTF16* target_start = reinterpret_cast<t_UTF16*>(&output);
 
84
        t_UTF16* target_end = target_start + sizeof(wchar_t);
 
85
 
 
86
        ConversionResult res = ConvertUTF8toUTF16(&source_start, source_end, &target_start, target_end, lenientConversion);
 
87
        if (res != conversionOK)
 
88
        {
 
89
            output = 0;
 
90
        }
 
91
        return output;
 
92
    }
 
93
 
 
94
    inline TCHAR ConvertAnsiCharToTCHAR(ANSICHAR In) {return In;}
 
95
    inline ANSICHAR ConvertTCHARToAnsiChar(TCHAR In) {return In;}
 
96
#endif
 
97
 
 
98
/*!
 
99
    Convert a single UNICHAR to ANSICHAR.
 
100
*/
 
101
inline ANSICHAR ConvertUnicodeCharToAnsiChar(UNICHAR In)
 
102
{
 
103
    TCHAR output;
 
104
    const t_UTF16 *source_start = &In;
 
105
    const t_UTF16 *source_end = source_start + 1;
 
106
    t_UTF8* target_start = reinterpret_cast<t_UTF8*>(&output);
 
107
    t_UTF8* target_end = target_start + sizeof(wchar_t);
 
108
 
 
109
    ConversionResult res = ConvertUTF16toUTF8(&source_start, source_end, &target_start, target_end, lenientConversion);
 
110
    if (res != conversionOK)
 
111
    {
 
112
        output = 0;
 
113
    }
 
114
    return output;
 
115
}
 
116
 
 
117
/*!
 
118
    Convert a single ANSICHAR to UNICHAR.
 
119
*/
 
120
inline UNICHAR ConvertAnsiCharToUnicodeChar(ANSICHAR In)
 
121
{
 
122
    TCHAR output;
 
123
    const t_UTF8 *source_start = reinterpret_cast<const t_UTF8*>(&In);
 
124
    const t_UTF8 *source_end = source_start + 1;
 
125
    t_UTF16* target_start = reinterpret_cast<t_UTF16*>(&output);
 
126
    t_UTF16* target_end = target_start + sizeof(wchar_t);
 
127
 
 
128
    ConversionResult res = ConvertUTF8toUTF16(&source_start, source_end, &target_start, target_end, lenientConversion);
 
129
    if (res != conversionOK)
 
130
    {
 
131
        output = 0;
 
132
    }
 
133
    return output;
 
134
}
 
135
 
 
136
class UnicharToAnsicharConvertion
 
137
{
 
138
public:
 
139
    // Default to ANSI code page
 
140
    UnicharToAnsicharConvertion() {}
 
141
 
 
142
    /*!
 
143
    Convert from UNICHAR to ANSICHAR
 
144
    @param Source String to convert. Null terminated.
 
145
    @return Return a pointer to the new string. Null terminated.
 
146
    */
 
147
    ANSICHAR* Convert(const UNICHAR* Source);
 
148
    /*{
 
149
        std::wstring utf16string(Source);
 
150
        size_t utf16size = utf16string.length();
 
151
        size_t utf8size = 6 * utf16size;
 
152
        ANSICHAR *utf8string = new ANSICHAR[utf8size+1];
 
153
 
 
154
        const t_UTF16 *source_start = utf16string.c_str();
 
155
        const t_UTF16 *source_end = source_start + utf16size;
 
156
        t_UTF8* target_start = reinterpret_cast<t_UTF8*>(utf8string);
 
157
        t_UTF8* target_end = target_start + utf8size;
 
158
 
 
159
        ConversionResult res = ConvertUTF16toUTF8(&source_start, source_end, &target_start, target_end, lenientConversion);
 
160
        if (res != conversionOK)
 
161
        {
 
162
            delete utf8string;
 
163
            utf8string = 0;
 
164
        }
 
165
        // mark end of string
 
166
        *target_start = 0;
 
167
        return utf8string;
 
168
    }*/
 
169
};
 
170
 
 
171
//! ANSICHAR to UNICHAR conversion 
 
172
class AnsicharToUnicharConvertion
 
173
{
 
174
public:
 
175
    AnsicharToUnicharConvertion() {}
 
176
 
 
177
    /*!
 
178
        Convert from ANSICHAR to UNICHAR
 
179
        @param Source String to convert. Null terminated.
 
180
        @return Return a pointer to the new string. Null terminated.
 
181
    */
 
182
    UNICHAR* Convert(const ANSICHAR* Source);
 
183
};
 
184
 
 
185
//! TCHAR to ANSI conversion
 
186
// TCHAR can be ansi or unicode depending if UNICODE is defined or not.
 
187
class TCharToAnsiConvertion
 
188
{
 
189
public:
 
190
    INL_INLINE TCharToAnsiConvertion() {}
 
191
 
 
192
      /*!
 
193
      Convert from TCHAR to ANSICHAR
 
194
      @param Source String to convert. Null terminated.
 
195
      @return Return a pointer to the new string. Null terminated.
 
196
      */
 
197
      INL_INLINE ANSICHAR* Convert(const TCHAR* Source)
 
198
      {
 
199
          // Determine whether we need to allocate memory or not
 
200
#ifdef UNICODE
 
201
          UnicharToAnsicharConvertion convert;
 
202
          return convert.Convert(Source);
 
203
#else
 
204
          size_t length = strlen(Source) + 1;
 
205
          size_t size = length * sizeof(ANSICHAR);
 
206
          ANSICHAR* Dest = new ANSICHAR[size];
 
207
          STRNCPY_S(Dest, size, Source, length);
 
208
          return Dest;
 
209
#endif
 
210
      }
 
211
};
 
212
 
 
213
//! ANSI to TCHAR conversion
 
214
// TCHAR can be ansi or unicode depending if UNICODE is defined or not.
 
215
class AnsiToTCharConversion
 
216
{
 
217
public:
 
218
    INL_INLINE AnsiToTCharConversion() {}
 
219
 
 
220
    /*!
 
221
    Convert from ANSICHAR to TCHAR
 
222
    @param Source String to convert. Null terminated.
 
223
    @return Return a pointer to the new string. Null terminated.
 
224
    */
 
225
    INL_INLINE TCHAR* Convert(const ANSICHAR* Source)
 
226
    {
 
227
#ifdef UNICODE
 
228
        AnsicharToUnicharConvertion convert;
 
229
        return convert.Convert(Source);
 
230
#else
 
231
        size_t length = strlen(Source) + 1;
 
232
        size_t size = length;
 
233
        TCHAR* Dest = new TCHAR[size];
 
234
        STRNCPY_S(Dest, size, Source, length);
 
235
        return Dest;
 
236
#endif
 
237
    }
 
238
};
 
239
 
 
240
/*!
 
241
Convert from one string format to another.
 
242
*/
 
243
template<typename CONVERT_TO,typename CONVERT_FROM, typename BASE_CONVERTER, DWORD DefaultConversionSize = 128>
 
244
class NCharacterConversion:   public BASE_CONVERTER
 
245
{
 
246
    CONVERT_TO* ConvertedString;
 
247
 
 
248
    // Hide the default constructor
 
249
    NCharacterConversion();
 
250
 
 
251
public:
 
252
    /*!
 
253
        Converts the data by using the Convert() method on the base class
 
254
    */
 
255
    explicit inline NCharacterConversion(const CONVERT_FROM* Source)
 
256
    {
 
257
        if (Source != NULL)
 
258
        {
 
259
            // Use base class' convert method
 
260
            ConvertedString = BASE_CONVERTER::Convert(Source);
 
261
        }
 
262
        else
 
263
        {
 
264
            ConvertedString = NULL;
 
265
        }
 
266
    }
 
267
 
 
268
    /*!
 
269
        If memory was allocated, then it is freed below
 
270
    */
 
271
    inline ~NCharacterConversion()
 
272
    {
 
273
        if (ConvertedString != NULL)
 
274
        {
 
275
            delete [] ConvertedString;
 
276
        }
 
277
    }
 
278
 
 
279
    // Operator to get access to the converted string
 
280
    inline operator CONVERT_TO*(void) const
 
281
    {
 
282
        return ConvertedString;
 
283
    }
 
284
};
 
285
 
 
286
// Conversion typedefs
 
287
// typedef NCharacterConversion<TCHAR, ANSICHAR, AnsiToTCharConversion>           ANSI_To_TCHAR_Conversion;
 
288
// typedef NCharacterConversion<ANSICHAR, TCHAR, TCharToAnsiConvertion>           TCHAR_To_ANSI_Conversion;
 
289
// typedef NCharacterConversion<ANSICHAR, UNICHAR, UnicharToAnsicharConvertion>   UNICHAR_To_ANSICHAR_Conversion;
 
290
// typedef NCharacterConversion<UNICHAR, ANSICHAR, AnsicharToUnicharConvertion>   ANSICHAR_To_UNICHAR_Conversion;
 
291
 
 
292
NAMESPACE_END
 
293
 
 
294
#endif // NUNICODE_H