1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// wcstombs.cxx
|
4 |
|
|
//
|
5 |
|
|
// ISO standard wcstombs() 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 wcstombs()
|
47 |
|
|
// Description:
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//===========================================================================
|
53 |
|
|
//
|
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:
|
56 |
|
|
//
|
57 |
|
|
/*
|
58 |
|
|
FUNCTION
|
59 |
|
|
<<wcstombs>>---wide char string to multibyte string converter
|
60 |
|
|
|
61 |
|
|
INDEX
|
62 |
|
|
wcstombs
|
63 |
|
|
|
64 |
|
|
ANSI_SYNOPSIS
|
65 |
|
|
#include <stdlib.h>
|
66 |
|
|
int wcstombs(const char *<[s]>, wchar_t *<[pwc]>, size_t <[n]>);
|
67 |
|
|
|
68 |
|
|
TRAD_SYNOPSIS
|
69 |
|
|
#include <stdlib.h>
|
70 |
|
|
int wcstombs(<[s]>, <[pwc]>, <[n]>)
|
71 |
|
|
const char *<[s]>;
|
72 |
|
|
wchar_t *<[pwc]>;
|
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 <<wcstombs>>. In this case,
|
78 |
|
|
all wide-characters are expected to represent single bytes and so
|
79 |
|
|
are converted simply by casting to char.
|
80 |
|
|
|
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.
|
85 |
|
|
|
86 |
|
|
RETURNS
|
87 |
|
|
This implementation of <<wcstombs>> returns <<0>> if
|
88 |
|
|
<[s]> is <<NULL>> or 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.
|
94 |
|
|
|
95 |
|
|
If the return value is -1, the state of the <<pwc>> string is
|
96 |
|
|
indeterminate. If the input has a length of 0, the output
|
97 |
|
|
string will be modified to contain a wchar_t nul terminator if
|
98 |
|
|
<<n>> > 0.
|
99 |
|
|
|
100 |
|
|
PORTABILITY
|
101 |
|
|
<<wcstombs>> is required in the ANSI C standard. However, the precise
|
102 |
|
|
effects vary with the locale.
|
103 |
|
|
|
104 |
|
|
<<wcstombs>> requires no supporting OS subroutines.
|
105 |
|
|
*/
|
106 |
|
|
|
107 |
|
|
// CONFIGURATION
|
108 |
|
|
|
109 |
|
|
#include <pkgconf/libc_i18n.h> // Configuration header
|
110 |
|
|
|
111 |
|
|
// INCLUDES
|
112 |
|
|
|
113 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions
|
114 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
115 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
116 |
|
|
#include <locale.h>
|
117 |
|
|
#include <stdlib.h> // Header for this file
|
118 |
|
|
#include <string.h> // strcmp definition
|
119 |
|
|
#include <stddef.h> // size_t definition
|
120 |
|
|
#include "internal.h" // __current_ctype_locale
|
121 |
|
|
|
122 |
|
|
#ifdef CYGSEM_LIBC_I18N_PER_THREAD_WCSTOMBS
|
123 |
|
|
# include <pkgconf/kernel.h> // kernel configuration
|
124 |
|
|
# include <cyg/kernel/thread.hxx> // per-thread data
|
125 |
|
|
# include <cyg/kernel/thread.inl> // per-thread data
|
126 |
|
|
# include <cyg/kernel/mutex.hxx> // mutexes
|
127 |
|
|
#endif
|
128 |
|
|
|
129 |
|
|
// TRACE
|
130 |
|
|
|
131 |
|
|
#if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_WCSTOMBS_TRACE_LEVEL)
|
132 |
|
|
static int wcstombs_trace = CYGNUM_LIBC_I18N_WCSTOMBS_TRACE_LEVEL;
|
133 |
|
|
# define TL1 (0 < wcstombs_trace)
|
134 |
|
|
#else
|
135 |
|
|
# define TL1 (0)
|
136 |
|
|
#endif
|
137 |
|
|
|
138 |
|
|
// STATICS
|
139 |
|
|
|
140 |
|
|
// FUNCTIONS
|
141 |
|
|
|
142 |
|
|
size_t
|
143 |
|
|
wcstombs ( char *s, const wchar_t *pwcs, size_t n )
|
144 |
|
|
{
|
145 |
|
|
size_t retval;
|
146 |
|
|
|
147 |
|
|
CYG_REPORT_FUNCNAMETYPE( "wcstombs", "returning %ud" );
|
148 |
|
|
CYG_REPORT_FUNCARG3( "s=%08x, pwcs=%08x, n=%ud", s, pwcs, n );
|
149 |
|
|
|
150 |
|
|
if (s != NULL)
|
151 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
152 |
|
|
if (pwcs != NULL)
|
153 |
|
|
CYG_CHECK_DATA_PTR( pwcs, "pwcs is not a valid pointer!" );
|
154 |
|
|
|
155 |
|
|
#ifdef CYGINT_LIBC_I18N_MB_REQUIRED
|
156 |
|
|
|
157 |
|
|
char *ptr = s;
|
158 |
|
|
size_t max = n;
|
159 |
|
|
int state = 0;
|
160 |
|
|
char buff[8];
|
161 |
|
|
int i, num_to_copy;
|
162 |
|
|
int (*wctomb_fn)(char *, wchar_t, int *) = __current_ctype_locale->wctomb_fn;
|
163 |
|
|
|
164 |
|
|
if (wctomb_fn)
|
165 |
|
|
{
|
166 |
|
|
while (n > 0)
|
167 |
|
|
{
|
168 |
|
|
int bytes = (size_t)(wctomb_fn (buff, *pwcs, &state));
|
169 |
|
|
if (bytes == -1)
|
170 |
|
|
{
|
171 |
|
|
retval = (size_t)-1;
|
172 |
|
|
CYG_REPORT_RETVAL( retval );
|
173 |
|
|
return retval;
|
174 |
|
|
}
|
175 |
|
|
num_to_copy = ((int)n > bytes ? bytes : (int)n);
|
176 |
|
|
for (i = 0; i < num_to_copy; ++i)
|
177 |
|
|
*ptr++ = buff[i];
|
178 |
|
|
|
179 |
|
|
if (*pwcs == 0x00)
|
180 |
|
|
{
|
181 |
|
|
retval = ptr - s - ((int)n >= bytes);
|
182 |
|
|
CYG_REPORT_RETVAL( retval );
|
183 |
|
|
return retval;
|
184 |
|
|
}
|
185 |
|
|
++pwcs;
|
186 |
|
|
n -= num_to_copy;
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
retval = max;
|
190 |
|
|
CYG_REPORT_RETVAL( retval );
|
191 |
|
|
return retval;
|
192 |
|
|
}
|
193 |
|
|
#endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
|
194 |
|
|
|
195 |
|
|
int count = 0;
|
196 |
|
|
|
197 |
|
|
if (n != 0) {
|
198 |
|
|
do {
|
199 |
|
|
if ((*s++ = (char) *pwcs++) == 0)
|
200 |
|
|
break;
|
201 |
|
|
count++;
|
202 |
|
|
} while (--n != 0);
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
retval = count;
|
206 |
|
|
CYG_REPORT_RETVAL( retval );
|
207 |
|
|
return retval;
|
208 |
|
|
} // wcstombs()
|
209 |
|
|
|
210 |
|
|
// EOF wcstombs.cxx
|