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

Last change on this file since 84 was 84, checked in by achimha, 26 years ago

Added my copyright because Patrick added his :-))

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