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

Annotation of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 278 - (hide annotations)
Mon Jan 15 22:02:04 2007 UTC (18 years, 1 month ago) by twoaday
File size: 2540 byte(s)
See ChangeLog.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26