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 39

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

powered by: WebSVN 2.1.0

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