-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathfrom_to.c
More file actions
193 lines (158 loc) · 6.37 KB
/
from_to.c
File metadata and controls
193 lines (158 loc) · 6.37 KB
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
/*
* Copyright (C) 2019-2020 Alexander Borisov
*
* Author: Alexander Borisov <[email protected]>
*/
#include <lexbor/encoding/encoding.h>
#define FAILED(with_usage, ...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
\
if (with_usage) { \
usage(); \
} \
\
exit(EXIT_FAILURE); \
} \
while (0)
static void
usage(void)
{
printf("Usage: from_to <from> <to>\n\n");
printf("Available encodings for 'from' and 'to':\n");
printf("big5, euc-jp, euc-kr, gbk, ibm866, iso-2022-jp, iso-8859-10, iso-8859-13,\n");
printf("iso-8859-14, iso-8859-15, iso-8859-16, iso-8859-2, iso-8859-3, iso-8859-4,\n");
printf("iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-8-i, koi8-r, koi8-u,\n");
printf("shift_jis, utf-16be, utf-16le, utf-8, gb18030, macintosh, replacement,\n");
printf("windows-1250, windows-1251, windows-1252, windows-1253, windows-1254,\n");
printf("windows-1255, windows-1256, windows-1257, windows-1258, windows-874,\n");
printf("x-mac-cyrillic, x-user-defined.\n");
}
int
main(int argc, const char *argv[])
{
size_t size;
lxb_status_t status, encode_status, decode_status;
lxb_encoding_encode_t encode;
lxb_encoding_decode_t decode;
const lxb_encoding_data_t *from, *to;
bool loop = true;
/* Encode */
lxb_char_t outbuf[4096];
/* Decode */
lxb_codepoint_t cp[4096];
const lxb_codepoint_t *cp_ref, *cp_end;
/* Incoming from stdin */
char inbuf[4096];
const lxb_char_t *data, *end;
if (argc != 3) {
usage();
exit(EXIT_SUCCESS);
}
/* Get encoding data for 'from' */
from = lxb_encoding_data_by_pre_name((const lxb_char_t *) argv[1],
strlen(argv[1]));
if (from == NULL) {
FAILED(true, "Failed to get encoding from name: %s\n", argv[1]);
}
/* Get encoding data for 'to' */
to = lxb_encoding_data_by_pre_name((const lxb_char_t *) argv[2],
strlen(argv[2]));
if (to == NULL) {
FAILED(true, "Failed to get encoding from name: %s\n", argv[2]);
}
/* Initialization decode */
status = lxb_encoding_decode_init(&decode, from, cp,
sizeof(cp) / sizeof(lxb_codepoint_t));
if (status != LXB_STATUS_OK) {
FAILED(false, "Failed to initialization decoder");
}
status = lxb_encoding_decode_replace_set(&decode,
LXB_ENCODING_REPLACEMENT_BUFFER, LXB_ENCODING_REPLACEMENT_BUFFER_LEN);
if (status != LXB_STATUS_OK) {
FAILED(false, "Failed to set replacement code point for decoder");
}
/* Initialization encode */
status = lxb_encoding_encode_init(&encode, to, outbuf, sizeof(outbuf));
if (status != LXB_STATUS_OK) {
FAILED(false, "Failed to initialization encoder");
}
if (to->encoding == LXB_ENCODING_UTF_8) {
status = lxb_encoding_encode_replace_set(&encode,
LXB_ENCODING_REPLACEMENT_BYTES, LXB_ENCODING_REPLACEMENT_SIZE);
}
else {
status = lxb_encoding_encode_replace_set(&encode, (lxb_char_t *) "?", 1);
}
if (status != LXB_STATUS_OK) {
FAILED(false, "Failed to set replacement bytes for encoder");
}
do {
/* Read standart input */
size = fread(inbuf, 1, sizeof(inbuf), stdin);
if (size != sizeof(inbuf)) {
if (feof(stdin)) {
loop = false;
}
else {
FAILED(false, "Failed to read stdin");
}
}
/* Decode incoming data */
data = (const lxb_char_t *) inbuf;
end = data + size;
do {
/* Decode */
decode_status = from->decode(&decode, &data, end);
cp_ref = cp;
cp_end = cp + lxb_encoding_decode_buf_used(&decode);
do {
encode_status = to->encode(&encode, &cp_ref, cp_end);
if (encode_status == LXB_STATUS_ERROR) {
cp_ref++;
encode_status = LXB_STATUS_SMALL_BUFFER;
}
size = lxb_encoding_encode_buf_used(&encode);
/* The printf function cannot print \x00, it can be in UTF-16 */
if (fwrite(outbuf, 1, size, stdout) != size) {
FAILED(false, "Failed to write data to stdout");
}
lxb_encoding_encode_buf_used_set(&encode, 0);
}
while (encode_status == LXB_STATUS_SMALL_BUFFER);
lxb_encoding_decode_buf_used_set(&decode, 0);
}
while (decode_status == LXB_STATUS_SMALL_BUFFER);
}
while (loop);
/* End of file */
/* In this moment encoder and decoder out buffer is empty */
/*
* First: finish decoding.
* If there was not enough data to create the code point,
* then the replacement character will put to the buffer.
*/
(void) lxb_encoding_decode_finish(&decode);
if (lxb_encoding_decode_buf_used(&decode)) {
cp_ref = cp;
cp_end = cp + lxb_encoding_decode_buf_used(&decode);
(void) to->encode(&encode, &cp_ref, cp_end);
size = lxb_encoding_encode_buf_used(&encode);
if (fwrite(outbuf, 1, size, stdout) != size) {
FAILED(false, "Failed to write data to stdout");
}
}
/*
* Second: finish encoding.
* The truth is that this is only necessary for one encoding: iso-2022-jp.
*/
(void) lxb_encoding_encode_finish(&encode);
size = lxb_encoding_encode_buf_used(&encode);
if (size != 0) {
if (fwrite(outbuf, 1, size, stdout) != size) {
FAILED(false, "Failed to write data to stdout");
}
}
return EXIT_SUCCESS;
}