1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// mbtowc_jp.cxx
|
4 |
|
|
//
|
5 |
|
|
// Internal __mbtowc_jp() 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-16
|
46 |
|
|
// Purpose: Provide internal use __mbtowc_jp() routine
|
47 |
|
|
// Description: Japanese locale version of mbtowc()
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//===========================================================================
|
53 |
|
|
//
|
54 |
|
|
// This code was taken from newlib/libc/stdlib/mbtowc_r.c
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
// CONFIGURATION
|
58 |
|
|
|
59 |
|
|
#include <pkgconf/libc_i18n.h> // Configuration header
|
60 |
|
|
|
61 |
|
|
// INCLUDES
|
62 |
|
|
|
63 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions
|
64 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
65 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
66 |
|
|
#include <locale.h>
|
67 |
|
|
#include <stdlib.h> // Header for this file
|
68 |
|
|
#include <string.h> // strcmp definition
|
69 |
|
|
#include <stddef.h> // size_t definition
|
70 |
|
|
#include "internal.h" // internal __isxxxx macros
|
71 |
|
|
|
72 |
|
|
// TRACE
|
73 |
|
|
|
74 |
|
|
#if defined(CYGDBG_USE_TRACING) && defined(CYGNUM_LIBC_STDLIB_MBTOWC_TRACE_LEVEL)
|
75 |
|
|
static int mbtowc_trace = CYGNUM_LIBC_STDLIB_MBTOWC_TRACE_LEVEL;
|
76 |
|
|
# define TL1 (0 < mbtowc_trace)
|
77 |
|
|
#else
|
78 |
|
|
# define TL1 (0)
|
79 |
|
|
#endif
|
80 |
|
|
|
81 |
|
|
// STATICS
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
#ifdef CYGFUN_LIBC_I18N_LOCALE_C_JIS
|
85 |
|
|
typedef enum { ESCAPE, DOLLAR, BRACKET, AT, B, J,
|
86 |
|
|
NUL, JIS_CHAR, OTHER, JIS_C_NUM } JIS_CHAR_TYPE;
|
87 |
|
|
typedef enum { ASCII, A_ESC, A_ESC_DL, JIS, JIS_1, JIS_2, J_ESC, J_ESC_BR,
|
88 |
|
|
J2_ESC, J2_ESC_BR, DONE, INV, JIS_S_NUM } JIS_STATE;
|
89 |
|
|
typedef enum { COPY_A, COPY_J, COPY_J2, MAKE_A, MAKE_J, NOOP, EMPTY, ERROR } JIS_ACTION;
|
90 |
|
|
|
91 |
|
|
/**************************************************************************************
|
92 |
|
|
* state/action tables for processing JIS encoding
|
93 |
|
|
* Where possible, switches to JIS are grouped with proceding JIS characters and switches
|
94 |
|
|
* to ASCII are grouped with preceding JIS characters. Thus, maximum returned length
|
95 |
|
|
* is 2 (switch to JIS) + 2 (JIS characters) + 2 (switch back to ASCII) = 6.
|
96 |
|
|
*************************************************************************************/
|
97 |
|
|
|
98 |
|
|
static JIS_STATE JIS_state_table[JIS_S_NUM][JIS_C_NUM] = {
|
99 |
|
|
/* ESCAPE DOLLAR BRACKET AT B J NUL JIS_CHAR OTHER */
|
100 |
|
|
/* ASCII */ { A_ESC, DONE, DONE, DONE, DONE, DONE, DONE, DONE, DONE },
|
101 |
|
|
/* A_ESC */ { DONE, A_ESC_DL, DONE, DONE, DONE, DONE, DONE, DONE, DONE },
|
102 |
|
|
/* A_ESC_DL */{ DONE, DONE, DONE, JIS, JIS, DONE, DONE, DONE, DONE },
|
103 |
|
|
/* JIS */ { J_ESC, JIS_1, JIS_1, JIS_1, JIS_1, JIS_1, INV, JIS_1, INV },
|
104 |
|
|
/* JIS_1 */ { INV, JIS_2, JIS_2, JIS_2, JIS_2, JIS_2, INV, JIS_2, INV },
|
105 |
|
|
/* JIS_2 */ { J2_ESC, DONE, DONE, DONE, DONE, DONE, INV, DONE, DONE },
|
106 |
|
|
/* J_ESC */ { INV, INV, J_ESC_BR, INV, INV, INV, INV, INV, INV },
|
107 |
|
|
/* J_ESC_BR */{ INV, INV, INV, INV, ASCII, ASCII, INV, INV, INV },
|
108 |
|
|
/* J2_ESC */ { INV, INV, J2_ESC_BR,INV, INV, INV, INV, INV, INV },
|
109 |
|
|
/* J2_ESC_BR*/{ INV, INV, INV, INV, DONE, DONE, INV, INV, INV },
|
110 |
|
|
};
|
111 |
|
|
|
112 |
|
|
static JIS_ACTION JIS_action_table[JIS_S_NUM][JIS_C_NUM] = {
|
113 |
|
|
/* ESCAPE DOLLAR BRACKET AT B J NUL JIS_CHAR OTHER */
|
114 |
|
|
/* ASCII */ { NOOP, COPY_A, COPY_A, COPY_A, COPY_A, COPY_A, EMPTY, COPY_A, COPY_A},
|
115 |
|
|
/* A_ESC */ { COPY_A, NOOP, COPY_A, COPY_A, COPY_A, COPY_A, COPY_A, COPY_A, COPY_A},
|
116 |
|
|
/* A_ESC_DL */{ COPY_A, COPY_A, COPY_A, MAKE_J, MAKE_J, COPY_A, COPY_A, COPY_A, COPY_A},
|
117 |
|
|
/* JIS */ { NOOP, NOOP, NOOP, NOOP, NOOP, NOOP, ERROR, NOOP, ERROR },
|
118 |
|
|
/* JIS_1 */ { ERROR, NOOP, NOOP, NOOP, NOOP, NOOP, ERROR, NOOP, ERROR },
|
119 |
|
|
/* JIS_2 */ { NOOP, COPY_J2, COPY_J2, COPY_J2, COPY_J2, COPY_J2, ERROR, COPY_J2, COPY_J2},
|
120 |
|
|
/* J_ESC */ { ERROR, ERROR, NOOP, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR },
|
121 |
|
|
/* J_ESC_BR */{ ERROR, ERROR, ERROR, ERROR, NOOP, NOOP, ERROR, ERROR, ERROR },
|
122 |
|
|
/* J2_ESC */ { ERROR, ERROR, NOOP, ERROR, ERROR, ERROR, ERROR, ERROR, ERROR },
|
123 |
|
|
/* J2_ESC_BR*/{ ERROR, ERROR, ERROR, ERROR, COPY_J, COPY_J, ERROR, ERROR, ERROR },
|
124 |
|
|
};
|
125 |
|
|
#endif // CYGFUN_LIBC_I18N_LOCALE_C_JIS
|
126 |
|
|
|
127 |
|
|
// FUNCTIONS
|
128 |
|
|
|
129 |
|
|
int
|
130 |
|
|
__mbtowc_jp ( wchar_t *pwc, const char *s, size_t n, int *state )
|
131 |
|
|
{
|
132 |
|
|
wchar_t dummy;
|
133 |
|
|
unsigned char *t = (unsigned char *)s;
|
134 |
|
|
int retval;
|
135 |
|
|
const char *cur_locale = __current_ctype_locale->name;
|
136 |
|
|
|
137 |
|
|
CYG_REPORT_FUNCNAMETYPE( "__mbtowc_jp", "returning %d" );
|
138 |
|
|
CYG_REPORT_FUNCARG4( "pwc=%08x, s=%08x, n=%ud, state=%08x", pwc, s, n, state );
|
139 |
|
|
|
140 |
|
|
if (pwc != NULL)
|
141 |
|
|
CYG_CHECK_DATA_PTR( pwc, "pwc is not a valid pointer!" );
|
142 |
|
|
if (s != NULL)
|
143 |
|
|
CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" );
|
144 |
|
|
CYG_CHECK_DATA_PTR( state, "state is not a valid pointer!" );
|
145 |
|
|
|
146 |
|
|
if (pwc == NULL)
|
147 |
|
|
pwc = &dummy;
|
148 |
|
|
|
149 |
|
|
if (s != NULL && n == 0)
|
150 |
|
|
{
|
151 |
|
|
retval = -1;
|
152 |
|
|
CYG_REPORT_RETVAL (retval);
|
153 |
|
|
return retval;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
if (cur_locale == NULL ||
|
157 |
|
|
(strlen (cur_locale) <= 1))
|
158 |
|
|
{ /* fall-through */ }
|
159 |
|
|
#ifdef CYGFUN_LIBC_I18N_LOCALE_C_SJIS
|
160 |
|
|
else if (!strcmp (cur_locale, "C-SJIS"))
|
161 |
|
|
{
|
162 |
|
|
int char1;
|
163 |
|
|
if (s == NULL)
|
164 |
|
|
{
|
165 |
|
|
retval = 0;
|
166 |
|
|
CYG_REPORT_RETVAL (retval);
|
167 |
|
|
return retval; /* not state-dependent */
|
168 |
|
|
}
|
169 |
|
|
char1 = *t;
|
170 |
|
|
if (_issjis1 (char1))
|
171 |
|
|
{
|
172 |
|
|
int char2 = t[1];
|
173 |
|
|
if (n <= 1)
|
174 |
|
|
retval = -1;
|
175 |
|
|
else if (_issjis2 (char2))
|
176 |
|
|
{
|
177 |
|
|
*pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
|
178 |
|
|
retval = 2;
|
179 |
|
|
}
|
180 |
|
|
else
|
181 |
|
|
retval = -1;
|
182 |
|
|
CYG_REPORT_RETVAL (retval);
|
183 |
|
|
return retval;
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
#endif /* CYGFUN_LIBC_I18N_LOCALE_C_SJIS */
|
187 |
|
|
#ifdef CYGFUN_LIBC_I18N_LOCALE_C_EUCJP
|
188 |
|
|
else if (!strcmp (cur_locale, "C-EUCJP"))
|
189 |
|
|
{
|
190 |
|
|
int char1;
|
191 |
|
|
if (s == NULL)
|
192 |
|
|
{
|
193 |
|
|
retval = 0; /* not state-dependent */
|
194 |
|
|
CYG_REPORT_RETVAL (retval);
|
195 |
|
|
return retval; /* not state-dependent */
|
196 |
|
|
}
|
197 |
|
|
char1 = *t;
|
198 |
|
|
if (_iseucjp (char1))
|
199 |
|
|
{
|
200 |
|
|
int char2 = t[1];
|
201 |
|
|
if (n <= 1)
|
202 |
|
|
retval = -1;
|
203 |
|
|
if (_iseucjp (char2))
|
204 |
|
|
{
|
205 |
|
|
*pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
|
206 |
|
|
retval = 2;
|
207 |
|
|
}
|
208 |
|
|
else
|
209 |
|
|
retval = -1;
|
210 |
|
|
CYG_REPORT_RETVAL (retval);
|
211 |
|
|
return retval;
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
#endif /* CYGFUN_LIBC_I18N_LOCALE_C_EUCJP */
|
215 |
|
|
#ifdef CYGFUN_LIBC_I18N_LOCALE_C_JIS
|
216 |
|
|
else if (!strcmp (cur_locale, "C-JIS"))
|
217 |
|
|
{
|
218 |
|
|
JIS_STATE curr_state;
|
219 |
|
|
JIS_ACTION action;
|
220 |
|
|
JIS_CHAR_TYPE ch;
|
221 |
|
|
unsigned char *ptr;
|
222 |
|
|
int i, curr_ch;
|
223 |
|
|
|
224 |
|
|
if (s == NULL)
|
225 |
|
|
{
|
226 |
|
|
*state = 0;
|
227 |
|
|
retval = 1;
|
228 |
|
|
CYG_REPORT_RETVAL (retval);
|
229 |
|
|
return retval; /* state-dependent */
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
curr_state = (*state == 0 ? ASCII : JIS);
|
233 |
|
|
ptr = t;
|
234 |
|
|
|
235 |
|
|
for (i = 0; i < (int)n; ++i)
|
236 |
|
|
{
|
237 |
|
|
curr_ch = t[i];
|
238 |
|
|
switch (curr_ch)
|
239 |
|
|
{
|
240 |
|
|
case ESC_CHAR:
|
241 |
|
|
ch = ESCAPE;
|
242 |
|
|
break;
|
243 |
|
|
case '$':
|
244 |
|
|
ch = DOLLAR;
|
245 |
|
|
break;
|
246 |
|
|
case '@':
|
247 |
|
|
ch = AT;
|
248 |
|
|
break;
|
249 |
|
|
case '(':
|
250 |
|
|
ch = BRACKET;
|
251 |
|
|
break;
|
252 |
|
|
case 'B':
|
253 |
|
|
ch = B;
|
254 |
|
|
break;
|
255 |
|
|
case 'J':
|
256 |
|
|
ch = J;
|
257 |
|
|
break;
|
258 |
|
|
case '\0':
|
259 |
|
|
ch = NUL;
|
260 |
|
|
break;
|
261 |
|
|
default:
|
262 |
|
|
if (_isjis (curr_ch))
|
263 |
|
|
ch = JIS_CHAR;
|
264 |
|
|
else
|
265 |
|
|
ch = OTHER;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
action = JIS_action_table[curr_state][ch];
|
269 |
|
|
curr_state = JIS_state_table[curr_state][ch];
|
270 |
|
|
|
271 |
|
|
switch (action)
|
272 |
|
|
{
|
273 |
|
|
case NOOP:
|
274 |
|
|
break;
|
275 |
|
|
case EMPTY:
|
276 |
|
|
*state = 0;
|
277 |
|
|
*pwc = (wchar_t)0;
|
278 |
|
|
retval = i;
|
279 |
|
|
CYG_REPORT_RETVAL (retval);
|
280 |
|
|
return retval;
|
281 |
|
|
case COPY_A:
|
282 |
|
|
*state = 0;
|
283 |
|
|
*pwc = (wchar_t)*ptr;
|
284 |
|
|
retval = i + 1;
|
285 |
|
|
CYG_REPORT_RETVAL (retval);
|
286 |
|
|
return retval;
|
287 |
|
|
case COPY_J:
|
288 |
|
|
*state = 0;
|
289 |
|
|
*pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
|
290 |
|
|
retval = i + 1;
|
291 |
|
|
CYG_REPORT_RETVAL (retval);
|
292 |
|
|
return retval;
|
293 |
|
|
case COPY_J2:
|
294 |
|
|
*state = 1;
|
295 |
|
|
*pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
|
296 |
|
|
retval = (ptr - t) + 2;
|
297 |
|
|
CYG_REPORT_RETVAL (retval);
|
298 |
|
|
return retval;
|
299 |
|
|
case MAKE_A:
|
300 |
|
|
case MAKE_J:
|
301 |
|
|
ptr = (unsigned char *)(t + i + 1);
|
302 |
|
|
break;
|
303 |
|
|
case ERROR:
|
304 |
|
|
default:
|
305 |
|
|
retval = -1;
|
306 |
|
|
CYG_REPORT_RETVAL (retval);
|
307 |
|
|
return retval;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
retval = -1; /* n < bytes needed */
|
313 |
|
|
CYG_REPORT_RETVAL (retval);
|
314 |
|
|
return retval;
|
315 |
|
|
}
|
316 |
|
|
#endif /* CYGFUN_LIBC_I18N_LOCALE_C_JIS */
|
317 |
|
|
|
318 |
|
|
/* otherwise this must be the "C" locale or unknown locale */
|
319 |
|
|
if (s == NULL)
|
320 |
|
|
retval = 0; /* not state-dependent */
|
321 |
|
|
else
|
322 |
|
|
{
|
323 |
|
|
if (pwc)
|
324 |
|
|
*pwc = (wchar_t)*t;
|
325 |
|
|
retval = (*t != '\0');
|
326 |
|
|
}
|
327 |
|
|
CYG_REPORT_RETVAL (retval);
|
328 |
|
|
return retval;
|
329 |
|
|
} // __mbtowc_jp()
|
330 |
|
|
|
331 |
|
|
// EOF mbtowc_jp.cxx
|