-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum8_builder.c
More file actions
95 lines (81 loc) · 1.99 KB
/
num8_builder.c
File metadata and controls
95 lines (81 loc) · 1.99 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
#include "num8.h"
#include <stdio.h>
#include <string.h>
static int trim_eol(char* s)
{
size_t n = strlen(s);
while (n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r'))
{
s[n - 1] = '\0';
n--;
}
return (int)n;
}
int main(int argc, char** argv)
{
FILE* in;
num8_engine_t engine;
char line[256];
unsigned long long line_no = 0;
unsigned long long added = 0;
unsigned long long duplicates = 0;
if (argc != 3)
{
fprintf(stderr, "Usage: %s <input.txt> <output.num8>\n", argv[0]);
return 2;
}
in = fopen(argv[1], "rb");
if (in == NULL)
{
fprintf(stderr, "Cannot open input: %s\n", argv[1]);
return 1;
}
if (num8_create(argv[2], &engine) != NUM8_STATUS_OK)
{
fclose(in);
fprintf(stderr, "Cannot create output: %s\n", argv[2]);
return 1;
}
while (fgets(line, (int)sizeof(line), in) != NULL)
{
num8_status_t st;
line_no++;
trim_eol(line);
if (line[0] == '\0')
{
fprintf(stderr, "Invalid empty line at %llu\n", line_no);
num8_close(&engine);
fclose(in);
return 1;
}
st = num8_add_str(&engine, line);
if (st == NUM8_STATUS_ADDED)
{
added++;
continue;
}
if (st == NUM8_STATUS_ALREADY_EXISTS)
{
duplicates++;
continue;
}
fprintf(stderr, "Invalid value at line %llu: '%s' (status=%d)\n", line_no, line, (int)st);
num8_close(&engine);
fclose(in);
return 1;
}
fclose(in);
if (num8_flush(&engine) != NUM8_STATUS_OK)
{
num8_close(&engine);
fprintf(stderr, "Flush failed\n");
return 1;
}
if (num8_close(&engine) != NUM8_STATUS_OK)
{
fprintf(stderr, "Close failed\n");
return 1;
}
fprintf(stdout, "Build done. added=%llu duplicates=%llu\n", added, duplicates);
return 0;
}