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

Contents of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 2829 byte(s)


1 /* wptUTF8.cpp - UTF8 conversation
2 * Copyright (C) 2002, 2004, 2005, 2006, 2009 Timo Schulz
3 *
4 * 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 */
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <windows.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include "wptTypes.h"
27 #include "wptErrors.h"
28
29
30 /**
31 * Converts the given intput string, which is encoded with the locale
32 * setting, into UTF-8 representation.
33 */
34 char*
35 native_to_utf8 (const char *string)
36 {
37 wchar_t *result;
38 char *native;
39 int n;
40
41 n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
42 if (n < 0)
43 return NULL;
44
45 result = new wchar_t[n+1];
46 if (!result)
47 BUG (0);
48
49 n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
50 if (n < 0) {
51 free_if_alloc (result);
52 return NULL;
53 }
54
55 n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL);
56 if (n < 0)
57 return NULL;
58
59 native = new char[n+1];
60 if (!native)
61 BUG (0);
62
63 n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
64 if (n < 0) {
65 free_if_alloc (result);
66 return NULL;
67 }
68
69 free_if_alloc (result);
70 //native[n] = '\0';
71 return native;
72 }
73
74
75 /**
76 * Converts the given string, which is encoded in UTF-8,
77 * into the locale setting.
78 */
79 char*
80 utf8_to_native (const char *string)
81 {
82 wchar_t *result;
83 char *native;
84 int n;
85
86 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
87 if (n < 0)
88 return NULL;
89
90 result = new wchar_t[n+1];
91 if (!result)
92 BUG (0);
93
94 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
95 if (n < 0) {
96 free_if_alloc (result);
97 return NULL;
98 }
99
100 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
101 if (n < 0)
102 return NULL;
103
104 native = new char[n+1];
105 if (!native)
106 BUG (0);
107
108 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
109 if (n < 0) {
110 free_if_alloc (result);
111 return NULL;
112 }
113
114 free_if_alloc (result);
115 //native[n] = '\0';
116 return native;
117 }
118
119
120 /**
121 * Returns -1 if the given string contains any 8-bit characters.
122 * This is a helper to decide when to use UTF8 encoding.
123 */
124 int
125 is_8bit_string (const char *str)
126 {
127 for (size_t i = 0; i < strlen (str); i++) {
128 if (str[i] & 0x80)
129 return -1;
130 }
131 return 0;
132 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26