os/ossrv/genericopenlibs/openenvcore/libc/src/wcrtomb.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/wcrtomb.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,100 @@
     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        : wcrtomb.cpp
    1.18 +// Part of     : MRT LIBC
    1.19 +// Contains the source for the wcrtomb 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 +
    1.32 +#include "wcharcnv.h"
    1.33 +
    1.34 +#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
    1.35 +#include "libc_wsd_defs.h"
    1.36 +#endif 
    1.37 +
    1.38 +#ifdef EMULATOR
    1.39 +
    1.40 +GET_STATIC_VAR_FROM_TLS(wcrtomb_mbs, mbstate_t)
    1.41 +#define mbs (*GET_WSD_VAR_NAME(wcrtomb_mbs, s)())
    1.42 +#endif //EMULATOR
    1.43 +
    1.44 +//-----------------------------------------------------------------------------
    1.45 +//Function Name : size_t wcrtomb (char * mbchar, wchar_t wc, mbstate_t* ps) 
    1.46 +//Description   : Converts a wide character to a multibyte character
    1.47 +//Return Value  : return If mbchar is null, the return value is true (non-zero)
    1.48 +//if multibyte characters have state-dependent encodings, or false (zero) 
    1.49 +//if they do not.
    1.50 +//-----------------------------------------------------------------------------
    1.51 +extern "C" {
    1.52 +
    1.53 +EXPORT_C size_t wcrtomb (char * mbchar, wchar_t wc, mbstate_t* ps)
    1.54 +{
    1.55 +#ifndef EMULATOR	
    1.56 +	static mbstate_t mbs;
    1.57 +#endif //EMULATOR	
    1.58 +
    1.59 +	if (ps == NULL)
    1.60 +		ps = &mbs;
    1.61 +
    1.62 +	mbstate_t *state = NULL;
    1.63 +	if(ps)
    1.64 +	{
    1.65 +		state = ps;
    1.66 +	}
    1.67 +	else
    1.68 +	{
    1.69 +		state =	&mbs;
    1.70 +	}
    1.71 +	if (mbchar)
    1.72 +	{
    1.73 +		//deal with the special null character case
    1.74 +		if (L'\0' == wc)
    1.75 +		{
    1.76 +			*mbchar = '\0';
    1.77 +			if ( state )
    1.78 +    	   	{
    1.79 +    	   		memset(state,0,sizeof(state));	
    1.80 +    	    	state->__count = _EUTF16InitialState;
    1.81 +    	    }
    1.82 +			return (1);
    1.83 +		}
    1.84 +		
    1.85 +		//return value of 0 is possible if the first half of a surrogate pair seen
    1.86 +		//crash is possible if mbchar is not at least MB_CUR_MAX bytes long
    1.87 +		return _Utf16ToUtf8(mbchar,wc, state,MB_CUR_MAX);
    1.88 +	}
    1.89 +	//calling with a null dest string is used to initialise shift state
    1.90 +	//effect is exactly as if internal dst buffer was used.
    1.91 +	//therefore we always return 1.
    1.92 +	else
    1.93 +	{
    1.94 +	    	if ( state )
    1.95 +	        {
    1.96 +	        	memset(state,0,sizeof(state));	
    1.97 +	        	state->__count = _EUTF16InitialState;
    1.98 +	        }
    1.99 +			return (1);
   1.100 +	}
   1.101 +} //end of function
   1.102 +
   1.103 +} //extern "C"