OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [stdlib/] [wctomb_r.c] - Blame information for rev 520

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
#include <errno.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <wchar.h>
5
#include <locale.h>
6
#include "mbctype.h"
7
#include "local.h"
8
 
9
int (*__wctomb) (struct _reent *, char *, wchar_t, const char *charset,
10
                 mbstate_t *)
11
#ifdef __CYGWIN__
12
    = __utf8_wctomb;
13
#else
14
    = __ascii_wctomb;
15
#endif
16
 
17
int
18
_DEFUN (_wctomb_r, (r, s, wchar, state),
19
        struct _reent *r     _AND
20
        char          *s     _AND
21
        wchar_t        _wchar _AND
22
        mbstate_t     *state)
23
{
24
  return __wctomb (r, s, _wchar, __locale_charset (), state);
25
}
26
 
27
int
28
_DEFUN (__ascii_wctomb, (r, s, wchar, charset, state),
29
        struct _reent *r       _AND
30
        char          *s       _AND
31
        wchar_t        _wchar  _AND
32
        const char    *charset _AND
33
        mbstate_t     *state)
34
{
35
  /* Avoids compiler warnings about comparisons that are always false
36
     due to limited range when sizeof(wchar_t) is 2 but sizeof(wint_t)
37
     is 4, as is the case on cygwin.  */
38
  wint_t wchar = _wchar;
39
 
40
  if (s == NULL)
41
    return 0;
42
 
43
  if ((size_t)wchar >= 0x100)
44
    {
45
      r->_errno = EILSEQ;
46
      return -1;
47
    }
48
 
49
  *s = (char) wchar;
50
  return 1;
51
}
52
 
53
#ifdef _MB_CAPABLE
54
/* for some conversions, we use the __count field as a place to store a state value */
55
#define __state __count
56
 
57
int
58
_DEFUN (__utf8_wctomb, (r, s, wchar, charset, state),
59
        struct _reent *r       _AND
60
        char          *s       _AND
61
        wchar_t        _wchar  _AND
62
        const char    *charset _AND
63
        mbstate_t     *state)
64
{
65
  wint_t wchar = _wchar;
66
  int ret = 0;
67
 
68
  if (s == NULL)
69
    return 0; /* UTF-8 encoding is not state-dependent */
70
 
71
  if (sizeof (wchar_t) == 2 && state->__count == -4
72
      && (wchar < 0xdc00 || wchar >= 0xdfff))
73
    {
74
      /* There's a leftover lone high surrogate.  Write out the CESU-8 value
75
         of the surrogate and proceed to convert the given character.  Note
76
         to return extra 3 bytes. */
77
      wchar_t tmp;
78
      tmp = (state->__value.__wchb[0] << 16 | state->__value.__wchb[1] << 8)
79
            - 0x10000 >> 10 | 0xd80d;
80
      *s++ = 0xe0 | ((tmp & 0xf000) >> 12);
81
      *s++ = 0x80 | ((tmp &  0xfc0) >> 6);
82
      *s++ = 0x80 |  (tmp &   0x3f);
83
      state->__count = 0;
84
      ret = 3;
85
    }
86
  if (wchar <= 0x7f)
87
    {
88
      *s = wchar;
89
      return ret + 1;
90
    }
91
  if (wchar >= 0x80 && wchar <= 0x7ff)
92
    {
93
      *s++ = 0xc0 | ((wchar & 0x7c0) >> 6);
94
      *s   = 0x80 |  (wchar &  0x3f);
95
      return ret + 2;
96
    }
97
  if (wchar >= 0x800 && wchar <= 0xffff)
98
    {
99
      /* No UTF-16 surrogate handling in UCS-4 */
100
      if (sizeof (wchar_t) == 2 && wchar >= 0xd800 && wchar <= 0xdfff)
101
        {
102
          wint_t tmp;
103
          if (wchar <= 0xdbff)
104
            {
105
              /* First half of a surrogate pair.  Store the state and
106
                 return ret + 0. */
107
              tmp = ((wchar & 0x3ff) << 10) + 0x10000;
108
              state->__value.__wchb[0] = (tmp >> 16) & 0xff;
109
              state->__value.__wchb[1] = (tmp >> 8) & 0xff;
110
              state->__count = -4;
111
              *s = (0xf0 | ((tmp & 0x1c0000) >> 18));
112
              return ret;
113
            }
114
          if (state->__count == -4)
115
            {
116
              /* Second half of a surrogate pair.  Reconstruct the full
117
                 Unicode value and return the trailing three bytes of the
118
                 UTF-8 character. */
119
              tmp = (state->__value.__wchb[0] << 16)
120
                    | (state->__value.__wchb[1] << 8)
121
                    | (wchar & 0x3ff);
122
              state->__count = 0;
123
              *s++ = 0xf0 | ((tmp & 0x1c0000) >> 18);
124
              *s++ = 0x80 | ((tmp &  0x3f000) >> 12);
125
              *s++ = 0x80 | ((tmp &    0xfc0) >> 6);
126
              *s   = 0x80 |  (tmp &     0x3f);
127
              return 4;
128
            }
129
          /* Otherwise translate into CESU-8 value. */
130
        }
131
      *s++ = 0xe0 | ((wchar & 0xf000) >> 12);
132
      *s++ = 0x80 | ((wchar &  0xfc0) >> 6);
133
      *s   = 0x80 |  (wchar &   0x3f);
134
      return ret + 3;
135
    }
136
  if (wchar >= 0x10000 && wchar <= 0x10ffff)
137
    {
138
      *s++ = 0xf0 | ((wchar & 0x1c0000) >> 18);
139
      *s++ = 0x80 | ((wchar &  0x3f000) >> 12);
140
      *s++ = 0x80 | ((wchar &    0xfc0) >> 6);
141
      *s   = 0x80 |  (wchar &     0x3f);
142
      return 4;
143
    }
144
 
145
  r->_errno = EILSEQ;
146
  return -1;
147
}
148
 
