-
Notifications
You must be signed in to change notification settings - Fork 4
/
hell_parser.h
283 lines (248 loc) · 8 KB
/
hell_parser.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* hell_parser - v0.0.1 - MIT LICENSE
*
* This is header only stb style inspired library ;
* It was created for my needs, use at your own risk.
*
* ~~ How To compile code with this library ~~
*
* Do this:
* #define HELL_PARSER_IMPLEMENTATION
* before you include this file in *one* C or C++ file to create the implementation.
*
* #define HELL_PARSER_IMPLEMENTATION
* #include "hellparser.h"
*
* You can #define HELL_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
*
*
* Example code:
*
* #include <stdlib.h>
* #include <stdio.h>
*
* #define HELL_PARSER_IMPLEMENTATION
* #include "../../hell_parser.h"
*
* int main()
* {
* char delim = '%';
* char *input_text = "background=%%color0.hex%%\n \
* foreground=%%color15.hex%%";
*
* // Initialize parser with input
* hell_parser_t *parser = hell_parser_create(input_text);
* char *buffer = NULL;
*
* while (!hell_parser_eof(parser))
* {
* char ch;
*
* if (hell_parser_next(parser, &ch) == HELL_PARSER_OK)
* {
* if (ch == delim)
* {
* parser->pos -= 1; // We have to do that so hell_parser_delim_buffer_between
* // keeps track of the position
*
* // Look out for delim, which is '%',
* // count is set to 2, so it will trigger when it finds "%%"
* if (hell_parser_delim_buffer_between(parser, delim, 2, &buffer) == HELL_PARSER_OK)
* {
* printf("Extracted content: '%s'\n", buffer);
* free(buffer);
* }
* }
* }
* }
*
* hell_parser_destroy(parser);
* return 0;
* }
*/
#ifndef HELL_PARSER_H
#define HELL_PARSER_H
#ifdef __cplusplus
extern "C" {
#endif
/* Version information */
#define HELL_PARSER_VERSION "0.0.1"
/* Configurable Options */
#ifndef HELL_DEF
#define HELL_DEF static inline
#else
#define HELL_DEF extern
#endif
/* Standard library dependencies */
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
/* Define your own malloc etc. */
#if defined(HELL_MALLOC) && defined(HELL_FREE) && (defined(HELL_REALLOC) || defined(HELL_REALLOC_SIZED))
// ok
#elif !defined(HELL_MALLOC) && !defined(HELL_FREE) && !defined(HELL_REALLOC) && !defined(HELL_REALLOC_SIZED)
// ok
#else
#error "Must define all or none of HELL_MALLOC, HELL_FREE, and HELL_REALLOC (or HELL_REALLOC_SIZED)."
#endif
#ifndef HELL_MALLOC
#define HELL_MALLOC(sz) malloc(sz)
#define HELL_REALLOC(p,newsz) realloc(p,newsz)
#define HELL_FREE(p) free(p)
#endif
#ifndef HELL_REALLOC_SIZED
#define HELL_REALLOC_SIZED(p,oldsz,newsz) HELL_REALLOC(p,newsz)
#endif
/* Library Types and Structures */
typedef enum
{
HELL_PARSER_OK = 0,
HELL_PARSER_ERROR = -1
} hell_parser_status_t;
typedef struct
{
const char *input; /* Input string to parse */
size_t pos; /* Current position in the input */
size_t length; /* Length of the input string */
} hell_parser_t;
/* Function Declarations */
HELL_DEF void hell_parser_destroy(hell_parser_t *parser);
HELL_DEF int hell_parser_eof(const hell_parser_t *parser);
HELL_DEF hell_parser_t *hell_parser_create(const char *input);
HELL_DEF hell_parser_status_t hell_parser_next(hell_parser_t *parser, char *out);
HELL_DEF hell_parser_status_t hell_parser_delim(hell_parser_t *parser, char delim, unsigned count);
HELL_DEF hell_parser_status_t hell_parser_delim_buffer_between(hell_parser_t *parser, char delim, unsigned count, char **buffer);
/* Implementation */
#ifdef HELL_PARSER_IMPLEMENTATION
HELL_DEF hell_parser_t *hell_parser_create(const char *input)
{
if (!input) return NULL;
hell_parser_t *parser = (hell_parser_t *)malloc(sizeof(hell_parser_t));
if (!parser) return NULL;
parser->input = input;
parser->pos = 0;
parser->length = strlen(input);
return parser;
}
HELL_DEF void hell_parser_destroy(hell_parser_t *parser)
{
if (parser)
{
free(parser);
}
}
/* Go next by one character, and pass it to *out,
* if it's out of bounds return error
*/
HELL_DEF hell_parser_status_t hell_parser_next(hell_parser_t *parser, char *out)
{
if (!parser || !out || parser->pos >= parser->length)
{
return HELL_PARSER_ERROR;
}
*out = parser->input[parser->pos++];
return HELL_PARSER_OK;
}
HELL_DEF hell_parser_status_t hell_parser_delim(hell_parser_t *parser, char delim, unsigned count)
{
if (!parser)
return HELL_PARSER_ERROR;
count = (count == 0) ? 1 : count; /* Default count to 1 if 0 */
size_t matched = 0; /* Tracks consecutive delimiter matches */
while (!hell_parser_eof(parser))
{
char current;
if (hell_parser_next(parser, ¤t) != HELL_PARSER_OK)
break;
if (current == delim)
{
matched++;
if (matched == count)
return HELL_PARSER_OK;
}
else
matched = 0;
}
return HELL_PARSER_ERROR;
}
/* Get content inside delim [count] times, if count is 0 it defaults to 1.
* For example, for '%' with count 2, it triggers on "%%" in the text.
* Dynamically allocates memory for the content between delimiters and stores it in `*buffer`.
* CALLER MUST FREE THE ALLOCATED MEMORY.
*/
HELL_DEF hell_parser_status_t hell_parser_delim_buffer_between(hell_parser_t *parser, char delim, unsigned count, char **buffer)
{
if (!parser || !buffer)
return HELL_PARSER_ERROR;
count = (count == 0) ? 1 : count; /* Default count to 1 if 0 */
size_t matched = 0; /* Tracks consecutive delimiter matches */
size_t capacity = 64; /* Initial buffer capacity */
size_t buffer_pos = 0; /* Tracks position in the buffer */
int inside = 0; /* Flag to determine if we're inside delimiters */
*buffer = (char *)malloc(capacity);
if (!*buffer) {
return HELL_PARSER_ERROR;
}
while (!hell_parser_eof(parser))
{
char current;
if (hell_parser_next(parser, ¤t) != HELL_PARSER_OK)
break;
if (current == delim)
{
matched++;
if (matched == count)
{
if (inside)
{
(*buffer)[buffer_pos] = '\0';
return HELL_PARSER_OK;
}
else
{
inside = 1;
buffer_pos = 0;
matched = 0;
}
}
}
else
{
if (inside)
{
int last_matched_unmatched = 0;
if (matched > 0)
last_matched_unmatched = matched;
if (buffer_pos + 1 + last_matched_unmatched >= capacity)
{
capacity *= 2;
char *new_buffer = (char *)realloc(*buffer, capacity);
if (!new_buffer)
{
free(*buffer);
return HELL_PARSER_ERROR;
}
*buffer = new_buffer;
}
while (last_matched_unmatched > 0)
{
last_matched_unmatched--;
(*buffer)[buffer_pos++] = delim;
}
(*buffer)[buffer_pos++] = current;
}
matched = 0;
}
}
free(*buffer);
*buffer = NULL;
return HELL_PARSER_ERROR;
}
HELL_DEF int hell_parser_eof(const hell_parser_t *parser)
{
return (parser && parser->pos >= parser->length);
}
#endif /* HELL_PARSER_IMPLEMENTATION */
#ifdef __cplusplus
}
#endif
#endif /* HELL_PARSER_H */