-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
213 lines (174 loc) · 4.03 KB
/
utf8.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/** fcom: Convert files to UTF-8
2023, Simon Zolin */
static const char* utf8_help()
{
return "\
Convert UTF-8/16 (with BOM) files to UTF-8 (without BOM).\n\
Usage:\n\
fcom utf8 INPUT... -o OUTPUT\n\
";
}
#include <fcom.h>
#include <ffbase/unicode.h>
static const fcom_core *core;
struct utf8 {
fcom_cominfo cominfo;
uint st;
uint stop;
fcom_cominfo *cmd;
fcom_file_obj *out;
ffstr iname;
ffstr data;
ffvec fdata, buf;
uint64 off;
};
static int args_parse(struct utf8 *u, fcom_cominfo *cmd)
{
static const struct ffarg args[] = {
{}
};
if (0 != core->com->args_parse(cmd, args, u, FCOM_COM_AP_INOUT))
return -1;
if (u->cmd->output.len == 0)
u->cmd->stdout = 1;
return 0;
}
static void utf8_close(fcom_op *op)
{
struct utf8 *u = op;
core->file->destroy(u->out);
ffvec_free(&u->fdata);
ffvec_free(&u->buf);
ffmem_free(u);
}
static fcom_op* utf8_create(fcom_cominfo *cmd)
{
struct utf8 *u = ffmem_new(struct utf8);
u->cmd = cmd;
if (0 != args_parse(u, cmd))
goto end;
struct fcom_file_conf fc = {};
fc.buffer_size = cmd->buffer_size;
u->out = core->file->create(&fc);
return u;
end:
utf8_close(u);
return NULL;
}
static ffsize ffutf8_encodedata(char *dst, ffsize cap, const char *src, ffsize *plen, uint flags)
{
ffssize r;
ffsize len = *plen;
switch (flags) {
case FFUNICODE_UTF8:
if (dst == NULL)
return len;
len = ffmin(cap, len);
ffmem_copy(dst, src, len);
*plen = len;
return len;
case FFUNICODE_UTF16LE:
r = ffutf8_from_utf16(dst, cap, src, *plen, FFUNICODE_UTF16LE);
break;
case FFUNICODE_UTF16BE:
r = ffutf8_from_utf16(dst, cap, src, *plen, FFUNICODE_UTF16BE);
break;
default:
r = ffutf8_from_cp(dst, cap, src, *plen, flags);
break;
}
if (r < 0)
return 0;
return r;
}
static ffsize ffutf8_encodewhole(char *dst, ffsize cap, const char *src, ffsize len, uint flags)
{
ffsize ln = len;
ffsize r = ffutf8_encodedata(dst, cap, src, &ln, flags);
if (ln != len)
return 0; //not enough output space
return r;
}
static ffsize ffutf8_strencode(ffvec *dst, const char *src, ffsize len, uint flags)
{
ffsize r = ffutf8_encodewhole(NULL, 0, src, len, flags);
if (NULL == ffvec_realloc(dst, r, 1))
return 0;
r = ffutf8_encodewhole(dst->ptr, dst->cap, src, len, flags);
dst->len = r;
return r;
}
static void utf8_run(fcom_op *op)
{
struct utf8 *u = op;
int r, rc = 1;
enum { I_IN, I_READ, I_PROC, I_OUT_OPEN, I_WRITE };
while (!FFINT_READONCE(u->stop)) {
switch (u->st) {
case I_IN:
if (0 > (r = core->com->input_next(u->cmd, &u->iname, NULL, 0))) {
if (r == FCOM_COM_RINPUT_NOMORE)
rc = 0;
goto end;
}
if (0 != core->com->input_allowed(u->cmd, u->iname, FCOM_COM_IA_AUTO))
continue;
u->st = I_READ;
// fallthrough
case I_READ:
if (0 != fffile_readwhole(u->iname.ptr, &u->fdata, (uint64)-1))
goto end;
u->st = I_PROC;
// fallthrough
case I_PROC: {
ffstr data = *(ffstr*)&u->fdata;
ffsize n = data.len;
int coding = ffutf_bom(data.ptr, &n);
if (coding == -1) {
fcom_infolog("%S: no BOM, skipping file", &u->iname);
u->st = I_IN;
continue;
}
ffstr_shift(&data, n);
u->buf.len = 0;
if (0 == ffutf8_strencode(&u->buf, data.ptr, data.len, coding)) {
fcom_errlog("ffutf8_strencode");
goto end;
}
u->st = I_OUT_OPEN;
}
// fallthrough
case I_OUT_OPEN: {
uint oflags = FCOM_FILE_WRITE;
oflags |= fcom_file_cominfo_flags_o(u->cmd);
r = core->file->open(u->out, u->cmd->output.ptr, oflags);
if (r == FCOM_FILE_ERR) goto end;
u->st = I_WRITE;
}
// fallthrough
case I_WRITE:
r = core->file->write(u->out, *(ffstr*)&u->buf, -1);
if (r == FCOM_FILE_ERR) goto end;
core->file->close(u->out);
u->st = I_IN;
continue;
}
}
end:
{
fcom_cominfo *cmd = u->cmd;
utf8_close(u);
core->com->complete(cmd, rc);
}
}
static void utf8_signal(fcom_op *op, uint signal)
{
struct utf8 *u = op;
FFINT_WRITEONCE(u->stop, 1);
}
static const fcom_operation fcom_op_utf8 = {
utf8_create, utf8_close,
utf8_run, utf8_signal,
utf8_help,
};
FCOM_MOD_DEFINE(utf8, fcom_op_utf8, core)