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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdlib/] [wctomb_r.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
#include <stdlib.h>
2
#include <string.h>
3
#include <wchar.h>
4
#include <locale.h>
5
#include "mbctype.h"
6
 
7
/* for some conversions, we use the __count field as a place to store a state value */
8
#define __state __count
9
 
10
extern char __lc_ctype[12];
11
 
12
int
13
_DEFUN (_wctomb_r, (r, s, wchar, state),
14
        struct _reent *r     _AND
15
        char          *s     _AND
16
        wchar_t        _wchar _AND
17
        mbstate_t     *state)
18
{
19
  /* Avoids compiler warnings about comparisons that are always false
20
     due to limited range when sizeof(wchar_t) is 2 but sizeof(wint_t)
21
     is 4, as is the case on cygwin.  */
22
  wint_t wchar = _wchar;
23
 
24
  if (strlen (__lc_ctype) <= 1)
25
    { /* fall-through */ }
26
  else if (!strcmp (__lc_ctype, "C-UTF-8"))
27
    {
28
      if (s == NULL)
29
        return 0; /* UTF-8 encoding is not state-dependent */
30
 
31
      if (wchar <= 0x7f)
32
        {
33
          *s = wchar;
34
          return 1;
35
        }
36
      else if (wchar >= 0x80 && wchar <= 0x7ff)
37
        {
38
          *s++ = 0xc0 | ((wchar & 0x7c0) >> 6);
39
          *s   = 0x80 |  (wchar &  0x3f);
40
          return 2;
41
        }
42
      else if (wchar >= 0x800 && wchar <= 0xffff)
43
        {
44
          /* UTF-16 surrogates -- must not occur in normal UCS-4 data */
45
          if (wchar >= 0xd800 && wchar <= 0xdfff)
46
            return -1;
47
 
48
          *s++ = 0xe0 | ((wchar & 0xf000) >> 12);
49
          *s++ = 0x80 | ((wchar &  0xfc0) >> 6);
50
          *s   = 0x80 |  (wchar &   0x3f);
51
          return 3;
52
        }
53
      else if (wchar >= 0x10000 && wchar <= 0x1fffff)
54
        {
55
          *s++ = 0xf0 | ((wchar & 0x1c0000) >> 18);
56
          *s++ = 0x80 | ((wchar &  0x3f000) >> 12);
57
          *s++ = 0x80 | ((wchar &    0xfc0) >> 6);
58
          *s   = 0x80 |  (wchar &     0x3f);
59
          return 4;
60
        }
61
      else if (wchar >= 0x200000 && wchar <= 0x3ffffff)
62
        {
63
          *s++ = 0xf8 | ((wchar & 0x3000000) >> 24);
64
          *s++ = 0x80 | ((wchar &  0xfc0000) >> 18);
65
          *s++ = 0x80 | ((wchar &   0x3f000) >> 12);
66
          *s++ = 0x80 | ((wchar &     0xfc0) >> 6);
67
          *s   = 0x80 |  (wchar &      0x3f);
68
          return 5;
69
        }
70
      else if (wchar >= 0x4000000 && wchar <= 0x7fffffff)
71
        {
72
          *s++ = 0xfc | ((wchar & 0x40000000) >> 30);
73
          *s++ = 0x80 | ((wchar & 0x3f000000) >> 24);
74
          *s++ = 0x80 | ((wchar &   0xfc0000) >> 18);
75
          *s++ = 0x80 | ((wchar &    0x3f000) >> 12);
76
          *s++ = 0x80 | ((wchar &      0xfc0) >> 6);
77
          *s   = 0x80 |  (wchar &       0x3f);
78
          return 6;
79
        }
80
      else
81
        return -1;
82
    }
83
  else if (!strcmp (__lc_ctype, "C-SJIS"))
84
    {
85
      unsigned char char2 = (unsigned char)wchar;
86
      unsigned char char1 = (unsigned char)(wchar >> 8);
87
 
88
      if (s == NULL)
89
        return 0;  /* not state-dependent */
90
 
91
      if (char1 != 0x00)
92
        {
93
        /* first byte is non-zero..validate multi-byte char */
94
          if (_issjis1(char1) && _issjis2(char2))
95
            {
96
              *s++ = (char)char1;
97
              *s = (char)char2;
98
              return 2;
99
            }
100
          else
101
            return -1;
102
        }
103
    }
104
  else if (!strcmp (__lc_ctype, "C-EUCJP"))
105
    {
106
      unsigned char char2 = (unsigned char)wchar;
107
      unsigned char char1 = (unsigned char)(wchar >> 8);
108
 
109
      if (s == NULL)
110
        return 0;  /* not state-dependent */
111
 
112
      if (char1 != 0x00)
113
        {
114
        /* first byte is non-zero..validate multi-byte char */
115
          if (_iseucjp (char1) && _iseucjp (char2))
116
            {
117
              *s++ = (char)char1;
118
              *s = (char)char2;
119
              return 2;
120
            }
121
          else
122
            return -1;
123
        }
124
    }
125
  else if (!strcmp (__lc_ctype, "C-JIS"))
126
    {
127
      int cnt = 0;
128
      unsigned char char2 = (unsigned char)wchar;
129
      unsigned char char1 = (unsigned char)(wchar >> 8);
130
 
131
      if (s == NULL)
132
        return 1;  /* state-dependent */
133
 
134
      if (char1 != 0x00)
135
        {
136
        /* first byte is non-zero..validate multi-byte char */
137
          if (_isjis (char1) && _isjis (char2))
138
            {
139
              if (state->__state == 0)
140
                {
141
                  /* must switch from ASCII to JIS state */
142
                  state->__state = 1;
143
                  *s++ = ESC_CHAR;
144
                  *s++ = '$';
145
                  *s++ = 'B';
146
                  cnt = 3;
147
                }
148
              *s++ = (char)char1;
149
              *s = (char)char2;
150
              return cnt + 2;
151
            }
152
          else
153
            return -1;
154
        }
155
      else
156
        {
157
          if (state->__state != 0)
158
            {
159
              /* must switch from JIS to ASCII state */
160
              state->__state = 0;
161
              *s++ = ESC_CHAR;
162
              *s++ = '(';
163
              *s++ = 'B';
164
              cnt = 3;
165
            }
166
          *s = (char)char2;
167
          return cnt + 1;
168
        }
169
    }
170
 
171
  if (s == NULL)
172
    return 0;
173
 
174
  /* otherwise we are dealing with a single byte character */
175
  *s = (char) wchar;
176
  return 1;
177
}
178
 

powered by: WebSVN 2.1.0

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