]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/i18n/v2_0/src/wcstombs.cxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / language / c / libc / i18n / v2_0 / src / wcstombs.cxx
1 //===========================================================================
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 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]>>.
95
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
99 <<n>> > 0.
100
101 PORTABILITY
102 <<wcstombs>> is required in the ANSI C standard.  However, the precise
103 effects vary with the locale.
104
105 <<wcstombs>> requires no supporting OS subroutines.
106 */
107
108 // CONFIGURATION
109
110 #include <pkgconf/libc_i18n.h>   // Configuration header
111
112 // INCLUDES
113
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
117 #include <locale.h>
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 
122
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
128 #endif
129
130 // TRACE
131
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)
135 #else
136 # define TL1 (0)
137 #endif
138
139 // STATICS
140
141 // FUNCTIONS
142
143 size_t 
144 wcstombs ( char *s, const wchar_t *pwcs, size_t n )
145 {
146   size_t  retval;
147   
148   CYG_REPORT_FUNCNAMETYPE( "wcstombs", "returning %ud" );
149   CYG_REPORT_FUNCARG3( "s=%08x, pwcs=%08x, n=%ud", s, pwcs, n );
150
151   if (s != NULL)
152     CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
153   if (pwcs != NULL)
154     CYG_CHECK_DATA_PTR( pwcs, "pwcs is not a valid pointer!" );
155   
156 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED  
157
158   char *ptr = s;
159   size_t max = n;
160   int state = 0;
161   char buff[8];
162   int i, num_to_copy;
163   int (*wctomb_fn)(char *, wchar_t, int *) = __current_ctype_locale->wctomb_fn;
164
165   if (wctomb_fn)
166     {
167       while (n > 0)
168         {
169           int bytes = (size_t)(wctomb_fn (buff, *pwcs, &state));
170           if (bytes == -1)
171             {
172               retval = (size_t)-1;
173               CYG_REPORT_RETVAL( retval );
174               return retval;
175             }
176           num_to_copy = ((int)n > bytes ? bytes : (int)n);
177           for (i = 0; i < num_to_copy; ++i)
178             *ptr++ = buff[i];
179           
180           if (*pwcs == 0x00)
181             {
182               retval = ptr - s - ((int)n >= bytes);
183               CYG_REPORT_RETVAL( retval );
184               return retval;
185             }
186           ++pwcs;
187           n -= num_to_copy;
188         }
189
190       retval = max;
191       CYG_REPORT_RETVAL( retval );
192       return retval;
193     }
194 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
195   
196   int count = 0;
197
198   if (s == NULL) {
199       while (*pwcs++ != 0) {
200           count++;
201       }
202   } else {
203       if (n != 0) {
204           do {
205               if ((*s++ = (char) *pwcs++) == 0) 
206                   break;
207               count++;
208           } while (--n != 0);
209       }
210   }
211   
212   retval = count;
213   CYG_REPORT_RETVAL( retval );
214   return retval;
215 } // wcstombs()
216
217 // EOF wcstombs.cxx