149
/* Cygwin defines its own doublebyte charset conversion functions
150
   because the underlying OS requires wchar_t == UTF-16. */
151
#ifndef __CYGWIN__
152
int
153
_DEFUN (__sjis_wctomb, (r, s, wchar, charset, state),
154
        struct _reent *r       _AND
155
        char          *s       _AND
156
        wchar_t        _wchar  _AND
157
        const char    *charset _AND
158
        mbstate_t     *state)
159
{
160
  wint_t wchar = _wchar;
161
 
162
  unsigned char char2 = (unsigned char)wchar;
163
  unsigned char char1 = (unsigned char)(wchar >> 8);
164
 
165
  if (s == NULL)
166
    return 0;  /* not state-dependent */
167
 
168
  if (char1 != 0x00)
169
    {
170
    /* first byte is non-zero..validate multi-byte char */
171
      if (_issjis1(char1) && _issjis2(char2))
172
        {
173
          *s++ = (char)char1;
174
          *s = (char)char2;
175
          return 2;
176
        }
177
      else
178
        {
179
          r->_errno = EILSEQ;
180
          return -1;
181
        }
182
    }
183
  *s = (char) wchar;
184
  return 1;
185
}
186
 
187
int
188
_DEFUN (__eucjp_wctomb, (r, s, wchar, charset, state),
189
        struct _reent *r       _AND
190
        char          *s       _AND
191
        wchar_t        _wchar  _AND
192
        const char    *charset _AND
193
        mbstate_t     *state)
194
{
195
  wint_t wchar = _wchar;
196
  unsigned char char2 = (unsigned char)wchar;
197
  unsigned char char1 = (unsigned char)(wchar >> 8);
198
 
199
  if (s == NULL)
200
    return 0;  /* not state-dependent */
201
 
202
  if (char1 != 0x00)
203
    {
204
    /* first byte is non-zero..validate multi-byte char */
205
      if (_iseucjp1 (char1) && _iseucjp2 (char2))
206
        {
207
          *s++ = (char)char1;
208
          *s = (char)char2;
209
          return 2;
210
        }
211
      else if (_iseucjp2 (char1) && _iseucjp2 (char2 | 0x80))
212
        {
213
          *s++ = (char)0x8f;
214
          *s++ = (char)char1;
215
          *s = (char)(char2 | 0x80);
216
          return 3;
217
        }
218
      else
219
        {
220
          r->_errno = EILSEQ;
221
          return -1;
222
        }
223
    }
224
  *s = (char) wchar;
225
  return 1;
226
}
227
 
