1 //===========================================================================
5 // ISO standard mbstowcs() routine
7 //===========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //===========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): jjohnstn
44 // Contributors: jjohnstn
46 // Purpose: Provide ISO C mbstowcs()
50 //####DESCRIPTIONEND####
52 //===========================================================================
54 // This code was based on newlib/libc/stdlib/mbstowcs.c and newlib/libc/stdlib/mbstowcs_r.c
55 // The following is the modified from the original newlib description:
59 <<mbstowcs>>---multibyte string to wide char converter
66 int mbstowcs(wchar_t *<[pwc]>, const char *<[s]>, size_t <[n]>);
70 int mbstowcs(<[pwc]>, <[s]>, <[n]>)
76 When CYGINT_LIBC_I18N_MB_REQUIRED is not defined, this is a minimal ANSI-conforming
77 implementation of <<mbstowcs>>. In this case, the
78 only ``multi-byte character sequences'' recognized are single bytes,
79 and they are ``converted'' to wide-char versions simply by byte
82 When CYGINT_LIBC_I18N_MB_REQUIRED is defined, this routine calls the LC_CTYPE locale mbtowc_fn
83 repeatedly to perform the conversion, passing a state variable to allow state dependent
84 decoding. The result is based on the locale setting which may
85 be restricted to a defined set of locales.
88 This implementation of <<mbstowcs>> returns <<0>> if
89 <[s]> is <<NULL>> or is the empty string;
90 it returns <<-1>> if CYGINT_LIBC_I18N_MB_REQUIRED and one of the
91 multi-byte characters is invalid or incomplete;
92 otherwise it returns the minimum of: <<n>> or the
93 number of multi-byte characters in <<s>> plus 1 (to
94 compensate for the nul character).
95 If the return value is -1, the state of the <<pwc>> string is
96 indeterminate. If the input has a length of 0, the output
97 string will be modified to contain a wchar_t nul terminator.
100 <<mbstowcs>> is required in the ANSI C standard. However, the precise
101 effects vary with the locale.
103 <<mbstowcs>> requires no supporting OS subroutines.
108 #include <pkgconf/libc_i18n.h> // Configuration header
112 #include <cyg/infra/cyg_type.h> // Common type definitions
113 #include <cyg/infra/cyg_trac.h> // Tracing support
114 #include <cyg/infra/cyg_ass.h> // Assertion support
116 #include <stdlib.h> // Header for this file
117 #include <string.h> // strcmp definition
118 #include <stddef.h> // size_t definition
119 #include "internal.h" // __current_ctype_locale
121 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_MBSTOWCS
122 # include <pkgconf/kernel.h> // kernel configuration
123 # include <cyg/kernel/thread.hxx> // per-thread data
124 # include <cyg/kernel/thread.inl> // per-thread data
125 # include <cyg/kernel/mutex.hxx> // mutexes
130 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_MBSTOWCS_TRACE_LEVEL)
131 static int mbstowcs_trace = CYGNUM_LIBC_I18N_MBSTOWCS_TRACE_LEVEL;
132 # define TL1 (0 < mbstowcs_trace)
142 mbstowcs ( wchar_t *pwcs, const char *s, size_t n )
146 CYG_REPORT_FUNCNAMETYPE( "mbstowcs", "returning %ud" );
147 CYG_REPORT_FUNCARG3( "pwcs=%08x, s=%08x, n=%ud", pwcs, s, n );
150 CYG_CHECK_DATA_PTR( pwcs, "pwcs is not a valid pointer!" );
152 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
154 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
156 CYG_TRACE2( TL1, "Retrieved mbstowcs_last address %08x containing %d",
164 int (*mbtowc_fn)(wchar_t *, const char *, size_t, int *) = __current_ctype_locale->mbtowc_fn;
170 bytes = mbtowc_fn (ptr, t, MB_CUR_MAX, &state);
174 CYG_REPORT_RETVAL( retval );
180 CYG_REPORT_RETVAL( retval );
189 CYG_REPORT_RETVAL( retval );
193 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
199 if ((*pwcs++ = (wchar_t) *s++) == 0)
206 CYG_REPORT_RETVAL( retval );