-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
56 lines (48 loc) · 1.41 KB
/
main.c
File metadata and controls
56 lines (48 loc) · 1.41 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
#include "utf8.h"
#include <stdio.h>
#include <string.h>
enum { OK, INPUT_ERROR, INTERNAL_ERROR, };
int main(int argc, char **argv) {
FILE *in = stdin, *e = stderr, *o = stdout;
unsigned long version = 0;
if (utf8_version(&version) < 0) {
(void)fprintf(e, "version not set by build system\n");
return INTERNAL_ERROR;
}
if (utf8_tests() < 0) {
(void)fprintf(e, "internal tests failed\n");
return INTERNAL_ERROR;
}
if (argc == 1) {
unsigned long state = 0, codep = 0;
unsigned long long count = 0, bytes = 0;
for (int ch = 0; (ch = fgetc(in)) != EOF; bytes++) {
const int r = utf8_decode(&state, &codep, ch);
if (r < 0) {
(void)fprintf(e, "invalid UTF-8 at/before byte %ld\n", (unsigned long)bytes);
return INPUT_ERROR;
}
count += r == 0;
}
if (fprintf(o, "%ld\n", (unsigned long)count) < 0)
return INPUT_ERROR;
return 0;
}
if (argc == 2) {
size_t count = 0;
if (utf8_code_points(argv[1], strlen(argv[1]), &count) < 0) {
if (fputs("invalid\n", o) < 0)
return INTERNAL_ERROR;
return INPUT_ERROR;
}
if (fprintf(o, "%lu\n", (unsigned long)count) < 0)
return INTERNAL_ERROR;
return 0;
}
static const char *help = "usage: %s [string]\n\n\
Simple UTF-8 validator, see <http://bjoern.hoehrmann.de/utf-8/decoder/dfa/>\n\
for original source and <https://github.com/howerj/utf8> for this programs\n\
source.\n\n";
(void)fprintf(e, help, argv[0]);
return INPUT_ERROR;
}