This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gutenberg_post_parser.c
113 lines (83 loc) · 2.65 KB
/
gutenberg_post_parser.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gutenberg_post_parser.h"
void print(const Vector_Node* nodes, int depth) {
const uintptr_t number_of_nodes = nodes->length;
if (number_of_nodes == 0) {
return;
}
printf("%*.*snumber of nodes = %lu\n\n", depth, depth, " ", number_of_nodes);
for (uintptr_t nth = 0; nth < number_of_nodes; ++nth) {
const Node node = nodes->buffer[nth];
if (node.tag == Block) {
const Block_Body block = node.block;
const Slice_c_char namespace = block.namespace;
const Slice_c_char name = block.name;
printf("%*.*sblock\n", depth, depth, " ");
printf(
"%*.*s %.*s/%.*s\n",
depth, depth, " ",
(int) namespace.length, namespace.pointer,
(int) name.length, name.pointer
);
if (block.attributes.tag == Some) {
const Slice_c_char attributes = block.attributes.some._0;
printf(
"%*.*s %.*s\n",
depth, depth, " ",
(int) attributes.length, attributes.pointer
);
}
const Vector_Node* children = (const Vector_Node*) (block.children);
print(children, depth + 4);
free((void*) children);
} else if (node.tag == Phrase) {
const Slice_c_char phrase = node.phrase._0;
printf("%*.*sphrase\n", depth, depth, " ");
printf(
"%*.*s %.*s\n",
depth, depth, " ",
(int) phrase.length, phrase.pointer
);
}
printf("\n");
}
free((void*) nodes->buffer);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Filename is required.");
return 1;
}
FILE* file = fopen(argv[1], "rb");
if (file == NULL) {
printf("Cannot read given file `%s`.", argv[1]);
return 2;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
char* file_content = (char*) malloc(file_size * sizeof(char*));
size_t result = fread(file_content, 1, file_size, file);
if (((long) result) != file_size) {
printf("Error while reading file content.");
return 3;
}
Result output = parse(file_content);
if (output.tag == Err) {
printf("Parse error\n");
return 4;
}
const Vector_Node nodes = output.ok._0;
print(&nodes, 0);
free(file_content);
fclose(file);
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/