1 //===========================================================================
5 // ISO standard wcstombs() routine
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.
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.
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
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.
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.
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.
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####
43 // Author(s): jjohnstn
44 // Contributors: jjohnstn
46 // Purpose: Provide ISO C wcstombs()
50 //####DESCRIPTIONEND####
52 //===========================================================================
54 // This code was based on newlib/libc/stdlib/wcstombs.c and newlib/libc/stdlib/wcstombs_r.c
55 // The following is modified from the original newlib description:
59 <<wcstombs>>---wide char string to multibyte string converter
66 int wcstombs(const char *<[s]>, wchar_t *<[pwc]>, size_t <[n]>);
70 int wcstombs(<[s]>, <[pwc]>, <[n]>)
76 When CYGINT_LIBC_I18N_MB_REQUIRED is not defined, this is a minimal ANSI-conforming
77 implementation of <<wcstombs>>. In this case,
78 all wide-characters are expected to represent single bytes and so
79 are converted simply by casting to char.
81 When CYGINT_LIBC_I18N_MB_REQUIRED is defined, this routine calls the LC_CTYPE locale wcstomb_fn
82 repeatedly to perform 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.
87 This implementation of <<wcstombs>> returns <<0>> if
88 <[s]> is the empty string;
89 it returns <<-1>> if CYGINT_LIBC_I18N_MB_REQUIRED and one of the
90 wide-char characters does not represent a valid multi-byte character;
91 otherwise it returns the minimum of: <<n>> or the
92 number of bytes that are transferred to <<s>>, not including the
93 nul terminator. If <[s]> is <<NULL>> it returns the number of
94 bytes that would have been transferred, regardless of <<[n]>>.
96 If the return value is -1, the state of the <<pwc>> string is
97 indeterminate. If the input has a length of 0, the output
98 string will be modified to contain a wchar_t nul terminator if
102 <<wcstombs>> is required in the ANSI C standard. However, the precise
103 effects vary with the locale.
105 <<wcstombs>> requires no supporting OS subroutines.
110 #include <pkgconf/libc_i18n.h> // Configuration header
114 #include <cyg/infra/cyg_type.h> // Common type definitions
115 #include <cyg/infra/cyg_trac.h> // Tracing support
116 #include <cyg/infra/cyg_ass.h> // Assertion support
118 #include <stdlib.h> // Header for this file
119 #include <string.h> // strcmp definition
120 #include <stddef.h> // size_t definition
121 #include "internal.h" // __current_ctype_locale
123 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_WCSTOMBS
124 # include <pkgconf/kernel.h> // kernel configuration
125 # include <cyg/kernel/thread.hxx> // per-thread data
126 # include <cyg/kernel/thread.inl> // per-thread data
127 # include <cyg/kernel/mutex.hxx> // mutexes
132 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_WCSTOMBS_TRACE_LEVEL)
133 static int wcstombs_trace = CYGNUM_LIBC_I18N_WCSTOMBS_TRACE_LEVEL;
134 # define TL1 (0 < wcstombs_trace)
144 wcstombs ( char *s, const wchar_t *pwcs, size_t n )
148 CYG_REPORT_FUNCNAMETYPE( "wcstombs", "returning %ud" );
149 CYG_REPORT_FUNCARG3( "s=%08x, pwcs=%08x, n=%ud", s, pwcs, n );
152 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
154 CYG_CHECK_DATA_PTR( pwcs, "pwcs is not a valid pointer!" );
156 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED
163 int (*wctomb_fn)(char *, wchar_t, int *) = __current_ctype_locale->wctomb_fn;
169 int bytes = (size_t)(wctomb_fn (buff, *pwcs, &state));
173 CYG_REPORT_RETVAL( retval );
176 num_to_copy = ((int)n > bytes ? bytes : (int)n);
177 for (i = 0; i < num_to_copy; ++i)
182 retval = ptr - s - ((int)n >= bytes);
183 CYG_REPORT_RETVAL( retval );
191 CYG_REPORT_RETVAL( retval );
194 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
199 while (*pwcs++ != 0) {
205 if ((*s++ = (char) *pwcs++) == 0)
213 CYG_REPORT_RETVAL( retval );