os/ossrv/genericopenlibs/openenvcore/libc/src/mbrtowc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/mbrtowc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +// Copyright (c) 2005-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 +// Name        : mbrtowc.cpp
    1.18 +// Part of     : MRT LIBC
    1.19 +// Contains the source for the mbrtowc API implementation.
    1.20 +// Version     :
    1.21 +//
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <e32std.h>
    1.26 +#include <utf.h>
    1.27 +#include <stdlib.h>
    1.28 +#include <string.h>
    1.29 +#include <wchar.h>
    1.30 +#include <stdio.h>
    1.31 +#include "wcharcnv.h"
    1.32 +
    1.33 +#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
    1.34 +#include "libc_wsd_defs.h"
    1.35 +#endif 
    1.36 +
    1.37 +#ifdef EMULATOR
    1.38 +
    1.39 +GET_STATIC_VAR_FROM_TLS(mbrtowc_mbs, mbstate_t)
    1.40 +#define mbs (*GET_WSD_VAR_NAME(mbrtowc_mbs, s)())
    1.41 +#endif //EMULATOR
    1.42 +
    1.43 +extern "C" {
    1.44 +
    1.45 +//-----------------------------------------------------------------------------
    1.46 +//Function Name : size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, 
    1.47 +//				mbstate_t *ps) 
    1.48 +//Description   :inspects at most  n  bytes  of the  multibyte  string starting
    1.49 +//at s, extracts the next complete multi-byte character, converts it to a wide
    1.50 +//character and stores it at  *pwc.It  updates the shift state *ps.
    1.51 +//Return Value  : returns the number of bytes parsed from the multi-byte 
    1.52 +//sequence starting at s, if a non-L'\0' wide character  was  recognized. It 
    1.53 +//returns 0, if a Lâ\0â wide character was recognized. It returns (size_t)(-1)
    1.54 +//and sets errno to EILSEQ, if an invalid  multibyte sequence  was encountered.
    1.55 +//It returns (size_t)(-2) if it couldn't parse a complete multibyte character, 
    1.56 +//meaning that n should be increased.
    1.57 +//-----------------------------------------------------------------------------
    1.58 +EXPORT_C size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
    1.59 +{
    1.60 +	int rval = 0;
    1.61 +#ifndef EMULATOR	
    1.62 +	static mbstate_t mbs;
    1.63 +#endif //EMULATOR	
    1.64 +
    1.65 +	if (ps == NULL)
    1.66 +		ps = &mbs;
    1.67 +	
    1.68 +	if (s)
    1.69 +	{
    1.70 +		wchar_t wide;
    1.71 +
    1.72 +		//number of chars to convert has a max of MB_CUR_MAX
    1.73 +		TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n);
    1.74 +		
    1.75 +		TPtrC8 src((const TUint8*)s, maxlen);
    1.76 +		//length of 1 as we only want 1 wide character
    1.77 +		TPtr16 awc((TUint16*)&wide, 1);		
    1.78 +
    1.79 +		TInt ret = ConvertToUnicodeFromUtf8(awc, src, ps);
    1.80 +	
    1.81 +		//return the number of chars converted which is the max number 
    1.82 +		//- the number not converted unless the character converted 
    1.83 +		//was the wide null character
    1.84 +		if (ret >= 0)
    1.85 +		{
    1.86 +			//rval = (L'\0' != wide) ? maxlen - ret : 0;
    1.87 +			if(L'\0' == wide)
    1.88 +			{
    1.89 +				rval = 0;
    1.90 +			}
    1.91 +			else
    1.92 +			{
    1.93 +				rval = ret;
    1.94 +			}
    1.95 +
    1.96 +			if (pwc)
    1.97 +		    	{
    1.98 +			    	//only assign the return if we have a target
    1.99 +				*pwc = wide;	
   1.100 +			}
   1.101 +		}
   1.102 +		else
   1.103 +		{
   1.104 +			rval = ret;
   1.105 +		}
   1.106 +	}
   1.107 +	return (size_t)(rval);
   1.108 +
   1.109 +} //end of function
   1.110 +
   1.111 +} //extern "C"