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

Annotation of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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


1 twoaday 193 /* wptUTF8.cpp - UTF8 conversation
2 twoaday 328 * Copyright (C) 2002, 2004, 2005, 2006, 2009 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 328 /**
31     * Converts the given intput string, which is encoded with the locale
32     * setting, into UTF-8 representation.
33     */
34 twoaday 187 char*
35     native_to_utf8 (const char *string)
36 werner 36 {
37 twoaday 187 wchar_t *result;
38     char *native;
39     int n;
40 werner 36
41 twoaday 187 n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
42     if (n < 0)
43     return NULL;
44    
45 twoaday 201 result = new wchar_t[n+1];
46 twoaday 187 if (!result)
47     BUG (0);
48    
49     n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
50     if (n < 0) {
51 twoaday 201 free_if_alloc (result);
52 twoaday 187 return NULL;
53 werner 36 }
54 twoaday 187
55     n = WideCharToMultiByte (CP_UTF8, 0, result, -1, NULL, 0, NULL, NULL);
56     if (n < 0)
57     return NULL;
58    
59 twoaday 201 native = new char[n+1];
60 twoaday 187 if (!native)
61     BUG (0);
62    
63     n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
64     if (n < 0) {
65 twoaday 201 free_if_alloc (result);
66 twoaday 187 return NULL;
67     }
68    
69 twoaday 201 free_if_alloc (result);
70 twoaday 328 //native[n] = '\0';
71 twoaday 187 return native;
72 twoaday 128 }
73 werner 36
74 twoaday 185
75 twoaday 328 /**
76     * Converts the given string, which is encoded in UTF-8,
77     * into the locale setting.
78     */
79 twoaday 187 char*
80 twoaday 185 utf8_to_native (const char *string)
81 twoaday 2 {
82 twoaday 185 wchar_t *result;
83     char *native;
84     int n;
85 twoaday 2
86 twoaday 185 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
87     if (n < 0)
88     return NULL;
89 twoaday 2
90 twoaday 328 result = new wchar_t[n+1];
91 twoaday 185 if (!result)
92     BUG (0);
93 twoaday 2
94 twoaday 185 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
95     if (n < 0) {
96 twoaday 328 free_if_alloc (result);
97 twoaday 185 return NULL;
98     }
99 twoaday 128
100 twoaday 185 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
101     if (n < 0)
102     return NULL;
103 twoaday 2
104 twoaday 328 native = new char[n+1];
105 twoaday 185 if (!native)
106     BUG (0);
107 twoaday 2
108 twoaday 187 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
109 twoaday 185 if (n < 0) {
110 twoaday 328 free_if_alloc (result);
111 twoaday 185 return NULL;
112     }
113 twoaday 328
114     free_if_alloc (result);
115     //native[n] = '\0';
116 twoaday 185 return native;
117 twoaday 2 }
118 werner 36
119    
120 twoaday 328 /**
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 werner 36 int
125 twoaday 193 is_8bit_string (const char *str)
126 werner 36 {
127 twoaday 328 for (size_t i = 0; i < strlen (str); i++) {
128 werner 36 if (str[i] & 0x80)
129     return -1;
130     }
131     return 0;
132 twoaday 128 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26