1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LCHAR/MBWCCONV.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,253 @@
1.4 +// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Rrun-time library routines for translating between multibyte and wide characters
1.18 +// mbstowcs, mbtowc, wcstombs, wctomb and mblen
1.19 +//
1.20 +//
1.21 +
1.22 +#include <e32std.h>
1.23 +#include <utf.h>
1.24 +#include <stdlib.h>
1.25 +#include <string.h>
1.26 +
1.27 +
1.28 +
1.29 +
1.30 +extern "C"
1.31 +{
1.32 +
1.33 +/**
1.34 +Converts the multibyte character addressed by s into the corresponding UNICODE
1.35 +character pwc
1.36 +@return the length, in bytes, of the multibyte character for which it found
1.37 +a UNICODE equivalent
1.38 +@param pwc Is the address of a wide character, type wchar_t,
1.39 +to receive the UNICODE equivalent of s.
1.40 +@param s Points to the multibyte character to be converted to UNICODE.
1.41 +@param n Is the maximum width, in bytes, for which to scan s for a valid multibyte
1.42 +sequence. Regardless of the value of n, no more than MB_CUR_MAX bytes are examined.
1.43 +*/
1.44 +EXPORT_C int mbtowc (wchar_t *pwc, const char *s, size_t n)
1.45 + {
1.46 +
1.47 + int rval = 0;
1.48 + if (s)
1.49 + {
1.50 + wchar_t wide;
1.51 +
1.52 + //number of chars to convert has a max of MB_CUR_MAX
1.53 + TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n);
1.54 +
1.55 + TPtrC8 src((const TUint8*)s, maxlen);
1.56 + TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
1.57 +
1.58 + TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
1.59 +
1.60 + //return the number of chars converted which is the max number - the number not converted
1.61 + //unless the character converted was the wide null character
1.62 + if (ret >= 0)
1.63 + {
1.64 + rval = (L'\0' != wide) ? maxlen - ret : 0;
1.65 +
1.66 + if (pwc)
1.67 + *pwc = wide; //only assign the return if we have a target
1.68 + }
1.69 + else
1.70 + return -1; //the conversion failed.
1.71 +
1.72 + }
1.73 + return rval;
1.74 + }
1.75 +}
1.76 +
1.77 +
1.78 +extern "C"
1.79 +{
1.80 +
1.81 +EXPORT_C int mbstowcs (wchar_t *wstring, const char * string, size_t size)
1.82 + {
1.83 + //convert the string "string" to wide characters
1.84 + //return number of wide characters
1.85 +
1.86 + if (string)
1.87 + {
1.88 +
1.89 + if (wstring)
1.90 + {
1.91 + TPtrC8 src((const TUint8*)string);
1.92 + TPtr16 awc((TUint16*)wstring, size); //max length of size
1.93 +
1.94 + TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
1.95 +
1.96 + if (ret >= 0)
1.97 + {
1.98 + TUint len = awc.Length(); //return number of wide characters
1.99 + if (len < size)
1.100 + awc.ZeroTerminate();
1.101 + return len;
1.102 + }
1.103 + else
1.104 + return -1; //the conversion failed.
1.105 + }
1.106 + else
1.107 + {
1.108 + //we have no output string.
1.109 + //ms say return len required
1.110 + //gcc says nowt
1.111 + return 1+strlen(string); //max is could be
1.112 + }
1.113 + }
1.114 + return 0;
1.115 + }
1.116 +
1.117 +
1.118 +
1.119 +}
1.120 +
1.121 +
1.122 +
1.123 +extern "C"
1.124 +{
1.125 +
1.126 +/**
1.127 +Converts a wide character to a multibyte character
1.128 +@return If s is null, the return value is true (non-zero) if multibyte
1.129 +characters have state-dependent encodings, or false (zero) if they do not.
1.130 +@param mbchar multibyte character
1.131 +@param wc wide character
1.132 +*/
1.133 +EXPORT_C int wctomb (char * mbchar, wchar_t wc)
1.134 + {
1.135 + if (mbchar)
1.136 + {
1.137 + //deal with the special null character case
1.138 + if (L'\0' == wc)
1.139 + {
1.140 + *mbchar = '\0';
1.141 + return 1;
1.142 + }
1.143 +
1.144 + //so we have possible character which is not null
1.145 + TPtr8 multi((TUint8*)mbchar, 0, MB_CUR_MAX); //limit max length to MB_CUR_MAX
1.146 + TPtrC16 wide ((const TUint16*)&wc);
1.147 +
1.148 + TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
1.149 +
1.150 + //ret has the number of wide characters left to convert, or an error
1.151 + if (ret >= 0) //we didn't get an error
1.152 + //return the number of characters in the output
1.153 + return multi.Length();
1.154 + else
1.155 + return -1;
1.156 +
1.157 + }
1.158 + //calling with a null dest string is used to initialise shift state
1.159 + //we are only dealing with UTF8 which hasn't got one,
1.160 + //therefore we always return 0.
1.161 + else
1.162 + return 0;
1.163 + }
1.164 +}
1.165 +
1.166 +extern "C"
1.167 +{
1.168 +/**
1.169 +The wcstombs function converts a wide string to a string of multibyte
1.170 +characters.
1.171 +@return
1.172 +@param string multibyte string
1.173 +@param wstring wide string
1.174 +@param size number of bytes written to.
1.175 +*/
1.176 +EXPORT_C int wcstombs (char * string, const wchar_t * wstring, size_t size)
1.177 + {
1.178 + if (wstring)
1.179 + {
1.180 + if (string)
1.181 + {
1.182 + TPtr8 multi((TUint8*)string, size); //limit max length to size
1.183 + TPtrC16 wide((TText16*)wstring);
1.184 +
1.185 + TInt ret = CnvUtfConverter::ConvertFromUnicodeToUtf8(multi, wide);
1.186 +
1.187 + if (ret >= 0) //we didn't get an error
1.188 + {
1.189 + TUint len = multi.Length();
1.190 + if (len < size)
1.191 + multi.ZeroTerminate();
1.192 + return len; //null terminate
1.193 + }
1.194 + else
1.195 + return -1;
1.196 + }
1.197 + else
1.198 + {
1.199 + //we have a null output string
1.200 + //ms expects the length back.
1.201 + //gcc says nothing about it.
1.202 + //quick and dirty!!
1.203 + return 1 + (MB_CUR_MAX * wcslen(wstring)); //max it can be
1.204 + }
1.205 + }
1.206 + else
1.207 + return 0;
1.208 + }
1.209 +}
1.210 +
1.211 +
1.212 +
1.213 +extern "C"
1.214 +{
1.215 +/**
1.216 +If string is not a null pointer, the function returns the number of bytes in the
1.217 +multibyte string
1.218 +that constitute the next multibyte character,
1.219 +or it returns -1 if the next n (or the remaining) bytes do not constitute a valid
1.220 +multibyte character. mblen does not include the terminating null in the count of bytes.
1.221 +@return the number of bytes in the multibyte string
1.222 +@param string
1.223 +@param size
1.224 +*/
1.225 +EXPORT_C int mblen (const char *string, size_t size)
1.226 + {
1.227 + if (string)
1.228 + {
1.229 + wchar_t wide;
1.230 +
1.231 +
1.232 + //deal with an empty string without doing the conversion.
1.233 + if ('\0' == *string)
1.234 + return 0;
1.235 +
1.236 + TInt maxlen = (size > MB_CUR_MAX ? MB_CUR_MAX : size);
1.237 +
1.238 + TPtrC8 src((const TUint8*)string, maxlen);
1.239 + TPtr16 awc((TUint16*)&wide, 1); //length of 1 as we only want 1 wide character
1.240 +
1.241 + TInt ret = CnvUtfConverter::ConvertToUnicodeFromUtf8(awc, src);
1.242 +
1.243 + //return the number of chars converted which is the max number - the number not converted
1.244 + //unless the character converted was the wide null character
1.245 + if (ret >= 0)
1.246 + {
1.247 + return ((L'\0' != wide) ? maxlen - ret : 0);
1.248 + }
1.249 + else
1.250 + return -1;
1.251 +
1.252 + }
1.253 + //shift state would be initialised here if were using them
1.254 + return 0;
1.255 + }
1.256 +}