os/ossrv/genericopenlibs/openenvcore/libc/src/mbrtowc.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        : mbrtowc.cpp
sl@0
    15
// Part of     : MRT LIBC
sl@0
    16
// Contains the source for the mbrtowc 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
#include "wcharcnv.h"
sl@0
    29
sl@0
    30
#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    31
#include "libc_wsd_defs.h"
sl@0
    32
#endif 
sl@0
    33
sl@0
    34
#ifdef EMULATOR
sl@0
    35
sl@0
    36
GET_STATIC_VAR_FROM_TLS(mbrtowc_mbs, mbstate_t)
sl@0
    37
#define mbs (*GET_WSD_VAR_NAME(mbrtowc_mbs, s)())
sl@0
    38
#endif //EMULATOR
sl@0
    39
sl@0
    40
extern "C" {
sl@0
    41
sl@0
    42
//-----------------------------------------------------------------------------
sl@0
    43
//Function Name : size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, 
sl@0
    44
//				mbstate_t *ps) 
sl@0
    45
//Description   :inspects at most  n  bytes  of the  multibyte  string starting
sl@0
    46
//at s, extracts the next complete multi-byte character, converts it to a wide
sl@0
    47
//character and stores it at  *pwc.It  updates the shift state *ps.
sl@0
    48
//Return Value  : returns the number of bytes parsed from the multi-byte 
sl@0
    49
//sequence starting at s, if a non-L'\0' wide character  was  recognized. It 
sl@0
    50
//returns 0, if a Lâ\0â wide character was recognized. It returns (size_t)(-1)
sl@0
    51
//and sets errno to EILSEQ, if an invalid  multibyte sequence  was encountered.
sl@0
    52
//It returns (size_t)(-2) if it couldn't parse a complete multibyte character, 
sl@0
    53
//meaning that n should be increased.
sl@0
    54
//-----------------------------------------------------------------------------
sl@0
    55
EXPORT_C size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
sl@0
    56
{
sl@0
    57
	int rval = 0;
sl@0
    58
#ifndef EMULATOR	
sl@0
    59
	static mbstate_t mbs;
sl@0
    60
#endif //EMULATOR	
sl@0
    61
sl@0
    62
	if (ps == NULL)
sl@0
    63
		ps = &mbs;
sl@0
    64
	
sl@0
    65
	if (s)
sl@0
    66
	{
sl@0
    67
		wchar_t wide;
sl@0
    68
sl@0
    69
		//number of chars to convert has a max of MB_CUR_MAX
sl@0
    70
		TInt maxlen = (n > MB_CUR_MAX ? MB_CUR_MAX : n);
sl@0
    71
		
sl@0
    72
		TPtrC8 src((const TUint8*)s, maxlen);
sl@0
    73
		//length of 1 as we only want 1 wide character
sl@0
    74
		TPtr16 awc((TUint16*)&wide, 1);		
sl@0
    75
sl@0
    76
		TInt ret = ConvertToUnicodeFromUtf8(awc, src, ps);
sl@0
    77
	
sl@0
    78
		//return the number of chars converted which is the max number 
sl@0
    79
		//- the number not converted unless the character converted 
sl@0
    80
		//was the wide null character
sl@0
    81
		if (ret >= 0)
sl@0
    82
		{
sl@0
    83
			//rval = (L'\0' != wide) ? maxlen - ret : 0;
sl@0
    84
			if(L'\0' == wide)
sl@0
    85
			{
sl@0
    86
				rval = 0;
sl@0
    87
			}
sl@0
    88
			else
sl@0
    89
			{
sl@0
    90
				rval = ret;
sl@0
    91
			}
sl@0
    92
sl@0
    93
			if (pwc)
sl@0
    94
		    	{
sl@0
    95
			    	//only assign the return if we have a target
sl@0
    96
				*pwc = wide;	
sl@0
    97
			}
sl@0
    98
		}
sl@0
    99
		else
sl@0
   100
		{
sl@0
   101
			rval = ret;
sl@0
   102
		}
sl@0
   103
	}
sl@0
   104
	return (size_t)(rval);
sl@0
   105
sl@0
   106
} //end of function
sl@0
   107
sl@0
   108
} //extern "C"