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