sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Name : mbrtowc.cpp sl@0: // Part of : MRT LIBC sl@0: // Contains the source for the mbrtowc API implementation. sl@0: // Version : sl@0: // sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "wcharcnv.h" sl@0: sl@0: #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libc_wsd_defs.h" sl@0: #endif sl@0: sl@0: #ifdef EMULATOR sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(mbrtowc_mbs, mbstate_t) sl@0: #define mbs (*GET_WSD_VAR_NAME(mbrtowc_mbs, s)()) sl@0: #endif //EMULATOR sl@0: sl@0: extern "C" { sl@0: sl@0: //----------------------------------------------------------------------------- sl@0: //Function Name : size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, sl@0: // mbstate_t *ps) sl@0: //Description :inspects at most n bytes of the multibyte string starting sl@0: //at s, extracts the next complete multi-byte character, converts it to a wide sl@0: //character and stores it at *pwc.It updates the shift state *ps. sl@0: //Return Value : returns the number of bytes parsed from the multi-byte sl@0: //sequence starting at s, if a non-L'\0' wide character was recognized. It sl@0: //returns 0, if a Lâ\0â wide character was recognized. It returns (size_t)(-1) sl@0: //and sets errno to EILSEQ, if an invalid multibyte sequence was encountered. sl@0: //It returns (size_t)(-2) if it couldn't parse a complete multibyte character, sl@0: //meaning that n should be increased. sl@0: //----------------------------------------------------------------------------- sl@0: EXPORT_C size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps) sl@0: { sl@0: int rval = 0; sl@0: #ifndef EMULATOR sl@0: static mbstate_t mbs; sl@0: #endif //EMULATOR sl@0: sl@0: if (ps == NULL) sl@0: ps = &mbs; sl@0: sl@0: if (s) sl@0: { sl@0: wchar_t wide; sl@0: sl@0: //number of chars to convert has a max of MB_CUR_MAX sl@0: TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n); sl@0: sl@0: TPtrC8 src((const TUint8*)s, maxlen); sl@0: //length of 1 as we only want 1 wide character sl@0: TPtr16 awc((TUint16*)&wide, 1); sl@0: sl@0: TInt ret = ConvertToUnicodeFromUtf8(awc, src, ps); sl@0: sl@0: //return the number of chars converted which is the max number sl@0: //- the number not converted unless the character converted sl@0: //was the wide null character sl@0: if (ret >= 0) sl@0: { sl@0: //rval = (L'\0' != wide) ? maxlen - ret : 0; sl@0: if(L'\0' == wide) sl@0: { sl@0: rval = 0; sl@0: } sl@0: else sl@0: { sl@0: rval = ret; sl@0: } sl@0: sl@0: if (pwc) sl@0: { sl@0: //only assign the return if we have a target sl@0: *pwc = wide; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: rval = ret; sl@0: } sl@0: } sl@0: return (size_t)(rval); sl@0: sl@0: } //end of function sl@0: sl@0: } //extern "C"