1 |
786 |
skrzyp |
//===========================================================================
|
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 Free Software Foundation, 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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//===========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): jjohnstn
|
43 |
|
|
// Contributors: jjohnstn
|
44 |
|
|
// Date: 2000-11-01
|
45 |
|
|
// Purpose: Provide ISO C mblen()
|
46 |
|
|
// Description:
|
47 |
|
|
// Usage:
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//===========================================================================
|
52 |
|
|
//
|
53 |
|
|
// This code was based on newlib/libc/stdlib/mblen.c
|
54 |
|
|
// The following is modified from the original newlib description:
|
55 |
|
|
//
|
56 |
|
|
/*
|
57 |
|
|
FUNCTION
|
58 |
|
|
<<mblen>>---multibyte length function
|
59 |
|
|
|
60 |
|
|
INDEX
|
61 |
|
|
mblen
|
62 |
|
|
|
63 |
|
|
ANSI_SYNOPSIS
|
64 |
|
|
#include <stdlib.h>
|
65 |
|
|
int mblen(const char *<[s]>, size_t <[n]>);
|
66 |
|
|
|
67 |
|
|
TRAD_SYNOPSIS
|
68 |
|
|
#include <stdlib.h>
|
69 |
|
|
int mblen(<[s]>, <[n]>)
|
70 |
|
|
const char *<[s]>;
|
71 |
|
|
size_t <[n]>;
|
72 |
|
|
|
73 |
|
|
DESCRIPTION
|
74 |
|
|
When CYGINT_LIBC_I18N_MB_REQUIRED is not defined, this is a minimal ANSI-conforming
|
75 |
|
|
implementation of <<mblen>>. In this case, the
|
76 |
|
|
only ``multi-byte character sequences'' recognized are single bytes,
|
77 |
|
|
and thus <<1>> is returned unless <[s]> is the null pointer or
|
78 |
|
|
has a length of 0 or is the empty string.
|
79 |
|
|
|
80 |
|
|
When CYGINT_LIBC_I18N_MB_REQUIRED is defined, this routine calls the locale's LC_CTYPE mbtowc_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 |
|
|
RETURNS
|
86 |
|
|
This implementation of <<mblen>> returns <<0>> if
|
87 |
|
|
<[s]> is <<NULL>> or the empty string; it returns <<1>> if not CYGINT_LIBC_I18N_MB_REQUIRED or
|
88 |
|
|
the character is a single-byte character; it returns <<-1>>
|
89 |
|
|
if the multi-byte character is invalid; otherwise it returns
|
90 |
|
|
the number of bytes in the multibyte character.
|
91 |
|
|
|
92 |
|
|
PORTABILITY
|
93 |
|
|
<<mblen>> is required in the ANSI C standard. However, the precise
|
94 |
|
|
effects vary with the locale.
|
95 |
|
|
|
96 |
|
|
<<mblen>> requires no supporting OS subroutines.
|
97 |
|
|
*/
|
98 |
|
|
|
99 |
|
|
// CONFIGURATION
|
100 |
|
|
|
101 |
|
|
#include <pkgconf/libc_i18n.h> // Configuration header
|
102 |
|
|
|
103 |
|
|
// INCLUDES
|
104 |
|
|
|
105 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions
|
106 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
107 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
108 |
|
|
#include <locale.h>
|
109 |
|
|
#include <stdlib.h> // Header for this file
|
110 |
|
|
#include <string.h> // strcmp definition
|
111 |
|
|
#include <stddef.h> // size_t definition
|
112 |
|
|
#include "internal.h" // __current_ctype_locale definition
|
113 |
|
|
|
114 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
115 |
|
|
# include <pkgconf/kernel.h> // kernel configuration
|
116 |
|
|
# include <cyg/kernel/thread.hxx> // per-thread data
|
117 |
|
|
# include <cyg/kernel/thread.inl> // per-thread data
|
118 |
|
|
# include <cyg/kernel/mutex.hxx> // mutexes
|
119 |
|
|
#endif /* CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
120 |
|
|
|
121 |
|
|
// TRACE
|
122 |
|
|
|
123 |
|
|
#if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_MBLEN_TRACE_LEVEL)
|
124 |
|
|
static int mblen_trace = CYGNUM_LIBC_I18N_MBLEN_TRACE_LEVEL;
|
125 |
|
|
# define TL1 (0 < mblen_trace)
|
126 |
|
|
#else
|
127 |
|
|
# define TL1 (0)
|
128 |
|
|
#endif
|
129 |
|
|
|
130 |
|
|
// STATICS
|
131 |
|
|
|
132 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
133 |
|
|
# ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
134 |
|
|
static volatile Cyg_Thread::cyg_data_index
|
135 |
|
|
mblen_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX;
|
136 |
|
|
|
137 |
|
|
static Cyg_Mutex mblen_data_mutex CYG_INIT_PRIORITY(LIBC);
|
138 |
|
|
# else
|
139 |
|
|
static int cyg_libc_mblen_last;
|
140 |
|
|
# endif
|
141 |
|
|
#endif
|
142 |
|
|
|
143 |
|
|
// FUNCTIONS
|
144 |
|
|
|
145 |
|
|
int
|
146 |
|
|
mblen ( const char *s, size_t n )
|
147 |
|
|
{
|
148 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
149 |
|
|
int *state;
|
150 |
|
|
#endif
|
151 |
|
|
int retval;
|
152 |
|
|
|
153 |
|
|
CYG_REPORT_FUNCNAMETYPE( "mblen", "returning %d" );
|
154 |
|
|
CYG_REPORT_FUNCARG2( "s=%08x, n=%ud", s, n );
|
155 |
|
|
|
156 |
|
|
if (s != NULL)
|
157 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
158 |
|
|
|
159 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
160 |
|
|
|
161 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
162 |
|
|
Cyg_Thread *self = Cyg_Thread::self();
|
163 |
|
|
|
164 |
|
|
// Get a per-thread data slot if we haven't got one already
|
165 |
|
|
// Do a simple test before locking and retrying test, as this is a
|
166 |
|
|
// rare situation
|
167 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==mblen_data_index) {
|
168 |
|
|
mblen_data_mutex.lock();
|
169 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==mblen_data_index) {
|
170 |
|
|
|
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 |
|
|
CYG_ASSERT(mblen_data_index >= 0, "failed to allocate data index" );
|
176 |
|
|
}
|
177 |
|
|
mblen_data_mutex.unlock();
|
178 |
|
|
} // if
|
179 |
|
|
|
180 |
|
|
// we have a valid index now
|
181 |
|
|
|
182 |
|
|
state = (int *)self->get_data_ptr(mblen_data_index);
|
183 |
|
|
#else /* not CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
184 |
|
|
state = &cyg_libc_mblen_last;
|
185 |
|
|
#endif /* not CYGSEM_LIBC_I18N_PER_THREAD_MB */
|
186 |
|
|
|
187 |
|
|
CYG_TRACE2( TL1, "Retrieved mblen_last address %08x containing %d",
|
188 |
|
|
state, *state );
|
189 |
|
|
|
190 |
|
|
if (__current_ctype_locale->mbtowc_fn)
|
191 |
|
|
{
|
192 |
|
|
retval = __current_ctype_locale->mbtowc_fn (NULL, s, n, state);
|
193 |
|
|
CYG_REPORT_RETVAL( retval );
|
194 |
|
|
return retval;
|
195 |
|
|
}
|
196 |
|
|
#endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
|
197 |
|
|
|
198 |
|
|
if (s == NULL || *s == '\0')
|
199 |
|
|
retval = 0;
|
200 |
|
|
else if (n == 0)
|
201 |
|
|
retval = -1;
|
202 |
|
|
else
|
203 |
|
|
retval = 1;
|
204 |
|
|
|
205 |
|
|
CYG_REPORT_RETVAL( retval );
|
206 |
|
|
return retval;
|
207 |
|
|
} // mblen()
|
208 |
|
|
|
209 |
|
|
// EOF mblen.cxx
|