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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
#include <stdlib.h>
2
#include <locale.h>
3
#include "mbctype.h"
4
 
5 56 joel
#ifdef MB_CAPABLE
6 39 lampret
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 56 joel
#endif /* MB_CAPABLE */
47 39 lampret
 
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 56 joel
#ifdef MB_CAPABLE
66 39 lampret
  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 = *t;
72
      if (s == NULL)
73
        return 0;  /* not state-dependent */
74
      if (_issjis1 (char1))
75
        {
76
          int char2 = t[1];
77
          if (n <= 1)
78
            return -1;
79
          if (_issjis2 (char2))
80
            {
81
              *pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
82
              return 2;
83
            }
84
          else
85
            return -1;
86
        }
87
    }
88
  else if (!strcmp (r->_current_locale, "C-EUCJP"))
89
    {
90
      int char1 = *t;
91
      if (s == NULL)
92
        return 0;  /* not state-dependent */
93
      if (_iseucjp (char1))
94
        {
95
          int char2 = t[1];
96
          if (n <= 1)
97
            return -1;
98
          if (_iseucjp (char2))
99
            {
100
              *pwc = (((wchar_t)*t) << 8) + (wchar_t)(*(t+1));
101
              return 2;
102
            }
103
          else
104
            return -1;
105
        }
106
    }
107
  else if (!strcmp (r->_current_locale, "C-JIS"))
108
    {
109
      JIS_STATE curr_state;
110
      JIS_ACTION action;
111
      JIS_CHAR_TYPE ch;
112
      unsigned char *ptr;
113
      int i, curr_ch;
114
 
115
      if (s == NULL)
116
        {
117
          *state = 0;
118
          return 1;  /* state-dependent */
119
        }
120
 
121
      curr_state = (*state == 0 ? ASCII : JIS);
122
      ptr = t;
123
 
124
      for (i = 0; i < n; ++i)
125
        {
126
          curr_ch = t[i];
127
          switch (curr_ch)
128
            {
129
            case ESC_CHAR:
130
              ch = ESCAPE;
131
              break;
132
            case '$':
133
              ch = DOLLAR;
134
              break;
135
            case '@':
136
              ch = AT;
137
              break;
138
            case '(':
139
              ch = BRACKET;
140
              break;
141
            case 'B':
142
              ch = B;
143
              break;
144
            case 'J':
145
              ch = J;
146
              break;
147
            case '\0':
148
              ch = NUL;
149
              break;
150
            default:
151
              if (_isjis (curr_ch))
152
                ch = JIS_CHAR;
153
              else
154
                ch = OTHER;
155
            }
156
 
157
          action = JIS_action_table[curr_state][ch];
158
          curr_state = JIS_state_table[curr_state][ch];
159
 
160
          switch (action)
161
            {
162
            case NOOP:
163
              break;
164
            case EMPTY:
165
              *state = 0;
166
              *pwc = (wchar_t)0;
167
              return i;
168
            case COPY_A:
169
              *state = 0;
170
              *pwc = (wchar_t)*ptr;
171
              return (i + 1);
172
            case COPY_J:
173
              *state = 0;
174
              *pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
175
              return (i + 1);
176
            case COPY_J2:
177
              *state = 1;
178
              *pwc = (((wchar_t)*ptr) << 8) + (wchar_t)(*(ptr+1));
179
              return (ptr - t) + 2;
180
            case MAKE_A:
181
            case MAKE_J:
182
              ptr = (char *)(t + i + 1);
183
              break;
184
            case ERROR:
185
            default:
186
              return -1;
187
            }
188
 
189
        }
190
 
191
      return -1;  /* n < bytes needed */
192
    }
193 56 joel
#endif /* MB_CAPABLE */               
194 39 lampret
 
195
  /* otherwise this must be the "C" locale or unknown locale */
196
  if (s == NULL)
197
    return 0;  /* not state-dependent */
198
 
199
  *pwc = (wchar_t)*t;
200
 
201
  if (*t == '\0')
202
    return 0;
203
 
204
  return 1;
205
}
206
 

powered by: WebSVN 2.1.0

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