source: trunk/src/kernel32/unicode.cpp@ 242

Last change on this file since 242 was 238, checked in by sandervl, 26 years ago

AsciiToUnicodeN bugfix

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