os/ossrv/genericopenlibs/openenvcore/libc/src/wcrtomb.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Name        : wcrtomb.cpp
    15 // Part of     : MRT LIBC
    16 // Contains the source for the wcrtomb API implementation.
    17 // Version     :
    18 //
    19 
    20 
    21 
    22 #include <e32std.h>
    23 #include <utf.h>
    24 #include <stdlib.h>
    25 #include <string.h>
    26 #include <wchar.h>
    27 #include <stdio.h>
    28 
    29 #include "wcharcnv.h"
    30 
    31 #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
    32 #include "libc_wsd_defs.h"
    33 #endif 
    34 
    35 #ifdef EMULATOR
    36 
    37 GET_STATIC_VAR_FROM_TLS(wcrtomb_mbs, mbstate_t)
    38 #define mbs (*GET_WSD_VAR_NAME(wcrtomb_mbs, s)())
    39 #endif //EMULATOR
    40 
    41 //-----------------------------------------------------------------------------
    42 //Function Name : size_t wcrtomb (char * mbchar, wchar_t wc, mbstate_t* ps) 
    43 //Description   : Converts a wide character to a multibyte character
    44 //Return Value  : return If mbchar is null, the return value is true (non-zero)
    45 //if multibyte characters have state-dependent encodings, or false (zero) 
    46 //if they do not.
    47 //-----------------------------------------------------------------------------
    48 extern "C" {
    49 
    50 EXPORT_C size_t wcrtomb (char * mbchar, wchar_t wc, mbstate_t* ps)
    51 {
    52 #ifndef EMULATOR	
    53 	static mbstate_t mbs;
    54 #endif //EMULATOR	
    55 
    56 	if (ps == NULL)
    57 		ps = &mbs;
    58 
    59 	mbstate_t *state = NULL;
    60 	if(ps)
    61 	{
    62 		state = ps;
    63 	}
    64 	else
    65 	{
    66 		state =	&mbs;
    67 	}
    68 	if (mbchar)
    69 	{
    70 		//deal with the special null character case
    71 		if (L'\0' == wc)
    72 		{
    73 			*mbchar = '\0';
    74 			if ( state )
    75     	   	{
    76     	   		memset(state,0,sizeof(state));	
    77     	    	state->__count = _EUTF16InitialState;
    78     	    }
    79 			return (1);
    80 		}
    81 		
    82 		//return value of 0 is possible if the first half of a surrogate pair seen
    83 		//crash is possible if mbchar is not at least MB_CUR_MAX bytes long
    84 		return _Utf16ToUtf8(mbchar,wc, state,MB_CUR_MAX);
    85 	}
    86 	//calling with a null dest string is used to initialise shift state
    87 	//effect is exactly as if internal dst buffer was used.
    88 	//therefore we always return 1.
    89 	else
    90 	{
    91 	    	if ( state )
    92 	        {
    93 	        	memset(state,0,sizeof(state));	
    94 	        	state->__count = _EUTF16InitialState;
    95 	        }
    96 			return (1);
    97 	}
    98 } //end of function
    99 
   100 } //extern "C"