1 //===========================================================================
5 // ISO standard mbtowc() 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 mbtowc() routine
50 //####DESCRIPTIONEND####
52 //===========================================================================
54 // This code was based upon newlib/libc/stdlib/mbtowc.c
55 // The following is modified from the original newlib description:
59 <<mbtowc>>---multibyte to wide char converter
66 int mbtowc(wchar_t *<[pwc]>, const char *<[s]>, size_t <[n]>);
70 int mbtowc(<[pwc]>, <[s]>, <[n]>)
76 When CYGINT_LIBC_I18N_MB_REQUIRED is not defined, this is a minimal ANSI-conforming
77 implementation of <<mbtowc>>. In this case,
78 only ``multi-byte character sequences'' recognized are single bytes,
79 and they are ``converted'' to themselves.
80 Each call to <<mbtowc>> copies one character from <<*<[s]>>> to
81 <<*<[pwc]>>>, unless <[s]> is a null pointer. The argument n
84 When CYGINT_LIBC_I18N_MB_REQUIRED is defined, this routine calls the LC_CTYPE locale mbtowc_fn to perform
85 the conversion, passing a state variable to allow state dependent
86 decoding. The result is based on the locale setting which may
87 be restricted to a defined set of locales.
90 This implementation of <<mbtowc>> returns <<0>> if
91 <[s]> is <<NULL>> or is the empty string;
92 it returns <<1>> if not CYGINT_LIBC_I18N_MB_REQUIRED or
93 the character is a single-byte character; it returns <<-1>>
94 if n is <<0>> or the multi-byte character is invalid;
95 otherwise it returns the number of bytes in the multibyte character.
96 If the return value is -1, no changes are made to the <<pwc>>
97 output string. If the input is the empty string, a wchar_t nul
98 is placed in the output string and 0 is returned. If the input
99 has a length of 0, no changes are made to the <<pwc>> output string.
102 <<mbtowc>> is required in the ANSI C standard. However, the precise
103 effects vary with the locale.
105 <<mbtowc>> requires no supporting OS subroutines.
111 #include <pkgconf/libc_i18n.h> // Configuration header
115 #include <cyg/infra/cyg_type.h> // Common type definitions
116 #include <cyg/infra/cyg_trac.h> // Tracing support
117 #include <cyg/infra/cyg_ass.h> // Assertion support
119 #include <stdlib.h> // Header for this file
120 #include <string.h> // strcmp definition
121 #include <stddef.h> // size_t definition
122 #include "internal.h" // __current_ctype_locale
124 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
125 # include <pkgconf/kernel.h> // kernel configuration
126 # include <cyg/kernel/thread.hxx> // per-thread data
127 # include <cyg/kernel/thread.inl> // per-thread data
128 # include <cyg/kernel/mutex.hxx> // mutexes
129 #endif /* CYGSEM_LIBC_I18N_PER_THREAD_MB */
133 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_MBTOWC_TRACE_LEVEL)
134 static int mbtowc_trace = CYGNUM_LIBC_I18N_MBTOWC_TRACE_LEVEL;
135 # define TL1 (0 < mbtowc_trace)
142 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
143 # ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
144 static volatile Cyg_Thread::cyg_data_index
145 mbtowc_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX;
147 static Cyg_Mutex mbtowc_data_mutex CYG_INIT_PRIORITY(LIBC);
149 static int cyg_libc_mbtowc_last;
156 mbtowc ( wchar_t *pwc, const char *s, size_t n )
158 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
163 CYG_REPORT_FUNCNAMETYPE( "mbtowc", "returning %d" );
164 CYG_REPORT_FUNCARG3( "pwc=%08x, s=%08x, n=%ud", pwc, s, n );
167 CYG_CHECK_DATA_PTR( pwc, "pwc is not a valid pointer!" );
169 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
171 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
173 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
174 Cyg_Thread *self = Cyg_Thread::self();
176 // Get a per-thread data slot if we haven't got one already
177 // Do a simple test before locking and retrying test, as this is a
179 if (CYGNUM_KERNEL_THREADS_DATA_MAX==mbtowc_data_index) {
180 mbtowc_data_mutex.lock();
181 if (CYGNUM_KERNEL_THREADS_DATA_MAX==mbtowc_data_index) {
183 // FIXME: Should use real CDL to pre-allocate a slot at compile
184 // time to ensure there are enough slots
185 mbtowc_data_index = self->new_data_index();
187 CYG_ASSERT(mbtowc_data_index >= 0, "failed to allocate data index" );
189 mbtowc_data_mutex.unlock();
192 // we have a valid index now
194 state = (int *)self->get_data_ptr(mbtowc_data_index);
196 state = &cyg_libc_mbtowc_last;
199 CYG_TRACE2( TL1, "Retrieved mbtowc_last address %08x containing %d",
202 if (__current_ctype_locale->mbtowc_fn)
204 retval = __current_ctype_locale->mbtowc_fn (pwc, s, n, state);
205 CYG_REPORT_RETVAL( retval );
208 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
218 retval = (*s != '\0');
221 CYG_REPORT_RETVAL( retval );