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

Annotation of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (hide annotations)
Wed Mar 22 11:04:20 2006 UTC (18 years, 11 months ago) by twoaday
File size: 2692 byte(s)
2006-03-21  Timo Schulz  <ts@g10code.de>
 
        * wptUTF8.cpp (native_to_utf8): Use directly W32 API.
        (utf8_to_native): Likewise. Remove cp850 conversion.
        * wptKeyEditDlgs.cpp (do_find_userid): Correct UTF8 handling.
        * wptKeyManager.cpp (km_delete_keys): Do not reset 'with_seckey'
        flag.

Prepare new release...


1 twoaday 2 /* 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     *
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 twoaday 2 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19     */
20    
21 werner 36 #ifdef HAVE_CONFIG_H
22     #include <config.h>
23     #endif
24    
25 twoaday 2 #include <windows.h>
26 werner 36 #include <stdlib.h>
27 twoaday 2 #include <stdio.h>
28     #include <string.h>
29     #include <ctype.h>
30 werner 36
31     #include "wptTypes.h"
32 twoaday 2 #include "wptErrors.h"
33    
34    
35 twoaday 187 char*
36     native_to_utf8 (const char *string)
37 werner 36 {
38 twoaday 187 wchar_t *result;
39     char *native;
40     int n;
41 werner 36
42 twoaday 187 n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
43     if (n < 0)
44     return NULL;
45    
46     result = (wchar_t*)malloc ((n+1) * sizeof *result);
47     if (!result)
48     BUG (0);
49    
50     n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
51     if (n < 0) {
52     free (result);
53     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     native = (char*)malloc (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 (result);
67     return NULL;
68     }
69    
70     free (result);
71     return native;
72 twoaday 128 }
73 werner 36
74 twoaday 185
75     /* Convert utf8 string @str to native CP. */
76 twoaday 187 char*
77 twoaday 185 utf8_to_native (const char *string)
78 twoaday 2 {
79 twoaday 185 wchar_t *result;
80     char *native;
81     int n;
82 twoaday 2
83 twoaday 185 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
84     if (n < 0)
85     return NULL;
86 twoaday 2
87 twoaday 185 result = (wchar_t*)malloc ((n+1) * sizeof *result);
88     if (!result)
89     BUG (0);
90 twoaday 2
91 twoaday 185 n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
92     if (n < 0) {
93     free (result);
94     return NULL;
95     }
96 twoaday 128
97 twoaday 185 n = WideCharToMultiByte (GetACP (), 0, result, -1, NULL, 0, NULL, NULL);
98     if (n < 0)
99     return NULL;
100 twoaday 2
101 twoaday 185 native = (char*)malloc (n+1);
102     if (!native)
103     BUG (0);
104 twoaday 2
105 twoaday 187 n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
106 twoaday 185 if (n < 0) {
107     free (result);
108     return NULL;
109     }
110 werner 36
111 twoaday 187 free (result);
112 twoaday 185 return native;
113 twoaday 2 }
114 werner 36
115    
116     int
117     is_8bit_string (const char * str)
118     {
119     size_t i;
120    
121     for (i = 0; i < strlen (str); i++) {
122     if (str[i] & 0x80)
123     return -1;
124     }
125     return 0;
126 twoaday 128 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26