228
int
229
_DEFUN (__jis_wctomb, (r, s, wchar, charset, state),
230
        struct _reent *r       _AND
231
        char          *s       _AND
232
        wchar_t        _wchar  _AND
233
        const char    *charset _AND
234
        mbstate_t     *state)
235
{
236
  wint_t wchar = _wchar;
237
  int cnt = 0;
238
  unsigned char char2 = (unsigned char)wchar;
239
  unsigned char char1 = (unsigned char)(wchar >> 8);
240
 
241
  if (s == NULL)
242
    return 1;  /* state-dependent */
243
 
244
  if (char1 != 0x00)
245
    {
246
    /* first byte is non-zero..validate multi-byte char */
247
      if (_isjis (char1) && _isjis (char2))
248
        {
249
          if (state->__state == 0)
250
            {
251
              /* must switch from ASCII to JIS state */
252
              state->__state = 1;
253
              *s++ = ESC_CHAR;
254
              *s++ = '$';
255
              *s++ = 'B';
256
              cnt = 3;
257
            }
258
          *s++ = (char)char1;
259
          *s = (char)char2;
260
          return cnt + 2;
261
        }
262
      r->_errno = EILSEQ;
263
      return -1;
264
    }
265
  if (state->__state != 0)
266
    {
267
      /* must switch from JIS to ASCII state */
268
      state->__state = 0;
269
      *s++ = ESC_CHAR;
270
      *s++ = '(';
271
      *s++ = 'B';
272
      cnt = 3;
273
    }
274
  *s = (char)char2;
275
  return cnt + 1;
276
}
277
#endif /* !__CYGWIN__ */
278
 
279
#ifdef _MB_EXTENDED_CHARSETS_ISO
280
int
281
_DEFUN (__iso_wctomb, (r, s, wchar, charset, state),
282
        struct _reent *r       _AND
283
        char          *s       _AND
284
        wchar_t        _wchar  _AND
285
        const char    *charset _AND
286
        mbstate_t     *state)
287
{
288
  wint_t wchar = _wchar;
289
 
290
  if (s == NULL)
291
    return 0;
292
 
293
  /* wchars <= 0x9f translate to all ISO charsets directly. */
294
  if (wchar >= 0xa0)
295
    {
296
      int iso_idx = __iso_8859_index (charset + 9);
297
      if (iso_idx >= 0)
298
        {
299
          unsigned char mb;
300
 
301
          if (s == NULL)
302
            return 0;
303
 
304
          for (mb = 0; mb < 0x60; ++mb)
305
            if (__iso_8859_conv[iso_idx][mb] == wchar)
306
              {
307
                *s = (char) (mb + 0xa0);
308
                return 1;
309
              }
310
          r->_errno = EILSEQ;
311
          return -1;
312
        }
313
    }
314
 
315
  if ((size_t)wchar >= 0x100)
316
    {
317
      r->_errno = EILSEQ;
318
      return -1;
319
    }
320
 
321
  *s = (char) wchar;
322
  return 1;
323
}
324
#endif /* _MB_EXTENDED_CHARSETS_ISO */
325
 
326
#ifdef _MB_EXTENDED_CHARSETS_WINDOWS
327
int
328
_DEFUN (__cp_wctomb, (r, s, wchar, charset, state),
329
        struct _reent *r       _AND
330
        char          *s       _AND
331
        wchar_t        _wchar  _AND
332
        const char    *charset _AND
333
        mbstate_t     *state)
334
{
335
  wint_t wchar = _wchar;
336
 
337
  if (s == NULL)
338
    return 0;
339
 
340
  if (wchar >= 0x80)
341
    {
342
      int cp_idx = __cp_index (charset + 2);
343
      if (cp_idx >= 0)
344
        {
345
          unsigned char mb;
346
 
347
          if (s == NULL)
348
            return 0;
349
 
350
          for (mb = 0; mb < 0x80; ++mb)
351
            if (__cp_conv[cp_idx][mb] == wchar)
352
              {
353
                *s = (char) (mb + 0x80);
354
                return 1;
355
              }
356
          r->_errno = EILSEQ;
357
          return -1;
358
        }
359
    }
360
 
361
  if ((size_t)wchar >= 0x100)
362
    {
363
      r->_errno = EILSEQ;
364
      return -1;
365
    }
366
 
367
  *s = (char) wchar;
368
  return 1;
369
}
370
#endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
371
#endif /* _MB_CAPABLE */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.