1 | /* $Id: unicode.cpp,v 1.1 1999-05-24 20:19:43 ktk Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | *
|
---|
5 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
6 | *
|
---|
7 | */
|
---|
8 | /*
|
---|
9 | * Unicode functions
|
---|
10 | *
|
---|
11 | * Copyright 1998 Joel Troster
|
---|
12 | *
|
---|
13 | */
|
---|
14 | #include <os2win.h>
|
---|
15 | #include <winnls.h>
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include "misc.h"
|
---|
19 |
|
---|
20 | static UconvObject uconv_object = NULL;
|
---|
21 | BOOL getUconvObject( void )
|
---|
22 | {
|
---|
23 | int rc;
|
---|
24 | BOOL ret;
|
---|
25 | if ( uconv_object )
|
---|
26 | ret = TRUE;
|
---|
27 | else
|
---|
28 | {
|
---|
29 | rc = UniCreateUconvObject( (UniChar*)L"", &uconv_object );
|
---|
30 | if ( rc == ULS_SUCCESS )
|
---|
31 | ret = TRUE;
|
---|
32 | else
|
---|
33 | {
|
---|
34 | uconv_object = NULL; // to make sure
|
---|
35 | return FALSE;
|
---|
36 | }
|
---|
37 | dprintf(("UniCreateUconvObject(%d)\n", rc ));
|
---|
38 | }
|
---|
39 | return ret;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //******************************************************************************
|
---|
43 | //Not identical, but close enough
|
---|
44 | //******************************************************************************
|
---|
45 | int WIN32API MultiByteToWideChar(UINT uCodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
|
---|
46 | int cchMultiByte, LPWSTR lpWideCharStr, int cchWideChar)
|
---|
47 | {
|
---|
48 | int i, len;
|
---|
49 |
|
---|
50 | // dprintf(("MultiByteToWideChar %s\n", lpMultiByteStr));
|
---|
51 |
|
---|
52 | if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
|
---|
53 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
54 | return(FALSE);
|
---|
55 | }
|
---|
56 | if(cchWideChar == 0) {//return space required for conversion
|
---|
57 | if(cchMultiByte == -1) cchMultiByte = strlen(lpMultiByteStr);
|
---|
58 | return(cchMultiByte); //return length in wide chars
|
---|
59 | }
|
---|
60 | if(cchMultiByte == -1)
|
---|
61 | cchMultiByte = strlen(lpMultiByteStr);
|
---|
62 |
|
---|
63 | len = min(cchWideChar, cchMultiByte);
|
---|
64 | for(i=0;i<=len;i++) { //including 0 terminator
|
---|
65 | lpWideCharStr[i] = lpMultiByteStr[i];
|
---|
66 | }
|
---|
67 | return(len);
|
---|
68 | }
|
---|
69 | //******************************************************************************
|
---|
70 | //******************************************************************************
|
---|
71 | //Not identical, close enough
|
---|
72 | //Forget about characters that can't be mapped; just do it
|
---|
73 | //******************************************************************************
|
---|
74 | int WIN32API WideCharToMultiByte(UINT uCodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
|
---|
75 | int cchWideChar, LPSTR lpMultiByteStr,
|
---|
76 | int cchMultiByte, LPCSTR lpDefaultChar,
|
---|
77 | LPBOOL lpfUsedDefaultChar)
|
---|
78 | {
|
---|
79 | int i, len;
|
---|
80 |
|
---|
81 | // dprintf(("WideCharToMultiByte\n"));
|
---|
82 |
|
---|
83 | if((int)lpMultiByteStr == (int)lpWideCharStr) {//not allowed
|
---|
84 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
85 | return(FALSE);
|
---|
86 | }
|
---|
87 | if(cchMultiByte == 0) {//return space required for conversion
|
---|
88 | if(cchWideChar == -1) cchWideChar = UniStrlen((UniChar *)lpWideCharStr);
|
---|
89 | return(cchWideChar);
|
---|
90 | }
|
---|
91 | if(cchWideChar == -1)
|
---|
92 | cchWideChar = UniStrlen((UniChar*)lpWideCharStr);
|
---|
93 |
|
---|
94 | len = min(cchWideChar, cchMultiByte);
|
---|
95 | for(i=0;i<=len;i++) { //including 0 terminator
|
---|
96 | lpMultiByteStr[i] = (UCHAR)lpWideCharStr[i];
|
---|
97 | }
|
---|
98 | return(len);
|
---|
99 | }
|
---|
100 | //******************************************************************************
|
---|
101 | //******************************************************************************
|
---|
102 | BOOL WIN32API GetCPInfo(UINT uCodePage, CPINFO *lpCPInfo)
|
---|
103 | {
|
---|
104 | dprintf(("GetCPInfo (%d), not correctly implemented\n", uCodePage));
|
---|
105 | memset(lpCPInfo, 0, sizeof(CPINFO));
|
---|
106 | lpCPInfo->MaxCharSize = 1;
|
---|
107 | lpCPInfo->DefaultChar[0] = 'a';
|
---|
108 |
|
---|
109 | return(TRUE);
|
---|
110 | }
|
---|
111 | //******************************************************************************
|
---|
112 | //******************************************************************************
|
---|
113 | int WIN32API UnicodeToAscii(WCHAR *ustring, char *astring)
|
---|
114 | {
|
---|
115 | int ulen, i;
|
---|
116 | int rc;
|
---|
117 | size_t uni_chars_left;
|
---|
118 | size_t out_bytes_left;
|
---|
119 | size_t num_subs;
|
---|
120 | UniChar * in_buf;
|
---|
121 | char * out_buf;
|
---|
122 |
|
---|
123 | if(ustring == NULL)
|
---|
124 | return(NULL);
|
---|
125 |
|
---|
126 | // dprintf(("KERNEL32: UnicodeToAscii\n"));
|
---|
127 | ulen = UniStrlen((UniChar*)ustring);
|
---|
128 | if ( getUconvObject() )
|
---|
129 | {
|
---|
130 | uni_chars_left = ulen + 1;
|
---|
131 | out_bytes_left = uni_chars_left;
|
---|
132 | in_buf = (UniChar*)ustring;
|
---|
133 | out_buf = astring;
|
---|
134 | rc = UniUconvFromUcs( uconv_object,
|
---|
135 | &in_buf, &uni_chars_left,
|
---|
136 | (void**)&out_buf, &out_bytes_left,
|
---|
137 | &num_subs );
|
---|
138 | // dprintf(("KERNEL32: UnicodeToAscii(%d) '%s'\n", rc, astring ));
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | for(i=0;i<ulen;i++) {
|
---|
143 | astring[i] = (char)ustring[i];
|
---|
144 | }
|
---|
145 | astring[ulen] = 0;
|
---|
146 | }
|
---|
147 | return(ulen);
|
---|
148 | }
|
---|
149 | //******************************************************************************
|
---|
150 | //******************************************************************************
|
---|
151 | char * WIN32API UnicodeToAsciiString(WCHAR *ustring)
|
---|
152 | {
|
---|
153 | char *astring;
|
---|
154 |
|
---|
155 | if(ustring == NULL) return(NULL);
|
---|
156 |
|
---|
157 | astring = (char *)malloc( 1 + UniStrlen((UniChar*)ustring) );
|
---|
158 | UnicodeToAscii( ustring, astring );
|
---|
159 | return(astring);
|
---|
160 | }
|
---|
161 | //******************************************************************************
|
---|
162 | //SvL: 24-6-'97 - Added
|
---|
163 | //******************************************************************************
|
---|
164 | void WIN32API FreeAsciiString(char *astring)
|
---|
165 | {
|
---|
166 | if( astring )
|
---|
167 | free( astring );
|
---|
168 | }
|
---|
169 | //******************************************************************************
|
---|
170 | //******************************************************************************
|
---|
171 | void WIN32API AsciiToUnicode(char *ascii, WCHAR *unicode)
|
---|
172 | {
|
---|
173 | int rc;
|
---|
174 | int i, len;
|
---|
175 | size_t uni_chars_left;
|
---|
176 | size_t in_bytes_left;
|
---|
177 | size_t num_subs;
|
---|
178 | UniChar * out_buf;
|
---|
179 | char * in_buf;
|
---|
180 |
|
---|
181 |
|
---|
182 | dprintf(("KERNEL32: AsciiToUnicode(%s,%08xh)\n",
|
---|
183 | ascii,
|
---|
184 | unicode));
|
---|
185 |
|
---|
186 | /* @@@PH 98/06/07 */
|
---|
187 | if ( (ascii == NULL) || /* garbage in, garbage out ! */
|
---|
188 | (unicode == NULL) )
|
---|
189 | return;
|
---|
190 |
|
---|
191 | /* determine length of ascii string */
|
---|
192 | len = strlen(ascii);
|
---|
193 |
|
---|
194 | // dprintf(("KERNEL32: AsciiToUnicode %s\n", ascii));
|
---|
195 | if ( getUconvObject() )
|
---|
196 | {
|
---|
197 | in_buf = ascii;
|
---|
198 | in_bytes_left = len;
|
---|
199 | out_buf = (UniChar*)unicode;
|
---|
200 |
|
---|
201 | uni_chars_left = in_bytes_left +1;
|
---|
202 | dprintf(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left));
|
---|
203 |
|
---|
204 | rc = UniUconvToUcs( uconv_object,
|
---|
205 | (void**)&in_buf, &in_bytes_left,
|
---|
206 | &out_buf, &uni_chars_left,
|
---|
207 | &num_subs );
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | for(i=0;
|
---|
212 | i<len;
|
---|
213 | i++)
|
---|
214 | unicode[i] = ascii[i];
|
---|
215 | }
|
---|
216 |
|
---|
217 | unicode[len] = 0;
|
---|
218 | //SvL: Messes up the heap
|
---|
219 | // unicode[len+1] = 0; /* @@@PH 98/06/07 */
|
---|
220 | }
|
---|
221 | //******************************************************************************
|
---|
222 | //TODO: use OS/2 unicode stuff
|
---|
223 | //******************************************************************************
|
---|
224 | char *UnicodeToAscii(int length, WCHAR *NameString)
|
---|
225 | {
|
---|
226 | static char asciistring[256];
|
---|
227 | int i;
|
---|
228 |
|
---|
229 | if(length >= 255) length = 255;
|
---|
230 | for(i=0;i<length;i++) {
|
---|
231 | asciistring[i] = NameString[i] & 0xFF;
|
---|
232 | }
|
---|
233 | asciistring[length] = 0;
|
---|
234 | return(asciistring);
|
---|
235 | }
|
---|
236 | //******************************************************************************
|
---|
237 | //******************************************************************************
|
---|