Update contrib.
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Rrun-time library routines for translating between multibyte and wide characters
15 // mbstowcs, mbtowc, wcstombs, wctomb and mblen
31 Converts the multibyte character addressed by s into the corresponding UNICODE
33 @return the length, in bytes, of the multibyte character for which it found
35 @param pwc Is the address of a wide character, type wchar_t,
36 to receive the UNICODE equivalent of s.
37 @param s Points to the multibyte character to be converted to UNICODE.
38 @param n Is the maximum width, in bytes, for which to scan s for a valid multibyte
39 sequence. Regardless of the value of n, no more than MB_CUR_MAX bytes are examined.
41 EXPORT_C int mbtowc (wchar_t *pwc, const char *s, size_t n)
49 //number of chars to convert has a max of MB_CUR_MAX
50 TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n);
52 TPtrC8 src((const TUint8*)s, maxlen);
53 TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
55 TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
57 //return the number of chars converted which is the max number - the number not converted
58 //unless the character converted was the wide null character
61 rval = (L'\0' != wide) ? maxlen - ret : 0;
64 *pwc = wide; //only assign the return if we have a target
67 return -1; //the conversion failed.
78 EXPORT_C int mbstowcs (wchar_t *wstring, const char * string, size_t size)
80 //convert the string "string" to wide characters
81 //return number of wide characters
88 TPtrC8 src((const TUint8*)string);
89 TPtr16 awc((TUint16*)wstring, size); //max length of size
91 TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
95 TUint len = awc.Length(); //return number of wide characters
101 return -1; //the conversion failed.
105 //we have no output string.
106 //ms say return len required
108 return 1+strlen(string); //max is could be
124 Converts a wide character to a multibyte character
125 @return If s is null, the return value is true (non-zero) if multibyte
126 characters have state-dependent encodings, or false (zero) if they do not.
127 @param mbchar multibyte character
128 @param wc wide character
130 EXPORT_C int wctomb (char * mbchar, wchar_t wc)
134 //deal with the special null character case
141 //so we have possible character which is not null
142 TPtr8 multi((TUint8*)mbchar, 0, MB_CUR_MAX); //limit max length to MB_CUR_MAX
143 TPtrC16 wide ((const TUint16*)&wc);
145 TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
147 //ret has the number of wide characters left to convert, or an error
148 if (ret >= 0) //we didn't get an error
149 //return the number of characters in the output
150 return multi.Length();
155 //calling with a null dest string is used to initialise shift state
156 //we are only dealing with UTF8 which hasn't got one,
157 //therefore we always return 0.
166 The wcstombs function converts a wide string to a string of multibyte
169 @param string multibyte string
170 @param wstring wide string
171 @param size number of bytes written to.
173 EXPORT_C int wcstombs (char * string, const wchar_t * wstring, size_t size)
179 TPtr8 multi((TUint8*)string, size); //limit max length to size
180 TPtrC16 wide((TText16*)wstring);
182 TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
184 if (ret >= 0) //we didn't get an error
186 TUint len = multi.Length();
188 multi.ZeroTerminate();
189 return len; //null terminate
196 //we have a null output string
197 //ms expects the length back.
198 //gcc says nothing about it.
200 return 1 + (MB_CUR_MAX * wcslen(wstring)); //max it can be
213 If string is not a null pointer, the function returns the number of bytes in the
215 that constitute the next multibyte character,
216 or it returns -1 if the next n (or the remaining) bytes do not constitute a valid
217 multibyte character. mblen does not include the terminating null in the count of bytes.
218 @return the number of bytes in the multibyte string
222 EXPORT_C int mblen (const char *string, size_t size)
229 //deal with an empty string without doing the conversion.
233 TInt maxlen = (size > MB_CUR_MAX ? MB_CUR_MAX : size);
235 TPtrC8 src((const TUint8*)string, maxlen);
236 TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
238 TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
240 //return the number of chars converted which is the max number - the number not converted
241 //unless the character converted was the wide null character
244 return ((L'\0' != wide) ? maxlen - ret : 0);
250 //shift state would be initialised here if were using them