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