| 1 |
27 |
unneback |
//===========================================================================
|
| 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_ucount32 mbtowc_data_index=CYGNUM_KERNEL_THREADS_DATA_MAX;
|
| 145 |
|
|
static Cyg_Mutex mbtowc_data_mutex CYG_INIT_PRIORITY(LIBC);
|
| 146 |
|
|
# else
|
| 147 |
|
|
static int cyg_libc_mbtowc_last;
|
| 148 |
|
|
# endif
|
| 149 |
|
|
#endif
|
| 150 |
|
|
|
| 151 |
|
|
// FUNCTIONS
|
| 152 |
|
|
|
| 153 |
|
|
int
|
| 154 |
|
|
mbtowc ( wchar_t *pwc, const char *s, size_t n )
|
| 155 |
|
|
{
|
| 156 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
| 157 |
|
|
int *state;
|
| 158 |
|
|
#endif
|
| 159 |
|
|
int retval;
|
| 160 |
|
|
|
| 161 |
|
|
CYG_REPORT_FUNCNAMETYPE( "mbtowc", "returning %d" );
|
| 162 |
|
|
CYG_REPORT_FUNCARG3( "pwc=%08x, s=%08x, n=%ud", pwc, s, n );
|
| 163 |
|
|
|
| 164 |
|
|
if (pwc != NULL)
|
| 165 |
|
|
CYG_CHECK_DATA_PTR( pwc, "pwc is not a valid pointer!" );
|
| 166 |
|
|
if (s != NULL)
|
| 167 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
| 168 |
|
|
|
| 169 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
| 170 |
|
|
|
| 171 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_MB
|
| 172 |
|
|
Cyg_Thread *self = Cyg_Thread::self();
|
| 173 |
|
|
|
| 174 |
|
|
// Get a per-thread data slot if we haven't got one already
|
| 175 |
|
|
// Do a simple test before locking and retrying test, as this is a
|
| 176 |
|
|
// rare situation
|
| 177 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==mbtowc_data_index) {
|
| 178 |
|
|
mbtowc_data_mutex.lock();
|
| 179 |
|
|
if (CYGNUM_KERNEL_THREADS_DATA_MAX==mbtowc_data_index) {
|
| 180 |
|
|
|
| 181 |
|
|
// the kernel just throws an assert if this doesn't work
|
| 182 |
|
|
// FIXME: Should use real CDL to pre-allocate a slot at compile
|
| 183 |
|
|
// time to ensure there are enough slots
|
| 184 |
|
|
mbtowc_data_index = self->new_data_index();
|
| 185 |
|
|
|
| 186 |
|
|
}
|
| 187 |
|
|
mbtowc_data_mutex.unlock();
|
| 188 |
|
|
} // if
|
| 189 |
|
|
|
| 190 |
|
|
// we have a valid index now
|
| 191 |
|
|
|
| 192 |
|
|
state = (int *)self->get_data_ptr(mbtowc_data_index);
|
| 193 |
|
|
#else
|
| 194 |
|
|
state = &cyg_libc_mbtowc_last;
|
| 195 |
|
|
#endif
|
| 196 |
|
|
|
| 197 |
|
|
CYG_TRACE2( TL1, "Retrieved mbtowc_last address %08x containing %d",
|
| 198 |
|
|
state, *state );
|
| 199 |
|
|
|
| 200 |
|
|
if (__current_ctype_locale->mbtowc_fn)
|
| 201 |
|
|
{
|
| 202 |
|
|
retval = __current_ctype_locale->mbtowc_fn (pwc, s, n, state);
|
| 203 |
|
|
CYG_REPORT_RETVAL( retval );
|
| 204 |
|
|
return retval;
|
| 205 |
|
|
}
|
| 206 |
|
|
#endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
|
| 207 |
|
|
|
| 208 |
|
|
if (s == NULL)
|
| 209 |
|
|
retval = 0;
|
| 210 |
|
|
else if (n == 0)
|
| 211 |
|
|
retval = -1;
|
| 212 |
|
|
else
|
| 213 |
|
|
{
|
| 214 |
|
|
if (pwc)
|
| 215 |
|
|
*pwc = (wchar_t) *s;
|
| 216 |
|
|
retval = (*s != '\0');
|
| 217 |
|
|
}
|
| 218 |
|
|
|
| 219 |
|
|
CYG_REPORT_RETVAL( retval );
|
| 220 |
|
|
return retval;
|
| 221 |
|
|
} // mbtowc()
|
| 222 |
|
|
|
| 223 |
|
|
// EOF mbtowc.cxx
|