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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdlib/] [mbtowc_r.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
#include <stdlib.h>
2
#include <locale.h>
3
#include "mbctype.h"
4
 
5
#ifdef MB_CAPABLE
6
typedef enum { ESCAPE, DOLLAR, BRACKET, AT, B, J,
7
               NUL, JIS_CHAR, OTHER, JIS_C_NUM } JIS_CHAR_TYPE;
8
typedef enum { ASCII, A_ESC, A_ESC_DL, JIS, JIS_1, JIS_2, J_ESC, J_ESC_BR,
9
               J2_ESC, J2_ESC_BR, DONE, INV, JIS_S_NUM } JIS_STATE;
10
typedef enum { COPY_A, COPY_J, COPY_J2, MAKE_A, MAKE_J, NOOP, EMPTY, ERROR } JIS_ACTION;
11
 
12
/**************************************************************************************
13
 * state/action tables for processing JIS encoding
14
 * Where possible, switches to JIS are grouped with proceding JIS characters and switches
15
 * to ASCII are grouped with preceding JIS characters.  Thus, maximum returned length
16
 * is 2 (switch to JIS) + 2 (JIS characters) + 2 (switch back to ASCII) = 6.
17
 *************************************************************************************/
18
 
19
static JIS_STATE JIS_state_table[JIS_S_NUM][JIS_C_NUM] = {
20
/*              ESCAPE   DOLLAR    BRACKET   AT       B       J        NUL      JIS_CHAR  OTHER */
21
/* ASCII */   { A_ESC,   DONE,     DONE,     DONE,    DONE,   DONE,    DONE,    DONE,     DONE },
22
/* A_ESC */   { DONE,    A_ESC_DL, DONE,     DONE,    DONE,   DONE,    DONE,    DONE,     DONE },
23
/* A_ESC_DL */{ DONE,    DONE,     DONE,     JIS,     JIS,    DONE,    DONE,    DONE,     DONE },
24
/* JIS */     { J_ESC,   JIS_1,    JIS_1,    JIS_1,   JIS_1,  JIS_1,   INV,     JIS_1,    INV },
25
/* JIS_1 */   { INV,     JIS_2,    JIS_2,    JIS_2,   JIS_2,  JIS_2,   INV,     JIS_2,    INV },
26
/* JIS_2 */   { J2_ESC,  DONE,     DONE,     DONE,    DONE,   DONE,    INV,     DONE,     DONE },
27
/* J_ESC */   { INV,     INV,      J_ESC_BR, INV,     INV,    INV,     INV,     INV,      INV },
28
/* J_ESC_BR */{ INV,     INV,      INV,      INV,     ASCII,  ASCII,   INV,     INV,      INV },
29
/* J2_ESC */  { INV,     INV,      J2_ESC_BR,INV,     INV,    INV,     INV,     INV,      INV },
30
/* J2_ESC_BR*/{ INV,     INV,      INV,      INV,     DONE,   DONE,    INV,     INV,      INV },
31
};
32
 
33
static JIS_ACTION JIS_action_table[JIS_S_NUM][JIS_C_NUM] = {
34
/*              ESCAPE   DOLLAR    BRACKET   AT       B        J        NUL      JIS_CHAR  OTHER */
35
/* ASCII */   { NOOP,    COPY_A,   COPY_A,   COPY_A,  COPY_A,  COPY_A,  EMPTY,   COPY_A,  COPY_A},
36
/* A_ESC */   { COPY_A,  NOOP,     COPY_A,   COPY_A,  COPY_A,  COPY_A,  COPY_A,  COPY_A,  COPY_A},
37
/* A_ESC_DL */{ COPY_A,  COPY_A,   COPY_A,   MAKE_J,  MAKE_J,  COPY_A,  COPY_A,  COPY_A,  COPY_A},
38
/* JIS */     { NOOP,    NOOP,     NOOP,     NOOP,    NOOP,    NOOP,    ERROR,   NOOP,    ERROR },
39
/* JIS_1 */   { ERROR,   NOOP,     NOOP,     NOOP,    NOOP,    NOOP,    ERROR,   NOOP,    ERROR },
40
/* JIS_2 */   { NOOP,    COPY_J2,  COPY_J2,  COPY_J2, COPY_J2, COPY_J2, ERROR,   COPY_J2, COPY_J2},
41
/* J_ESC */   { ERROR,   ERROR,    NOOP,     ERROR,   ERROR,   ERROR,   ERROR,   ERROR,   ERROR },
42
/* J_ESC_BR */{ ERROR,   ERROR,    ERROR,    ERROR,   NOOP,    NOOP,    ERROR,   ERROR,   ERROR },
43
/* J2_ESC */  { ERROR,   ERROR,    NOOP,     ERROR,   ERROR,   ERROR,   ERROR,   ERROR,   ERROR },
44
/* J2_ESC_BR*/{ ERROR,   ERROR,    ERROR,    ERROR,   COPY_J,  COPY_J,  ERROR,   ERROR,   ERROR },
45
};
46
#endif /* MB_CAPABLE */
47
 
48
int
49
_DEFUN (_mbtowc_r, (r, pwc, s, n, state),
50
        struct _reent *r   _AND
51
        wchar_t       *pwc _AND
52
        const char    *s   _AND
53
        size_t         n   _AND
54
        int           *state)
55
{
56
  wchar_t dummy;
57
  unsigned char *t = (unsigned char *)s;
58
 
59
  if (pwc == NULL)
60
    pwc = &dummy;
61
 
62
  if (s != NULL && n == 0)
63
    return -1;
64
 
65
#ifdef MB_CAPABLE
66
  if (r->_current_locale == NULL ||
67
      (strlen (r->_current_locale) <= 1))
68
    { /* fall-through */ }
69
  else if (!strcmp (r->_current_locale, "C-SJIS"))
70
    {
71
      int char1;
72
      if (s == NULL)
73
        return 0;  /* not state-dependent */
74
      char1 = *t;
75
      if (_issjis1 (char1))
76
        {
77
          int char2 = t[1];
78
          if (n <= 1)
79
            return -1;
80
          if (_issjis2 (char2))
81
            {
82
              *pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
83
              return 2;
84
            }
85
          else
86
            return -1;
87
        }
88
    }
89
  else if (!strcmp (r->_current_locale, "C-EUCJP"))
90
    {
91
      int char1;
92
      if (s == NULL)
93
        return 0;  /* not state-dependent */
94
      char1 = *t;
95
      if (_iseucjp (char1))
96
        {
97
          int char2 = t[1];
98
          if (n <= 1)
99
            return -1;
100
          if (_iseucjp (char2))
101
            {
102
              *pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
103
              return 2;
104
            }
105
          else
106
            return -1;
107
        }
108
    }
109
  else if (!strcmp (r->_current_locale, "C-JIS"))
110
    {
111
      JIS_STATE curr_state;
112
      JIS_ACTION action;
113
      JIS_CHAR_TYPE ch;
114
      unsigned char *ptr;
115
      int i, curr_ch;
116
 
117
      if (s == NULL)
118
        {
119
          *state = 0;
120
          return 1;  /* state-dependent */
121
        }
122
 
123
      curr_state = (*state == 0 ? ASCII : JIS);
124
      ptr = t;
125
 
126
      for (i = 0; i < n; ++i)
127
        {
128
          curr_ch = t[i];
129
          switch (curr_ch)
130
            {
131
            case ESC_CHAR:
132
              ch = ESCAPE;
133
              break;
134
            case '$':
135
              ch = DOLLAR;
136
              break;
137
            case '@':
138
              ch = AT;
139
              break;
140
            case '(':
141
              ch = BRACKET;
142
              break;
143
            case 'B':
144
              ch = B;
145
              break;
146
            case 'J':
147
              ch = J;
148
              break;
149
            case '\0':
150
              ch = NUL;
151
              break;
152
            default:
153
              if (_isjis (curr_ch))
154
                ch = JIS_CHAR;
155
              else
156
                ch = OTHER;
157
            }
158
 
159
          action = JIS_action_table[curr_state][ch];
160
          curr_state = JIS_state_table[curr_state][ch];
161
 
162
          switch (action)
163
            {
164
            case NOOP:
165
              break;
166
            case EMPTY:
167
              *state = 0;
168
              *pwc = (wchar_t)0;
169
              return i;
170
            case COPY_A:
171
              *state = 0;
172
              *pwc = (wchar_t)*ptr;
173
              return (i + 1);
174
            case COPY_J:
175
              *state = 0;
176
              *pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
177
              return (i + 1);
178
            case COPY_J2:
179
              *state = 1;
180
              *pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
181
              return (ptr - t) + 2;
182
            case MAKE_A:
183
            case MAKE_J:
184
              ptr = (char *)(t + i + 1);
185
              break;
186
            case ERROR:
187
            default:
188
              return -1;
189
            }
190
 
191
        }
192
 
193
      return -1;  /* n < bytes needed */
194
    }
195
#endif /* MB_CAPABLE */               
196
 
197
  /* otherwise this must be the "C" locale or unknown locale */
198
  if (s == NULL)
199
    return 0;  /* not state-dependent */
200
 
201
  *pwc = (wchar_t)*t;
202
 
203
  if (*t == '\0')
204
    return 0;
205
 
206
  return 1;
207
}
208
 

powered by: WebSVN 2.1.0

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