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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
/*
Copyright (C) 2009 Ricardo Pescuma Domenecci
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef __UTF8_HELPERS_H__
# define __UTF8_HELPERS_H__
#include <windows.h>
#include <newpluginapi.h>
#include <m_system.h>
class TcharToUtf8
{
public:
TcharToUtf8(const char *str) : utf8(NULL)
{
int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
if (size <= 0)
throw L"Could not convert string to WCHAR";
WCHAR *tmp = (WCHAR *) mir_alloc(size * sizeof(WCHAR));
if (tmp == NULL)
throw L"mir_alloc returned NULL";
MultiByteToWideChar(CP_ACP, 0, str, -1, tmp, size);
init(tmp);
mir_free(tmp);
}
TcharToUtf8(const WCHAR *str) : utf8(NULL)
{
init(str);
}
~TcharToUtf8()
{
if (utf8 != NULL)
mir_free(utf8);
}
char *detach()
{
char *ret = utf8;
utf8 = NULL;
return ret;
}
const char * get() const
{
return utf8;
}
operator const char *() const
{
return utf8;
}
const char & operator[](int pos) const
{
return utf8[pos];
}
private:
char *utf8;
void init(const WCHAR *str)
{
int size = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
if (size <= 0)
throw L"Could not convert string to UTF8";
utf8 = (char *) mir_alloc(size);
if (utf8 == NULL)
throw L"mir_alloc returned NULL";
WideCharToMultiByte(CP_UTF8, 0, str, -1, utf8, size, NULL, NULL);
}
};
class Utf8ToTchar
{
public:
Utf8ToTchar(const char *str) : tchar(NULL)
{
if (str == NULL)
return;
int size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
if (size <= 0)
throw L"Could not convert string to WCHAR";
WCHAR *tmp = (WCHAR *) mir_alloc(size * sizeof(WCHAR));
if (tmp == NULL)
throw L"mir_alloc returned NULL";
MultiByteToWideChar(CP_UTF8, 0, str, -1, tmp, size);
#ifdef UNICODE
tchar = tmp;
#else
size = WideCharToMultiByte(CP_ACP, 0, tmp, -1, NULL, 0, NULL, NULL);
if (size <= 0)
{
mir_free(tmp);
throw L"Could not convert string to ACP";
}
tchar = (TCHAR *) mir_alloc(size * sizeof(char));
if (tchar == NULL)
{
mir_free(tmp);
throw L"mir_alloc returned NULL";
}
WideCharToMultiByte(CP_ACP, 0, tmp, -1, tchar, size, NULL, NULL);
mir_free(tmp);
#endif
}
~Utf8ToTchar()
{
if (tchar != NULL)
mir_free(tchar);
}
TCHAR *detach()
{
TCHAR *ret = tchar;
tchar = NULL;
return ret;
}
TCHAR * get() const
{
return tchar;
}
operator TCHAR *() const
{
return tchar;
}
TCHAR & operator[](int pos)
{
return tchar[pos];
}
private:
TCHAR *tchar;
};
class CharToTchar
{
public:
CharToTchar(const char *str) : tchar(NULL)
{
if (str == NULL)
return;
#ifdef UNICODE
tchar = mir_a2u(str);
#else
tchar = str;
#endif
}
~CharToTchar()
{
#ifdef UNICODE
if (tchar != NULL)
mir_free(tchar);
#endif
}
TCHAR *detach()
{
#ifdef UNICODE
TCHAR *ret = tchar;
#else
TCHAR *ret = (tchar == NULL ? NULL : mir_strdup(tchar));
#endif
tchar = NULL;
return ret;
}
const TCHAR * get() const
{
return tchar;
}
operator const TCHAR *() const
{
return tchar;
}
const TCHAR & operator[](int pos) const
{
return tchar[pos];
}
private:
#ifdef UNICODE
TCHAR *tchar;
#else
const TCHAR *tchar;
#endif
};
class WcharToTchar
{
public:
WcharToTchar(const WCHAR *str) : tchar(NULL)
{
if (str == NULL)
return;
#ifdef UNICODE
tchar = str;
#else
tchar = mir_u2a(str);
#endif
}
~WcharToTchar()
{
#ifndef UNICODE
if (tchar != NULL)
mir_free(tchar);
#endif
}
TCHAR *detach()
{
#ifdef UNICODE
TCHAR *ret = (tchar == NULL ? NULL : mir_wstrdup(tchar));
#else
TCHAR *ret = tchar;
#endif
tchar = NULL;
return ret;
}
const TCHAR * get() const
{
return tchar;
}
operator const TCHAR *() const
{
return tchar;
}
const TCHAR & operator[](int pos) const
{
return tchar[pos];
}
private:
#ifdef UNICODE
const TCHAR *tchar;
#else
TCHAR *tchar;
#endif
};
class CharToWchar
{
public:
CharToWchar(const char *str) : wchar(NULL)
{
if (str == NULL)
return;
wchar = mir_a2u(str);
}
~CharToWchar()
{
if (wchar != NULL)
mir_free(wchar);
}
WCHAR *detach()
{
WCHAR *ret = wchar;
wchar = NULL;
return ret;
}
const WCHAR * get() const
{
return wchar;
}
operator const WCHAR *() const
{
return wchar;
}
const WCHAR & operator[](int pos) const
{
return wchar[pos];
}
private:
WCHAR *wchar;
};
class TcharToChar
{
public:
TcharToChar(const TCHAR *str) : val(NULL)
{
if (str == NULL)
return;
#ifdef UNICODE
val = mir_u2a(str);
#else
val = str;
#endif
}
~TcharToChar()
{
#ifdef UNICODE
if (val != NULL)
mir_free(val);
#endif
}
char *detach()
{
#ifdef UNICODE
char *ret = val;
#else
char *ret = (val == NULL ? NULL : mir_strdup(val));
#endif
val = NULL;
return ret;
}
const char * get() const
{
return val;
}
operator const char *() const
{
return val;
}
const char & operator[](int pos) const
{
return val[pos];
}
private:
#ifdef UNICODE
char *val;
#else
const char *val;
#endif
};
class TcharToWchar
{
public:
TcharToWchar(const TCHAR *str) : val(NULL)
{
if (str == NULL)
return;
#ifdef UNICODE
val = str;
#else
val = mir_a2u(str);
#endif
}
~TcharToWchar()
{
#ifndef UNICODE
if (val != NULL)
mir_free(val);
#endif
}
WCHAR *detach()
{
#ifdef UNICODE
WCHAR *ret = (val == NULL ? NULL : mir_wstrdup(val));
#else
WCHAR *ret = val;
#endif
val = NULL;
return ret;
}
const WCHAR * get() const
{
return val;
}
operator const WCHAR *() const
{
return val;
}
const WCHAR & operator[](int pos) const
{
return val[pos];
}
private:
#ifdef UNICODE
const WCHAR *val;
#else
WCHAR *val;
#endif
};
class BstrToTchar
{
public:
BstrToTchar() : bstr(NULL)
#ifndef UNICODE
, tchar(NULL)
#endif
{
}
BstrToTchar(const WCHAR *str) : bstr(NULL)
#ifndef UNICODE
, tchar(NULL)
#endif
{
if (str == NULL)
return;
bstr = SysAllocString(str);
}
BstrToTchar(const char *str) : bstr(NULL)
#ifndef UNICODE
, tchar(NULL)
#endif
{
if (str == NULL)
return;
bstr = SysAllocString(CharToWchar(str));
}
~BstrToTchar()
{
if (bstr != NULL)
SysFreeString(bstr);
#ifndef UNICODE
freeTchar();
#endif
}
BSTR detach()
{
BSTR ret = bstr;
bstr = NULL;
return ret;
}
operator const TCHAR *()
{
#ifdef UNICODE
return bstr;
#else
if (tchar == NULL)
tchar = mir_u2a(bstr);
return tchar;
#endif
}
operator const BSTR() const
{
return bstr;
}
operator BSTR *()
{
#ifndef UNICODE
freeTchar();
#endif
return &bstr;
}
private:
BSTR bstr;
#ifndef UNICODE
TCHAR *tchar;
void freeTchar()
{
if (tchar != NULL)
{
mir_free(tchar);
tchar = NULL;
}
}
#endif
};
#endif // __UTF8_HELPERS_H__
|