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

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

* empty log message *

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