forked from MoSync/MoSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwchar.c
166 lines (156 loc) · 3.97 KB
/
wchar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (C) 2010 MoSync AB
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "wchar.h"
// TODO: shorten these function by using loops.
int mbtowc(wchar_t* dst, const char* src, size_t count) {
if(count < 1)
return -1;
if((src[0] & 0x80) == 0) { // 1 byte
*dst = *src;
return *dst == 0 ? 0 : 1;
} else if((src[0] & 0xE0) == 0xC0) { // 2 bytes
if(count < 2)
return -1;
if((src[1] & 0xC0) != 0x80)
return -1;
*dst = ((((wchar_t)src[0]) & 0x1F) << 6) | (((wchar_t)src[1]) & 0x3F);
return *dst == 0 ? 0 : 2;
} else if((src[0] & 0xF0) == 0xE0) { // 3 bytes
if(count < 3)
return -1;
if((src[1] & 0xC0) != 0x80)
return -1;
if((src[2] & 0xC0) != 0x80)
return -1;
*dst = ((((wchar_t)src[0]) & 0x0F) << 12) | ((((wchar_t)src[1]) & 0x1F) << 6) |
(((wchar_t)src[2]) & 0x3F);
return *dst == 0 ? 0 : 3;
// if we use UCS-2, we can't deal with Unicode characters outside the Basic Multilingual Plane.
#if MB_CUR_MAX > 3
} else if((src[0] & 0xF8) == 0xF0) { // 4 bytes
if(count < 4)
return -1;
if((src[1] & 0xC0) != 0x80)
return -1;
if((src[2] & 0xC0) != 0x80)
return -1;
if((src[3] & 0xC0) != 0x80)
return -1;
*dst = ((((wchar_t)src[0]) & 0x03) << 18) | ((((wchar_t)src[1]) & 0x0F) << 12) |
((((wchar_t)src[2]) & 0x1F) << 6) | (((wchar_t)src[3]) & 0x3F);
return *dst == 0 ? 0 : 4;
#endif //MB_CUR_MAX
}
return -1;
}
int wctomb(char* dst, wchar_t src) {
if(src <= 0x7F) {
*dst = (char)src;
return src == 0 ? 0 : 1;
} else if(src <= 0x7FF) {
dst[1] = 0x80 | (src & 0x3F);
dst[0] = 0xC0 | ((src & 0x7E0) >> 6);
return 2;
} else {
#if MB_CUR_MAX > 3
if(src <= 0xFFFF)
#endif
{
dst[2] = 0x80 | (src & 0x3F);
dst[1] = 0x80 | ((src & 0x0FE0) >> 6);
dst[0] = 0xE0 | ((src & 0xF000) >> 12);
return 3;
}
#if MB_CUR_MAX > 3
else if(src <= 0x10FFFF) {
dst[3] = 0x80 | (src & 0x3F);
dst[2] = 0x80 | ((src & 0x0FE0) >> 6);
dst[1] = 0x80 | ((src & 0x03F000) >> 12);
dst[0] = 0xF0 | ((src & 0x3C0000) >> 18);
return 4;
}
#endif
}
#if MB_CUR_MAX > 3
return -1;
#endif
}
size_t mbstowcs(wchar_t* dst, const char* src, size_t count) {
size_t wpos = 0;
const char* srcp = src;
if(dst == NULL) {
wchar_t wc;
int res;
do {
res = mbtowc(&wc, srcp, 4);
if(res < 0)
return res;
wpos++;
srcp += res;
} while(res > 0);
} else while(wpos < count) {
int res = mbtowc(dst + wpos, srcp, 4);
if(res < 0)
return res;
if(res == 0) {
break;
}
wpos++;
srcp += res;
}
return wpos;
}
size_t wcstombs(char* dst, const wchar_t* src, size_t count) {
size_t dpos = 0;
const wchar_t* srcp = src;
if(dst == NULL) {
char mb[MB_CUR_MAX];
int res;
do {
res = wctomb(mb, *srcp);
if(res < 0)
return res;
dpos += res;
srcp++;
} while(res > 0);
dpos++; // terminating NULL.
} else {
// write directly to dst while enough space remains.
while(dpos + MB_CUR_MAX < count) {
int res = wctomb(dst + dpos, *srcp);
if(res < 0)
return res;
if(res == 0)
return dpos;
srcp++;
dpos += res;
}
// slower method when very little space remains.
while(dpos < count) {
char buf[MB_CUR_MAX];
int bytes = wctomb(buf, *srcp);
int remain, num_to_copy;
if(bytes < 0)
return bytes;
remain = count - dpos;
num_to_copy = (remain > bytes ? bytes : remain);
for(int i=0; i<num_to_copy; ++i)
dst[dpos++] = buf[i];
if (*srcp == 0)
return dpos;
++srcp;
}
}
return dpos;
}