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