sl@0
|
1 |
// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// Rrun-time library routines for translating between multibyte and wide characters
|
sl@0
|
15 |
// mbstowcs, mbtowc, wcstombs, wctomb and mblen
|
sl@0
|
16 |
//
|
sl@0
|
17 |
//
|
sl@0
|
18 |
|
sl@0
|
19 |
#include <e32std.h>
|
sl@0
|
20 |
#include <utf.h>
|
sl@0
|
21 |
#include <stdlib.h>
|
sl@0
|
22 |
#include <string.h>
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
|
sl@0
|
26 |
|
sl@0
|
27 |
extern "C"
|
sl@0
|
28 |
{
|
sl@0
|
29 |
|
sl@0
|
30 |
/**
|
sl@0
|
31 |
Converts the multibyte character addressed by s into the corresponding UNICODE
|
sl@0
|
32 |
character pwc
|
sl@0
|
33 |
@return the length, in bytes, of the multibyte character for which it found
|
sl@0
|
34 |
a UNICODE equivalent
|
sl@0
|
35 |
@param pwc Is the address of a wide character, type wchar_t,
|
sl@0
|
36 |
to receive the UNICODE equivalent of s.
|
sl@0
|
37 |
@param s Points to the multibyte character to be converted to UNICODE.
|
sl@0
|
38 |
@param n Is the maximum width, in bytes, for which to scan s for a valid multibyte
|
sl@0
|
39 |
sequence. Regardless of the value of n, no more than MB_CUR_MAX bytes are examined.
|
sl@0
|
40 |
*/
|
sl@0
|
41 |
EXPORT_C int mbtowc (wchar_t *pwc, const char *s, size_t n)
|
sl@0
|
42 |
{
|
sl@0
|
43 |
|
sl@0
|
44 |
int rval = 0;
|
sl@0
|
45 |
if (s)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
wchar_t wide;
|
sl@0
|
48 |
|
sl@0
|
49 |
//number of chars to convert has a max of MB_CUR_MAX
|
sl@0
|
50 |
TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n);
|
sl@0
|
51 |
|
sl@0
|
52 |
TPtrC8 src((const TUint8*)s, maxlen);
|
sl@0
|
53 |
TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
|
sl@0
|
54 |
|
sl@0
|
55 |
TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
|
sl@0
|
56 |
|
sl@0
|
57 |
//return the number of chars converted which is the max number - the number not converted
|
sl@0
|
58 |
//unless the character converted was the wide null character
|
sl@0
|
59 |
if (ret >= 0)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
rval = (L'\0' != wide) ? maxlen - ret : 0;
|
sl@0
|
62 |
|
sl@0
|
63 |
if (pwc)
|
sl@0
|
64 |
*pwc = wide; //only assign the return if we have a target
|
sl@0
|
65 |
}
|
sl@0
|
66 |
else
|
sl@0
|
67 |
return -1; //the conversion failed.
|
sl@0
|
68 |
|
sl@0
|
69 |
}
|
sl@0
|
70 |
return rval;
|
sl@0
|
71 |
}
|
sl@0
|
72 |
}
|
sl@0
|
73 |
|
sl@0
|
74 |
|
sl@0
|
75 |
extern "C"
|
sl@0
|
76 |
{
|
sl@0
|
77 |
|
sl@0
|
78 |
EXPORT_C int mbstowcs (wchar_t *wstring, const char * string, size_t size)
|
sl@0
|
79 |
{
|
sl@0
|
80 |
//convert the string "string" to wide characters
|
sl@0
|
81 |
//return number of wide characters
|
sl@0
|
82 |
|
sl@0
|
83 |
if (string)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
|
sl@0
|
86 |
if (wstring)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
TPtrC8 src((const TUint8*)string);
|
sl@0
|
89 |
TPtr16 awc((TUint16*)wstring, size); //max length of size
|
sl@0
|
90 |
|
sl@0
|
91 |
TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
|
sl@0
|
92 |
|
sl@0
|
93 |
if (ret >= 0)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
TUint len = awc.Length(); //return number of wide characters
|
sl@0
|
96 |
if (len < size)
|
sl@0
|
97 |
awc.ZeroTerminate();
|
sl@0
|
98 |
return len;
|
sl@0
|
99 |
}
|
sl@0
|
100 |
else
|
sl@0
|
101 |
return -1; //the conversion failed.
|
sl@0
|
102 |
}
|
sl@0
|
103 |
else
|
sl@0
|
104 |
{
|
sl@0
|
105 |
//we have no output string.
|
sl@0
|
106 |
//ms say return len required
|
sl@0
|
107 |
//gcc says nowt
|
sl@0
|
108 |
return 1+strlen(string); //max is could be
|
sl@0
|
109 |
}
|
sl@0
|
110 |
}
|
sl@0
|
111 |
return 0;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
|
sl@0
|
114 |
|
sl@0
|
115 |
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
|
sl@0
|
119 |
|
sl@0
|
120 |
extern "C"
|
sl@0
|
121 |
{
|
sl@0
|
122 |
|
sl@0
|
123 |
/**
|
sl@0
|
124 |
Converts a wide character to a multibyte character
|
sl@0
|
125 |
@return If s is null, the return value is true (non-zero) if multibyte
|
sl@0
|
126 |
characters have state-dependent encodings, or false (zero) if they do not.
|
sl@0
|
127 |
@param mbchar multibyte character
|
sl@0
|
128 |
@param wc wide character
|
sl@0
|
129 |
*/
|
sl@0
|
130 |
EXPORT_C int wctomb (char * mbchar, wchar_t wc)
|
sl@0
|
131 |
{
|
sl@0
|
132 |
if (mbchar)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
//deal with the special null character case
|
sl@0
|
135 |
if (L'\0' == wc)
|
sl@0
|
136 |
{
|
sl@0
|
137 |
*mbchar = '\0';
|
sl@0
|
138 |
return 1;
|
sl@0
|
139 |
}
|
sl@0
|
140 |
|
sl@0
|
141 |
//so we have possible character which is not null
|
sl@0
|
142 |
TPtr8 multi((TUint8*)mbchar, 0, MB_CUR_MAX); //limit max length to MB_CUR_MAX
|
sl@0
|
143 |
TPtrC16 wide ((const TUint16*)&wc);
|
sl@0
|
144 |
|
sl@0
|
145 |
TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
|
sl@0
|
146 |
|
sl@0
|
147 |
//ret has the number of wide characters left to convert, or an error
|
sl@0
|
148 |
if (ret >= 0) //we didn't get an error
|
sl@0
|
149 |
//return the number of characters in the output
|
sl@0
|
150 |
return multi.Length();
|
sl@0
|
151 |
else
|
sl@0
|
152 |
return -1;
|
sl@0
|
153 |
|
sl@0
|
154 |
}
|
sl@0
|
155 |
//calling with a null dest string is used to initialise shift state
|
sl@0
|
156 |
//we are only dealing with UTF8 which hasn't got one,
|
sl@0
|
157 |
//therefore we always return 0.
|
sl@0
|
158 |
else
|
sl@0
|
159 |
return 0;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
extern "C"
|
sl@0
|
164 |
{
|
sl@0
|
165 |
/**
|
sl@0
|
166 |
The wcstombs function converts a wide string to a string of multibyte
|
sl@0
|
167 |
characters.
|
sl@0
|
168 |
@return
|
sl@0
|
169 |
@param string multibyte string
|
sl@0
|
170 |
@param wstring wide string
|
sl@0
|
171 |
@param size number of bytes written to.
|
sl@0
|
172 |
*/
|
sl@0
|
173 |
EXPORT_C int wcstombs (char * string, const wchar_t * wstring, size_t size)
|
sl@0
|
174 |
{
|
sl@0
|
175 |
if (wstring)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
if (string)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
TPtr8 multi((TUint8*)string, size); //limit max length to size
|
sl@0
|
180 |
TPtrC16 wide((TText16*)wstring);
|
sl@0
|
181 |
|
sl@0
|
182 |
TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
|
sl@0
|
183 |
|
sl@0
|
184 |
if (ret >= 0) //we didn't get an error
|
sl@0
|
185 |
{
|
sl@0
|
186 |
TUint len = multi.Length();
|
sl@0
|
187 |
if (len < size)
|
sl@0
|
188 |
multi.ZeroTerminate();
|
sl@0
|
189 |
return len; //null terminate
|
sl@0
|
190 |
}
|
sl@0
|
191 |
else
|
sl@0
|
192 |
return -1;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
else
|
sl@0
|
195 |
{
|
sl@0
|
196 |
//we have a null output string
|
sl@0
|
197 |
//ms expects the length back.
|
sl@0
|
198 |
//gcc says nothing about it.
|
sl@0
|
199 |
//quick and dirty!!
|
sl@0
|
200 |
return 1 + (MB_CUR_MAX * wcslen(wstring)); //max it can be
|
sl@0
|
201 |
}
|
sl@0
|
202 |
}
|
sl@0
|
203 |
else
|
sl@0
|
204 |
return 0;
|
sl@0
|
205 |
}
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
|
sl@0
|
209 |
|
sl@0
|
210 |
extern "C"
|
sl@0
|
211 |
{
|
sl@0
|
212 |
/**
|
sl@0
|
213 |
If string is not a null pointer, the function returns the number of bytes in the
|
sl@0
|
214 |
multibyte string
|
sl@0
|
215 |
that constitute the next multibyte character,
|
sl@0
|
216 |
or it returns -1 if the next n (or the remaining) bytes do not constitute a valid
|
sl@0
|
217 |
multibyte character. mblen does not include the terminating null in the count of bytes.
|
sl@0
|
218 |
@return the number of bytes in the multibyte string
|
sl@0
|
219 |
@param string
|
sl@0
|
220 |
@param size
|
sl@0
|
221 |
*/
|
sl@0
|
222 |
EXPORT_C int mblen (const char *string, size_t size)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
if (string)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
wchar_t wide;
|
sl@0
|
227 |
|
sl@0
|
228 |
|
sl@0
|
229 |
//deal with an empty string without doing the conversion.
|
sl@0
|
230 |
if ('\0' == *string)
|
sl@0
|
231 |
return 0;
|
sl@0
|
232 |
|
sl@0
|
233 |
TInt maxlen = (size > MB_CUR_MAX ? MB_CUR_MAX : size);
|
sl@0
|
234 |
|
sl@0
|
235 |
TPtrC8 src((const TUint8*)string, maxlen);
|
sl@0
|
236 |
TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
|
sl@0
|
237 |
|
sl@0
|
238 |
TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
|
sl@0
|
239 |
|
sl@0
|
240 |
//return the number of chars converted which is the max number - the number not converted
|
sl@0
|
241 |
//unless the character converted was the wide null character
|
sl@0
|
242 |
if (ret >= 0)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
return ((L'\0' != wide) ? maxlen - ret : 0);
|
sl@0
|
245 |
}
|
sl@0
|
246 |
else
|
sl@0
|
247 |
return -1;
|
sl@0
|
248 |
|
sl@0
|
249 |
}
|
sl@0
|
250 |
//shift state would be initialised here if were using them
|
sl@0
|
251 |
return 0;
|
sl@0
|
252 |
}
|
sl@0
|
253 |
}
|