forked from infancy/devlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwchar.c
188 lines (156 loc) · 3.41 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <limits.h>
#include "DBG.h"
#include "crowbar.h"
size_t
CRB_wcslen(CRB_Char *str)
{
return wcslen(str);
}
CRB_Char *
CRB_wcscpy(CRB_Char *dest, CRB_Char *src)
{
return wcscpy(dest, src);
}
CRB_Char *
CRB_wcsncpy(CRB_Char *dest, CRB_Char *src, size_t n)
{
return wcsncpy(dest, src, n);
}
int
CRB_wcscmp(CRB_Char *s1, CRB_Char *s2)
{
return wcscmp(s1, s2);
}
CRB_Char *
CRB_wcscat(CRB_Char *s1, CRB_Char *s2)
{
return wcscat(s1, s2);
}
int
CRB_mbstowcs_len(const char *src)
{
int src_idx, dest_idx;
int status;
mbstate_t ps;
memset(&ps, 0, sizeof(mbstate_t));
for (src_idx = dest_idx = 0; src[src_idx] != '\0'; ) {
status = mbrtowc(NULL, &src[src_idx], MB_LEN_MAX, &ps);
if (status < 0) {
return status;
}
dest_idx++;
src_idx += status;
}
return dest_idx;
}
void
CRB_mbstowcs(const char *src, CRB_Char *dest)
{
int src_idx, dest_idx;
int status;
mbstate_t ps;
memset(&ps, 0, sizeof(mbstate_t));
for (src_idx = dest_idx = 0; src[src_idx] != '\0'; ) {
status = mbrtowc(&dest[dest_idx], &src[src_idx],
MB_LEN_MAX, &ps);
dest_idx++;
src_idx += status;
}
dest[dest_idx] = L'\0';
}
CRB_Char *
CRB_mbstowcs_alloc(CRB_Interpreter *inter, CRB_LocalEnvironment *env,
int line_number, const char *src)
{
int len;
CRB_Char *ret;
len = CRB_mbstowcs_len(src);
if (len < 0) {
return NULL;
crb_runtime_error(inter, env, line_number,
BAD_MULTIBYTE_CHARACTER_ERR,
CRB_MESSAGE_ARGUMENT_END);
}
ret = MEM_malloc(sizeof(CRB_Char) * (len+1));
CRB_mbstowcs(src, ret);
return ret;
}
int
CRB_wcstombs_len(const CRB_Char *src)
{
int src_idx, dest_idx;
int status;
char dummy[MB_LEN_MAX];
mbstate_t ps;
memset(&ps, 0, sizeof(mbstate_t));
for (src_idx = dest_idx = 0; src[src_idx] != L'\0'; ) {
status = wcrtomb(dummy, src[src_idx], &ps);
src_idx++;
dest_idx += status;
}
return dest_idx;
}
void
CRB_wcstombs(const CRB_Char *src, char *dest)
{
int src_idx, dest_idx;
int status;
mbstate_t ps;
memset(&ps, 0, sizeof(mbstate_t));
for (src_idx = dest_idx = 0; src[src_idx] != '\0'; ) {
status = wcrtomb(&dest[dest_idx], src[src_idx], &ps);
src_idx++;
dest_idx += status;
}
dest[dest_idx] = '\0';
}
char *
CRB_wcstombs_alloc(const CRB_Char *src)
{
int len;
char *ret;
len = CRB_wcstombs_len(src);
ret = MEM_malloc(len + 1);
CRB_wcstombs(src, ret);
return ret;
}
char
CRB_wctochar(CRB_Char src)
{
mbstate_t ps;
int status;
char dest;
memset(&ps, 0, sizeof(mbstate_t));
status = wcrtomb(&dest, src, &ps);
DBG_assert(status == 1, ("wcrtomb status..%d\n", status));
return dest;
}
int
CRB_print_wcs(FILE *fp, CRB_Char *str)
{
char *tmp;
int mb_len;
int result;
mb_len = CRB_wcstombs_len(str);
tmp = MEM_malloc(mb_len + 1);
CRB_wcstombs(str, tmp);
result = fprintf(fp, "%s", tmp);
MEM_free(tmp);
return result;
}
int
CRB_print_wcs_ln(FILE *fp, CRB_Char *str)
{
int result;
result = CRB_print_wcs(fp, str);
fprintf(fp, "\n");
return result;
}
CRB_Boolean
CRB_iswdigit(CRB_Char ch)
{
return ch >= L'0' && ch <= L'9';
}