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