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
|
/*
* blogc: A blog compiler.
* Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file COPYING.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdbool.h>
#include "utils/utils.h"
#include "source-parser.h"
#include "error.h"
typedef enum {
SOURCE_START = 1,
SOURCE_CONFIG_KEY,
SOURCE_CONFIG_VALUE_START,
SOURCE_CONFIG_VALUE,
SOURCE_SEPARATOR,
SOURCE_CONTENT_START,
SOURCE_CONTENT,
} blogc_source_parser_state_t;
b_trie_t*
blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
{
if (err == NULL || *err != NULL)
return NULL;
size_t current = 0;
size_t start = 0;
char *key = NULL;
char *tmp = NULL;
b_trie_t *rv = b_trie_new(free);
blogc_source_parser_state_t state = SOURCE_START;
while (current < src_len) {
char c = src[current];
switch (state) {
case SOURCE_START:
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
break;
if (c >= 'A' && c <= 'Z') {
state = SOURCE_CONFIG_KEY;
start = current;
break;
}
if (c == '-') {
state = SOURCE_SEPARATOR;
break;
}
*err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len,
current,
"Can't find a configuration key or the content separator.");
break;
case SOURCE_CONFIG_KEY:
if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')
break;
if (c == ':') {
key = b_strndup(src + start, current - start);
state = SOURCE_CONFIG_VALUE_START;
break;
}
*err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len,
current, "Invalid configuration key.");
break;
case SOURCE_CONFIG_VALUE_START:
if (c != '\n' && c != '\r') {
state = SOURCE_CONFIG_VALUE;
start = current;
break;
}
*err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len,
current, "Configuration value not provided for '%s'.",
key);
break;
case SOURCE_CONFIG_VALUE:
if (c == '\n' || c == '\r') {
tmp = b_strndup(src + start, current - start);
b_trie_insert(rv, key, b_strdup(b_str_strip(tmp)));
free(tmp);
free(key);
key = NULL;
state = SOURCE_START;
}
break;
case SOURCE_SEPARATOR:
if (c == '-')
break;
if (c == '\n' || c == '\r') {
state = SOURCE_CONTENT_START;
break;
}
*err = blogc_error_parser(BLOGC_ERROR_SOURCE_PARSER, src, src_len,
current,
"Invalid content separator. Must be one or more '-' characters.");
break;
case SOURCE_CONTENT_START:
start = current;
state = SOURCE_CONTENT;
break;
case SOURCE_CONTENT:
if (current == (src_len - 1))
b_trie_insert(rv, "CONTENT",
b_strndup(src + start, src_len - start));
break;
}
if (*err != NULL)
break;
current++;
}
if (*err != NULL) {
free(key);
b_trie_free(rv);
return NULL;
}
return rv;
}
|