-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.c
105 lines (79 loc) · 2.77 KB
/
build.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
#include <_string.h>
#include <stdio.h>
#include <stdlib.h>
#include "arena.h"
#include "csv.h"
#include "fs.h"
#include "strchrepl.h"
#include "struppercase.h"
void render_methods(struct csv_t *csv) {
FILE* output = fs_open("./http_methods.h", "w");
fprintf(output, "#ifndef __HTTP_TYPES_METHODS__\n");
fprintf(output, "#define __HTTP_TYPES_METHODS__ 1\n\n");
for (size_t i = 1; i < csv->row_count - 1; ++i) {
char** row = csv_row(csv, i);
fprintf(output, "#define HTTP_METHODS_%s \"%s\"\n", row[0], row[0]);
}
fprintf(output, "\n#endif // __HTTP_TYPES_METHODS__\n");
fclose(output);
}
// http-status-codes-1.csv
void render_headers(struct csv_t *csv) {
FILE* output = fs_open("./http_headers.h", "w");
fprintf(output, "#ifndef __HTTP_TYPES_HEADERS__\n");
fprintf(output, "#define __HTTP_TYPES_HEADERS__ 1\n\n");
for (size_t i = 1; i < csv->row_count - 1; ++i) {
char** row = csv_row(csv, i);
char* name = strdup(row[0]);
struppercase(name);
(void)strchrepl(name, '-', '_');
fprintf(output, "#define HTTP_HEADER_%s \"%s\"\n", name, row[0]);
free(name);
}
fprintf(output, "\n#endif // __HTTP_TYPES_HEADERS__\n");
fclose(output);
}
void render_statuses(struct csv_t *csv) {
FILE* output = fs_open("./http_statuses.h", "w");
fprintf(output, "#ifndef __HTTP_TYPES_STATUSES__\n");
fprintf(output, "#define __HTTP_TYPES_STATUSES__ 1\n\n");
for (size_t i = 1; i < csv->row_count - 1; ++i) {
char** row = csv_row(csv, i);
char* name = strdup(row[1]);
(void)strchrepl(name, ' ', '_');
(void)strchrepl(name, '-', '_');
struppercase(name);
if (memcmp(row[1], "Unassigned", strlen("Unassigned")) == 0 ||
memcmp(row[1], "(Unused)", strlen("(Unused)")) == 0 ||
memcmp(row[0], "105", strlen("105")) == 0 ||
memcmp(row[0], "104", strlen("104")) == 0 ||
memcmp(row[0], "510", strlen("510")) == 0) {
fprintf(output, "// #define HTTP_STATUS_%s \"%s\"\n", row[0], row[0]);
fprintf(output, "// #define HTTP_STATUS_%s \"%s\"\n", name, row[1]);
} else {
fprintf(output, "#define HTTP_STATUS_%s %s\n", row[0], row[0]);
fprintf(output, "#define HTTP_STATUS_%s \"%s\"\n", name, row[1]);
}
free(name);
}
fprintf(output, "\n#endif // __HTTP_TYPES_STATUSES__\n");
fclose(output);
}
int build(char* filename, void (*render)(struct csv_t*)) {
arena_t arena = {0};
arena_create(&arena, 4096 * 16);
struct csv_t csv = {0};
const char *content = fs_read(filename);
(void)csv_parse(&arena, &csv, content);
render(&csv);
free((char*)content);
// csv_free(&csv);
arena_free(&arena);
return 0;
}
int main(void) {
build("methods.csv", render_methods);
build("field-names.csv", render_headers);
build("http-status-codes-1.csv", render_statuses);
return 0;
}