/[winpt]/trunk/Src/wptUTF8.cpp
ViewVC logotype

Annotation of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 464 - (hide annotations)
Tue Oct 9 09:27:57 2012 UTC (12 years, 4 months ago) by twoaday
File size: 3382 byte(s)


1 twoaday 193 /* wptUTF8.cpp - UTF8 conversation
2 twoaday 456 * Copyright (C) 2002, 2004, 2005, 2006, 2009, 2012 Timo Schulz
3 twoaday 2 *
4 werner 36 * This file is part of WinPT.
5     *
6     * WinPT is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * WinPT is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15 twoaday 2 */
16 werner 36 #ifdef HAVE_CONFIG_H
17     #include <config.h>
18     #endif
19    
20 twoaday 2 #include <windows.h>
21 werner 36 #include <stdlib.h>
22 twoaday 2 #include <stdio.h>
23     #include <string.h>
24     #include <ctype.h>
25 werner 36
26     #include "wptTypes.h"
27 twoaday 2 #include "wptErrors.h"
28    
29    
30 twoaday 464 /* Byte order mark that is usually used to indicate that the following
31     data is encoded in UTF-8. */
32     BYTE UTF8_BOM[] = {0xEF, 0xBB, 0xBF};
33    
34    
35 twoaday 328 /**
36 twoaday 456 * Convert the given intput string, which is encoded with the locale
37 twoaday 328 * setting, into UTF-8 representation.
38     */
39 twoaday 187 char*
40     native_to_utf8 (const char *string)
41 werner 36 {
42 twoaday 456 int n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
43 twoaday 187 if (n < 0)
44     return NULL;
45    
46 twoaday 456 wchar_t *result = new wchar_t[n+1];
47 twoaday 187 if (!result)
48     BUG (0);
49    
50     n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
51     if (n < 0) {
52 twoaday 201 free_if_alloc (result);
53 twoaday 187 return NULL;
54 werner 36 }
55 twoaday 187
56     n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL);
57     if (n < 0)
58     return NULL;
59    
60 twoaday 456 char *native = new char[n + 1];
61 twoaday 187 if (!native)
62     BUG (0);
63 twoaday 456 memset(native, 0, n + 1);
64 twoaday 187
65     n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
66     if (n < 0) {
67 twoaday 201 free_if_alloc (result);
68 twoaday 187 return NULL;
69     }
70    
71 twoaday 201 free_if_alloc (result);
72 twoaday 187 return native;
73 twoaday 128 }
74 werner 36
75 twoaday 185
76 twoaday 328 /**
77 twoaday 456 * Convert an UTF-8 string into an UTF-16 string.
78     */
79     wchar_t*
80     utf8_to_utf16(const char *string, size_t *retlen)
81     {
82     int n = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
83     if (n < 0)
84     return NULL;
85    
86     wchar_t *result = new wchar_t[n + 1];
87     if (!result)
88     BUG(0);
89    
90     n = MultiByteToWideChar(CP_UTF8, 0, string, -1, result, n);
91     if (n < 0) {
92     free_if_alloc(result);
93     return NULL;
94     }
95    
96     *retlen = n;
97     return result;
98     }
99    
100    
101     /**
102     * Convert the given string, which is encoded in UTF-8,
103 twoaday 328 * into the locale setting.
104     */
105 twoaday 187 char*
106 twoaday 185 utf8_to_native (const char *string)
107 twoaday 2 {
108 twoaday 456 int n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
109 twoaday 185 if (n < 0)
110     return NULL;
111 twoaday 2
112 twoaday 456 wchar_t *result = new wchar_t[n+1];
113 twoaday 185 if (!result)
114     BUG (0);
115 twoaday 2
116 twoaday 185 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
117     if (n < 0) {
118 twoaday 328 free_if_alloc (result);
119 twoaday 185 return NULL;
120     }
121 twoaday 128
122 twoaday 185 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
123     if (n < 0)
124     return NULL;
125 twoaday 2
126 twoaday 456 char *native = new char[n + 1];
127 twoaday 185 if (!native)
128     BUG (0);
129 twoaday 456 memset(native, 0, n + 1);
130 twoaday 2
131 twoaday 187 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
132 twoaday 185 if (n < 0) {
133 twoaday 328 free_if_alloc (result);
134 twoaday 185 return NULL;
135     }
136 twoaday 328
137     free_if_alloc (result);
138 twoaday 185 return native;
139 twoaday 2 }
140 werner 36
141    
142 twoaday 328 /**
143 twoaday 456 * Return -1 if the given string contains any 8-bit characters.
144 twoaday 328 * This is a helper to decide when to use UTF8 encoding.
145     */
146 werner 36 int
147 twoaday 193 is_8bit_string (const char *str)
148 werner 36 {
149 twoaday 328 for (size_t i = 0; i < strlen (str); i++) {
150 werner 36 if (str[i] & 0x80)
151     return -1;
152     }
153     return 0;
154 twoaday 128 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26