-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
218 lines (178 loc) · 5.16 KB
/
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parse_block_end.h"
#include "parse_block_start.h"
#include "parse_inline_start.h"
#include "parse_inline_end.h"
#include "parser.h"
#include "utils.h"
/*
* Add text to a text node.
*/
static int
append_text (node_t *text_node, const char *text)
{
if (text_node->type != NODE_TEXT)
{
fprintf (stderr, "parser.c : append_text() : error : trying to add text to a non text node.\n");
return 1;
}
size_t len = text_node->text_content ? strlen (text_node->text_content) : 0;
text_node->text_content = xrealloc (text_node->text_content, len + strlen (text) + 1);
snprintf (text_node->text_content + len, strlen (text) + 1, "%s", text);
return 0;
}
/*
* Add a child to a parent's memory.
*/
void
append_child (node_t *parent, node_t *child)
{
parent->children_len++;
parent->children = xrealloc (parent->children, parent->children_len * sizeof (*child));
parent->children[parent->children_len - 1] = child;
child->parent = parent;
parent->last_child = child;
if (parent->children_len > 1)
parent->children[parent->children_len - 2]->next_sibling = child;
}
/*
* Write text buffer to text node.
*/
int
flush_text_buffer (node_t *current_node, char *buffer, char **buffer_ptr)
{
int err = 0;
if (!current_node->children || current_node->last_child->type != NODE_TEXT)
{
node_t *text_node = xalloc (sizeof *text_node);
text_node->type = NODE_TEXT;
append_child (current_node, text_node);
}
err = append_text (current_node->last_child, buffer);
if (err)
{
fprintf (stderr, "parser.c : flush_text_buffer() : error while append text to text node.\n");
return err;
}
memset (buffer, 0, BUFSIZ);
if (buffer_ptr)
*buffer_ptr = buffer;
return err;
}
/*
* Cleanup memory for a node.
*/
void
free_node (node_t *node)
{
if (node->text_content)
free (node->text_content);
if (node->children)
{
for (size_t i = 0; i < node->children_len; i++)
free_node (node->children[i]);
free (node->children);
}
free (node);
}
/*
* Build a representation of the document, so that it's
* then easier to serialize.
*
* The result is stored in `root`. You should provide
* the memory for it, and clean its content with `free_node()`.
*/
int
parse (const char *filename, node_t *root)
{
int err = 0;
char content[MAX_FILE_SIZE] = {0};
FILE *file = NULL;
file = fopen (filename, "r");
if (!file)
{
fprintf (stderr, "parser.c : parse() : can't read file %s\n", filename);
goto cleanup;
}
size_t content_len = fread (content, 1, MAX_FILE_SIZE - 1, file);
if (content_len == MAX_FILE_SIZE - 1)
fprintf (stderr, "parser.c : parse() : warning : input file has probably been truncated due to its size.\n");
node_t *current_node = root;
char *reading_ptr = content;
char buffer[BUFSIZ] = {0};
char *buffer_ptr = buffer;
bool nowiki = false;
while (true)
{
node_t *initial_node = current_node;
if (strncmp (reading_ptr, "<nowiki>", 8) == 0 && !nowiki)
{
nowiki = true;
reading_ptr += 8;
}
if (strncmp (reading_ptr, "</nowiki>", 9) == 0 && nowiki)
{
nowiki = false;
reading_ptr += 9;
}
if (!nowiki)
{
err = parse_block_end (¤t_node, &reading_ptr, buffer, &buffer_ptr);
if (err)
{
fprintf (stderr, "parser.c : parse() : error while parsing block end.\n");
return err;
}
err = parse_block_start (¤t_node, &reading_ptr);
if (err)
{
fprintf (stderr, "parser.c : parse() : error while parsing block start.\n");
return err;
}
if (!current_node)
break;
if (current_node != initial_node)
continue;
err = parse_inline_start (¤t_node, &reading_ptr, buffer, &buffer_ptr);
if (err)
{
fprintf (stderr, "parser.c : parse() : error while parsing for inline tag start.\n");
return err;
}
if (current_node != initial_node)
continue;
err = parse_inline_end (¤t_node, &reading_ptr, buffer, &buffer_ptr);
if (err)
{
fprintf (stderr, "parser.c : parse() : error while parsing for inline tag end.\n");
return err;
}
if (current_node != initial_node)
continue;
}
if (buffer_ptr - buffer == BUFSIZ - 1)
{
err = flush_text_buffer (current_node, buffer, &buffer_ptr);
if (err)
{
fprintf (stderr, "parser.c : parse() : error while append flushing text buffer.\n");
return err;
}
}
buffer_ptr[0] = reading_ptr[0];
buffer_ptr[1] = 0;
buffer_ptr++;
reading_ptr++;
if ((size_t) (reading_ptr - content) >= content_len - 1)
{
flush_text_buffer (current_node, buffer, &buffer_ptr);
break;
}
}
cleanup:
if (file) fclose (file);
return err;
}