1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// wctomb.cxx
|
4 |
|
|
//
|
5 |
|
|
// ISO standard wctomb() 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-02
|
46 |
|
|
// Purpose: Provide ISO C wctomb() routine
|
47 |
|
|
// Description:
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//===========================================================================
|
53 |
|
|
//
|
54 |
|
|
// This code was based on newlib/libc/stdlib/wctomb.c
|
55 |
|
|
// The following is modified from the original newlib description:
|
56 |
|
|
//
|
57 |
|
|
/*
|
58 |
|
|
FUNCTION
|
59 |
|
|
<<wctomb>>---wide char to multibyte converter
|
60 |
|
|
|
61 |
|
|
INDEX
|
62 |
|
|
wctomb
|
63 |
|
|
|
64 |
|
|
ANSI_SYNOPSIS
|
65 |
|
|
#include <stdlib.h>
|
66 |
|
|
int wctomb(char *<[s]>, wchar_t <[wchar]>);
|
67 |
|
|
|
68 |
|
|
TRAD_SYNOPSIS
|
69 |
|
|
#include <stdlib.h>
|
70 |
|
|
int wctomb(<[s]>, <[wchar]>)
|
71 |
|
|
char *<[s]>;
|
72 |
|
|
wchar_t <[wchar]>;
|
73 |
|
|
|
74 |
|
|
DESCRIPTION
|
75 |
|
|
When CYG_LIBC_MB_CAPABLE is not defined, this is a minimal ANSI-conforming
|
76 |
|
|
implementation of <<wctomb>>. The
|
77 |
|
|
only ``wide characters'' recognized are single bytes,
|
78 |
|
|
and they are ``converted'' to themselves.
|
79 |
|
|
|
80 |
|
|
When CYG_LIBC_MB_CAPABLE is defined, this routine calls the LC_CTYPE locale wctomb_fn to perform
|
81 |
|
|
the conversion, passing a state variable to allow state dependent
|
82 |
|
|
decoding. The result is based on the locale setting which may
|
83 |
|
|
be restricted to a defined set of locales.
|
84 |
|
|
|
85 |
|
|
Each call to <<wctomb>> modifies <<*<[s]>>> unless <[s]> is a null
|
86 |
|
|
pointer or CYG_LIBC_MB_CAPABLE is defined and <[wchar]> is invalid.
|
87 |
|
|
|
88 |
|
|
RETURNS
|
89 |
|
|
This implementation of <<wctomb>> returns <<0>> if
|
90 |
|
|
<[s]> is <<NULL>>; it returns <<-1>> if CYG_LIBC_MB_CAPABLE is enabled
|
91 |
|
|
and the wchar is not a valid multi-byte character, it returns <<1>>
|
92 |
|
|
if CYG_LIBC_MB_CAPABLE is not defined or the wchar is in reality a single
|
93 |
|
|
byte character, otherwise it returns the number of bytes in the
|
94 |
|
|
multi-byte character.
|
95 |
|
|
|
96 |
|
|
PORTABILITY
|
97 |
|
|
<<wctomb>> is required in the ANSI C standard. However, the precise
|
98 |
|
|
effects vary with the locale.
|
99 |
|
|
|
100 |
|
|
<<wctomb>> requires no supporting OS subroutines.
|
101 |
|
|
*/
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
// CONFIGURATION
|
105 |
|
|
|
106 |
|
|
#include <pkgconf/libc_i18n.h> // Configuration header
|
107 |
|
|
|
108 |
|
|
// INCLUDES
|
109 |
|
|
|
110 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions
|
111 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
112 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
113 |
|
|
#include <locale.h>
|
114 |
|
|
#include <stdlib.h> // Header for this file
|
115 |
|
|
#include <string.h> // strcmp definition
|
116 |
|
|
#include <stddef.h> // size_t definition
|
117 |
|
|
#include "internal.h" // __current_ctype_locale definition
|
118 |
|
|
|
119 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
120 |
|
|
# include <pkgconf/kernel.h> // kernel configuration
|
121 |
|
|
# include <cyg/kernel/thread.hxx> // per-thread data
|
122 |
|
|
# include <cyg/kernel/thread.inl> // per-thread data
|
123 |
|
|
# include <cyg/kernel/mutex.hxx> // mutexes
|
124 |
|
|
#endif /* CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
125 |
|
|
|
126 |
|
|
// TRACE
|
127 |
|
|
|
128 |
|
|
#if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_WCTOMB_TRACE_LEVEL)
|
129 |
|
|
static int wctomb_trace = CYGNUM_LIBC_I18N_WCTOMB_TRACE_LEVEL;
|
130 |
|
|
# define TL1 (0 < wctomb_trace)
|
131 |
|
|
#else
|
132 |
|
|
# define TL1 (0)
|
133 |
|
|
#endif
|
134 |
|
|
|
135 |
|
|
// STATICS
|
136 |
|
|
|
137 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
138 |
|
|
# ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
139 |
|
|
static volatile cyg_ucount32 wctomb_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX;
|
140 |
|
|
static Cyg_Mutex wctomb_data_mutex CYG_INIT_PRIORITY(LIBC);
|
141 |
|
|
# else
|
142 |
|
|
static int cyg_libc_wctomb_last;
|
143 |
|
|
# endif
|
144 |
|
|
#endif
|
145 |
|
|
|
146 |
|
|
int
|
147 |
|
|
wctomb ( char *s, const wchar_t wchar )
|
148 |
|
|
{
|
149 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
150 |
|
|
int *state;
|
151 |
|
|
#endif
|
152 |
|
|
int retval;
|
153 |
|
|
|
154 |
|
|
CYG_REPORT_FUNCNAMETYPE( "wctomb", "returning %d" );
|
155 |
|
|
CYG_REPORT_FUNCARG2( "s=%08x, wchar=%08x", s, wchar );
|
156 |
|
|
|
157 |
|
|
if (s != NULL)
|
158 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
159 |
|
|
|
160 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
161 |
|
|
|
162 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
163 |
|
|
Cyg_Thread *self = Cyg_Thread::self();
|
164 |
|
|
|
165 |
|
|
// Get a per-thread data slot if we haven't got one already
|
166 |
|
|
// Do a simple test before locking and retrying test, as this is a
|
167 |
|
|
// rare situation
|
168 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==wctomb_data_index) {
|
169 |
|
|
wctomb_data_mutex.lock();
|
170 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==wctomb_data_index) {
|
171 |
|
|
|
172 |
|
|
// the kernel just throws an assert if this doesn't work
|
173 |
|
|
// FIXME: Should use real CDL to pre-allocate a slot at compile
|
174 |
|
|
// time to ensure there are enough slots
|
175 |
|
|
wctomb_data_index = self->new_data_index();
|
176 |
|
|
|
177 |
|
|
}
|
178 |
|
|
wctomb_data_mutex.unlock();
|
179 |
|
|
} // if
|
180 |
|
|
|
181 |
|
|
// we have a valid index now
|
182 |
|
|
|
183 |
|
|
state = (int *)self->get_data_ptr(wctomb_data_index);
|
184 |
|
|
#else /* not CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
185 |
|
|
state = &cyg_libc_wctomb_last;
|
186 |
|
|
#endif /* not CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
187 |
|
|
|
188 |
|
|
CYG_TRACE2( TL1, "Retrieved wctomb_last address %08x containing %d",
|
189 |
|
|
state, *state );
|
190 |
|
|
|
191 |
|
|
if (__current_ctype_locale->wctomb_fn)
|
192 |
|
|
{
|
193 |
|
|
retval = __current_ctype_locale->wctomb_fn (s, wchar, state);
|
194 |
|
|
CYG_REPORT_RETVAL( retval );
|
195 |
|
|
return retval;
|
196 |
|
|
}
|
197 |
|
|
#endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
|
198 |
|
|
|
199 |
|
|
if (s == NULL)
|
200 |
|
|
retval = 0;
|
201 |
|
|
else
|
202 |
|
|
{
|
203 |
|
|
*s = (char) wchar;
|
204 |
|
|
retval = 1;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
CYG_REPORT_RETVAL( retval );
|
208 |
|
|
return retval;
|
209 |
|
|
|
210 |
|
|
} // wctomb()
|
211 |
|
|
|
212 |
|
|
// EOF wctomb.cxx
|