]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/i18n/v2_0/src/mbstowcs.cxx
Initial revision
[karo-tx-redboot.git] / packages / language / c / libc / i18n / v2_0 / src / mbstowcs.cxx
1 //===========================================================================
2 //
3 //      mbstowcs.cxx
4 //
5 //      ISO standard mbstowcs() 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 mbstowcs()
47 // Description: 
48 // Usage:       
49 //
50 //####DESCRIPTIONEND####
51 //
52 //===========================================================================
53 //
54 // This code was based on newlib/libc/stdlib/mbstowcs.c and newlib/libc/stdlib/mbstowcs_r.c
55 // The following is the modified from the original newlib description:
56 //
57 /*
58 FUNCTION
59 <<mbstowcs>>---multibyte string to wide char converter
60
61 INDEX
62         mbstowcs
63
64 ANSI_SYNOPSIS
65         #include <stdlib.h>
66         int mbstowcs(wchar_t *<[pwc]>, const char *<[s]>, size_t <[n]>);
67
68 TRAD_SYNOPSIS
69         #include <stdlib.h>
70         int mbstowcs(<[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 <<mbstowcs>>.  In this case, the
78 only ``multi-byte character sequences'' recognized are single bytes,
79 and they are ``converted'' to wide-char versions simply by byte
80 extension.
81
82 When CYGINT_LIBC_I18N_MB_REQUIRED is defined, this routine calls the LC_CTYPE locale mbtowc_fn
83 repeatedly to perform the conversion, passing a state variable to allow state dependent
84 decoding.  The result is based on the locale setting which may
85 be restricted to a defined set of locales.
86
87 RETURNS
88 This implementation of <<mbstowcs>> returns <<0>> if
89 <[s]> is <<NULL>> or is the empty string; 
90 it returns <<-1>> if CYGINT_LIBC_I18N_MB_REQUIRED and one of the
91 multi-byte characters is invalid or incomplete;
92 otherwise it returns the minimum of: <<n>> or the
93 number of multi-byte characters in <<s>> plus 1 (to
94 compensate for the nul character).
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.
98
99 PORTABILITY
100 <<mbstowcs>> is required in the ANSI C standard.  However, the precise
101 effects vary with the locale.
102
103 <<mbstowcs>> requires no supporting OS subroutines.
104 */
105
106 // CONFIGURATION
107
108 #include <pkgconf/libc_i18n.h>   // Configuration header
109
110 // INCLUDES
111
112 #include <cyg/infra/cyg_type.h>    // Common type definitions
113 #include <cyg/infra/cyg_trac.h>    // Tracing support
114 #include <cyg/infra/cyg_ass.h>     // Assertion support
115 #include <locale.h>
116 #include <stdlib.h>                // Header for this file
117 #include <string.h>                // strcmp definition
118 #include <stddef.h>                // size_t definition
119 #include "internal.h"           // __current_ctype_locale
120
121 #ifdef CYGSEM_LIBC_I18N_PER_THREAD_MBSTOWCS
122 # include <pkgconf/kernel.h>       // kernel configuration
123 # include <cyg/kernel/thread.hxx>  // per-thread data
124 # include <cyg/kernel/thread.inl>  // per-thread data
125 # include <cyg/kernel/mutex.hxx>   // mutexes
126 #endif
127
128 // TRACE
129
130 #if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_I18N_MBSTOWCS_TRACE_LEVEL)
131 static int mbstowcs_trace = CYGNUM_LIBC_I18N_MBSTOWCS_TRACE_LEVEL;
132 # define TL1 (0 < mbstowcs_trace)
133 #else
134 # define TL1 (0)
135 #endif
136
137 // STATICS
138
139 // FUNCTIONS
140
141 size_t
142 mbstowcs ( wchar_t *pwcs, const char *s, size_t n ) 
143 {
144   size_t  retval;
145   
146   CYG_REPORT_FUNCNAMETYPE( "mbstowcs", "returning %ud" );
147   CYG_REPORT_FUNCARG3( "pwcs=%08x, s=%08x, n=%ud", pwcs, s, n );
148
149   if (pwcs != NULL)
150     CYG_CHECK_DATA_PTR( pwcs, "pwcs is not a valid pointer!" );
151   if (s != NULL)
152     CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
153   
154 #ifdef CYGINT_LIBC_I18N_MB_REQUIRED  
155
156   CYG_TRACE2( TL1, "Retrieved mbstowcs_last address %08x containing %d",
157               state, *state );
158
159   wchar_t *ptr = pwcs;
160   size_t max = n;
161   int state = 0;
162   char *t = (char *)s;
163   int bytes;
164   int (*mbtowc_fn)(wchar_t *, const char *, size_t, int *) = __current_ctype_locale->mbtowc_fn;
165   
166   if (mbtowc_fn)
167     {
168       while (n > 0)
169         {
170           bytes = mbtowc_fn (ptr, t, MB_CUR_MAX, &state);
171           if (bytes == -1)
172             {
173               retval = (size_t)-1;
174               CYG_REPORT_RETVAL( retval );
175               return retval;
176             }
177           else if (bytes == 0)
178             {
179               retval = ptr - pwcs;
180               CYG_REPORT_RETVAL( retval );
181               return retval;
182             }
183           t += bytes;
184           ++ptr;
185           --n;
186         }
187       
188       retval = max;
189       CYG_REPORT_RETVAL( retval );
190       return retval;
191     }
192   
193 #endif /* CYGINT_LIBC_I18N_MB_REQUIRED */
194   
195   int count = 0;
196   
197   if (n != 0) {
198     do {
199       if ((*pwcs++ = (wchar_t) *s++) == 0)
200         break;
201       count++;
202     } while (--n != 0);
203   }
204   
205   retval = count;
206   CYG_REPORT_RETVAL( retval );
207   return retval;
208 } // mbstowcs()
209
210 // EOF mbstowcs.cxx