]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/i18n/v2_0/src/mbtowc.cxx
Initial revision
[karo-tx-redboot.git] / packages / language / c / libc / i18n / v2_0 / src / mbtowc.cxx
1 //===========================================================================
2 //
3 //      mbtowc.cxx
4 //
5 //      ISO standard mbtowc() routine 
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):     jjohnstn
44 // Contributors:  jjohnstn
45 // Date:          2000-11-01
46 // Purpose:       Provide ISO C mbtowc() routine
47 // Description: 
48 // Usage:       
49 //
50 //####DESCRIPTIONEND####
51 //
52 //===========================================================================
53 //
54 // This code was based upon newlib/libc/stdlib/mbtowc.c
55 // The following is modified from the original newlib description:
56 //
57 /*
58 FUNCTION
59 <<mbtowc>>---multibyte to wide char converter
60
61 INDEX
62         mbtowc
63
64 ANSI_SYNOPSIS
65         #include <stdlib.h>
66         int mbtowc(wchar_t *<[pwc]>, const char *<[s]>, size_t <[n]>);
67
68 TRAD_SYNOPSIS
69         #include <stdlib.h>
70         int mbtowc(<[pwc]>, <[s]>, <[n]>)
71         wchar_t *<[pwc]>;
72         const char *<[s]>;
73         size_t <[n]>;
74
75 DESCRIPTION
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
82 is ignored.
83
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.
88
89 RETURNS
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.
100
101 PORTABILITY
102 <<mbtowc>> is required in the ANSI C standard.  However, the precise
103 effects vary with the locale.
104
105 <<mbtowc>> requires no supporting OS subroutines.
106 */
107
108
109 // CONFIGURATION
110
111 #include <pkgconf/libc_i18n.h>     // Configuration header
112
113 // INCLUDES
114
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
118 #include <locale.h>
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
123
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 */
130
131 // TRACE
132
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)
136 #else
137 # define TL1 (0)
138 #endif
139
140 // STATICS
141
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;
146
147 static Cyg_Mutex mbtowc_data_mutex CYG_INIT_PRIORITY(LIBC);
148 # else
149 static int cyg_libc_mbtowc_last;
150 # endif
151 #endif
152
153 // FUNCTIONS
154
155 int
156 mbtowc ( wchar_t *pwc, const char *s, size_t n ) 
157 {
158 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
159   int  *state;
160 #endif
161   int   retval;
162   
163   CYG_REPORT_FUNCNAMETYPE( "mbtowc", "returning %d" );
164   CYG_REPORT_FUNCARG3( "pwc=%08x, s=%08x, n=%ud", pwc, s, n );
165   
166   if (pwc != NULL)
167     CYG_CHECK_DATA_PTR( pwc, "pwc is not a valid pointer!" );
168   if (s != NULL)
169     CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
170
171 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
172
173 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
174   Cyg_Thread *self = Cyg_Thread::self();
175   
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
178   // rare situation
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) {
182       
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();
186       
187       CYG_ASSERT(mbtowc_data_index >= 0, "failed to allocate data index" );
188     }
189     mbtowc_data_mutex.unlock();
190   } // if
191   
192   // we have a valid index now
193   
194   state = (int *)self->get_data_ptr(mbtowc_data_index);
195 #else
196   state = &cyg_libc_mbtowc_last;
197 #endif
198   
199   CYG_TRACE2( TL1, "Retrieved mbtowc_last address %08x containing %d",
200               state, *state );
201
202   if (__current_ctype_locale->mbtowc_fn)
203     {
204       retval = __current_ctype_locale->mbtowc_fn (pwc, s, n, state);
205       CYG_REPORT_RETVAL( retval );
206       return retval;
207     }
208 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
209
210   if (s == NULL)
211     retval = 0;
212   else if (n == 0)
213     retval = -1;
214   else
215     {
216       if (pwc)
217         *pwc = (wchar_t) *s;
218       retval =  (*s != '\0');
219     }
220   
221   CYG_REPORT_RETVAL( retval );
222   return retval;
223 } // mbtowc()
224
225 // EOF mbtowc.cxx