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

Contents of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (show annotations)
Sat Apr 22 18:30:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 2747 byte(s)
See ChangeLog.


1 /* wptUTF8.cpp - UTF8 conversation
2 * Copyright (C) 2002, 2004, 2005, 2006 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 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <windows.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "wptTypes.h"
32 #include "wptErrors.h"
33
34
35 char*
36 native_to_utf8 (const char *string)
37 {
38 wchar_t *result;
39 char *native;
40 int n;
41
42 n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
43 if (n < 0)
44 return NULL;
45
46 result = new wchar_t[n+1];
47 if (!result)
48 BUG (0);
49
50 n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
51 if (n < 0) {
52 free_if_alloc (result);
53 return NULL;
54 }
55
56 n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL);
57 if (n < 0)
58 return NULL;
59
60 native = new char[n+1];
61 if (!native)
62 BUG (0);
63
64 n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
65 if (n < 0) {
66 free_if_alloc (result);
67 return NULL;
68 }
69
70 free_if_alloc (result);
71 return native;
72 }
73
74
75 /* Convert utf8 string @str to native CP. */
76 char*
77 utf8_to_native (const char *string)
78 {
79 wchar_t *result;
80 char *native;
81 int n;
82
83 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
84 if (n < 0)
85 return NULL;
86
87 result = (wchar_t*)malloc ((n+1) * sizeof *result);
88 if (!result)
89 BUG (0);
90
91 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
92 if (n < 0) {
93 free (result);
94 return NULL;
95 }
96
97 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
98 if (n < 0)
99 return NULL;
100
101 native = (char*)malloc (n+1);
102 if (!native)
103 BUG (0);
104
105 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
106 if (n < 0) {
107 free (result);
108 return NULL;
109 }
110
111 free (result);
112 return native;
113 }
114
115
116 /* Return -1 if the string contains any 8-bit characters. */
117 int
118 is_8bit_string (const char *str)
119 {
120 size_t i;
121
122 for (i = 0; i < strlen (str); i++) {
123 if (str[i] & 0x80)
124 return -1;
125 }
126 return 0;
127 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26