forked from prideout/par
-
Notifications
You must be signed in to change notification settings - Fork 0
/
par_string_blocks.h
468 lines (415 loc) · 15.3 KB
/
par_string_blocks.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// STRING_BLOCKS :: https://github.com/prideout/par
// String extraction and concatenation, especially useful for snippets of GLSL or Lua.
//
// This little library extracts blocks of text from a memory blob or file, then lets you retrieve
// them by name. It also makes it easy to glue together a sequence of blocks.
//
// Each block of text is assigned a name using a prefix line that starts with three dash characters,
// such as "--- the_name" or "--- my.block".
//
// For example, suppose you have a file called "shaders.glsl" that looks like this:
//
// --- my_shader
// void main() { ... }
// --- common
// uniform vec4 resolution;
// uniform vec4 color;
//
// You can use this library to read in the file and extract one of the blocks:
//
// parsb_context* blocks = parsb_create_context((parsb_options){});
// parsb_add_blocks_from_file(blocks, "shaders.glsl");
// const char* single = parsb_get_blocks(blocks, "my_shader");
//
// You can also concatenate blocks using a space-delimited list of names:
//
// const char* concatenated = parsb_get_blocks(blocks, "common my_shader");
//
// You can also add or replace blocks on the fly:
//
// parsb_add_block(blocks, "prefix", "#version 330\n");
// const char* concatenated = parsb_get_blocks(blocks, "prefix common my_shader");
//
// The "blocks" context in the above examples holds a cache of generated strings, so be sure to
// destroy it when you're done:
//
// parsb_destroy_context(blocks);
//
// Distributed under the MIT License, see bottom of file.
#ifndef PAR_STRING_BLOCKS_H
#define PAR_STRING_BLOCKS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
// OPTIONS
// -------
// line_directives ... adds #line annotations into concatenated strings for better error messages.
typedef struct parsb_options {
bool line_directives;
} parsb_options;
// CONTEXT CREATION AND DESTRUCTION
// --------------------------------
// A context is an opaque handle to a memory arena. All generated strings are owned by the context
// and freed when the context is destroyed.
typedef struct parsb_context_s parsb_context;
parsb_context* parsb_create_context(parsb_options);
void parsb_destroy_context(parsb_context*);
// ADDING AND REPLACING BLOCKS
// ---------------------------
// When using the plural form (add_blocks), the submitted buffer may contain multiple blocks, each
// with a name defined by its closest preceding triple-dash line. If a block with the specified name
// already exists, it gets replaced.
//
// The singular form (add_block) adds a single block whose name is explicitly specified as an
// argument. Again, if a block with the given name already exists, it gets replaced.
//
// These functions do not retain the passed-in strings so clients can free them after pushing them.
void parsb_add_blocks(parsb_context*, const char* buffer, int buffer_size);
void parsb_add_block(parsb_context*, const char* name, const char* body);
#ifndef PARSB_NO_STDIO
void parsb_add_blocks_from_file(parsb_context* context, const char* filename);
#endif
// EXTRACTING AND CONCATENATING BLOCKS
// -----------------------------------
// The block_names string is a space-separated list of block names that are being requested. The
// returned string is owned by the context, so please make a copy if you need it to outlive the
// context. If the returned string is null, then one or more of the block names could not be found.
const char* parsb_get_blocks(parsb_context*, const char* block_names);
// GETTING BLOCKS BY INDEX
// -----------------------
int parsb_get_num_blocks(const parsb_context*);
void parsb_get_block(const parsb_context*, int index, const char** name, const char** body);
// SAVING THE BLOCK LIST
// ---------------------
// These functions export the entire "database" of atomic blocks.
typedef void (*parsb_write_line)(const char* line, void* userdata);
void parsb_write_blocks(parsb_context*, parsb_write_line writefn, void* user);
#ifndef PARSB_NO_STDIO
void parsb_write_blocks_to_file(parsb_context*, const char* filename);
#endif
#ifndef PARSB_MAX_NUM_BLOCKS
#define PARSB_MAX_NUM_BLOCKS 128
#endif
#ifndef PARSB_MAX_NAME_LENGTH
#define PARSB_MAX_NAME_LENGTH 256
#endif
#ifndef PARSB_MAX_LINE_LENGTH
#define PARSB_MAX_LINE_LENGTH 256
#endif
#ifdef __cplusplus
}
#endif
// -----------------------------------------------------------------------------
// END PUBLIC API
// -----------------------------------------------------------------------------
#ifdef PAR_STRING_BLOCKS_IMPLEMENTATION
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#ifndef PARSB_NO_STDIO
#include <stdio.h>
#endif
#define PARSB_MIN(a, b) (a > b ? b : a)
typedef struct {
int count;
char* values[PARSB_MAX_NUM_BLOCKS];
char* names[PARSB_MAX_NUM_BLOCKS];
} parsb__list;
struct parsb_context_s {
parsb_options options;
parsb__list blocks;
parsb__list results;
};
static char* parsb__add_or_replace(parsb_context*, const char* id, const char* value,
int value_size, int line_number);
static char* parsb__list_add(parsb__list*, const char* id, const char* value, int value_size,
int line_number);
static char* parsb__list_get(parsb__list*, const char* id, int idlen);
static void parsb__list_free(parsb__list* );
parsb_context* parsb_create_context(parsb_options options) {
parsb_context* context = (parsb_context*) calloc(1, sizeof(parsb_context));
context->options = options;
return context;
}
void parsb_destroy_context(parsb_context* context) {
parsb__list_free(&context->blocks);
parsb__list_free(&context->results);
free(context);
}
void parsb_add_blocks(parsb_context* context, const char* blob, int buffer_size) {
const char* previous_block = 0;
char previous_name[PARSB_MAX_NAME_LENGTH];
int line_number = 0;
int block_line_number = 0;
for (int i = 0; i < buffer_size - 4; i++) {
if (blob[i] != '-' || blob[i + 1] != '-' || blob[i + 2] != '-' || blob[i + 3] != ' ') {
if (blob[i] == '\n') {
line_number++;
}
continue;
}
if (previous_block) {
parsb__add_or_replace(context, previous_name, previous_block,
i - (previous_block - blob), block_line_number);
}
i += 4;
const char* name = blob + i;
const char* block_start = 0;
for (; i < buffer_size; i++) {
if (blob[i] == '\n') {
line_number++;
int name_length = i - (name - blob);
memcpy(previous_name, name, name_length);
block_line_number = line_number + 2;
previous_name[name_length] = 0;
block_start = blob + i + 1;
break;
}
if (isspace(blob[i])) {
int name_length = i - (name - blob);
memcpy(previous_name, name, name_length);
block_line_number = line_number + 2;
previous_name[name_length] = 0;
for (i++; i < buffer_size; i++) {
if (blob[i] == '\n') {
line_number++;
block_start = blob + i + 1;
break;
}
}
break;
}
}
if (block_start == 0) {
return;
}
previous_block = block_start;
}
if (previous_block) {
parsb__add_or_replace(context, previous_name, previous_block,
buffer_size - (previous_block - blob), block_line_number);
}
}
void parsb_add_block(parsb_context* context, const char* name, const char* body) {
char* dup = strdup(body);
parsb__add_or_replace(context, name, dup, 1 + strlen(body), 0);
}
const char* parsb_get_blocks(parsb_context* context, const char* block_names) {
int len = strlen(block_names);
const char* name = block_names;
int name_length = 0;
int result_length = 0;
// First pass determines the amount of required memory.
int num_names = 0;
for (int i = 0; i < len; i++) {
char c = block_names[i];
if (isspace(c) || !c) {
const char* block = parsb__list_get(&context->blocks, name, name_length);
if (block) {
result_length += strlen(block);
num_names++;
} else {
return NULL;
}
name_length = 0;
name = block_names + i + 1;
} else {
name_length++;
}
}
const char* block = parsb__list_get(&context->blocks, name, name_length);
if (block) {
result_length += strlen(block);
num_names++;
}
// If no concatenation is required, return early.
if (num_names == 1) {
return parsb__list_get(&context->blocks, name, name_length);
}
// Allocate storage for the result.
char* result = parsb__list_add(&context->results, 0, 0, result_length, 0);
char* cursor = result;
// Second pass populates the result.
name = block_names;
name_length = 0;
for (int i = 0; i < len; i++) {
char c = block_names[i];
if (isspace(c) || !c) {
const char* block = parsb__list_get(&context->blocks, name, name_length);
if (block) {
memcpy(cursor, block, strlen(block));
cursor += strlen(block);
}
name_length = 0;
name = block_names + i + 1;
} else {
name_length++;
}
}
block = parsb__list_get(&context->blocks, name, name_length);
if (block) {
memcpy(cursor, block, strlen(block));
cursor += strlen(block);
}
return result;
}
int parsb_get_num_blocks(const parsb_context* context) {
return context->blocks.count;
}
void parsb_get_block(const parsb_context* context, int index, const char** name,
const char** body) {
if (index < 0 || index >= context->blocks.count) {
return;
}
*name = context->blocks.names[index];
*body = context->blocks.values[index];
}
void parsb_write_blocks(parsb_context* context, parsb_write_line writefn, void* userdata) {
char line[PARSB_MAX_LINE_LENGTH + 1] = {0};
for (int i = 0; i < context->blocks.count; i++) {
sprintf(line, "--- %s", context->blocks.names[i]);
writefn(line, userdata);
const char* cursor = context->blocks.values[i];
const int blocklen = strlen(cursor);
int previous = 0;
for (int i = 0; i < blocklen; i++) {
if (cursor[i] == '\n') {
int line_length = PARSB_MIN(i - previous, PARSB_MAX_LINE_LENGTH);
memcpy(line, cursor + previous, line_length);
line[line_length] = 0;
writefn(line, userdata);
previous = i + 1;
} else if (i == blocklen - 1) {
int line_length = PARSB_MIN(1 + i - previous, PARSB_MAX_LINE_LENGTH);
memcpy(line, cursor + previous, line_length);
line[line_length] = 0;
writefn(line, userdata);
previous = i + 1;
}
}
}
}
static char* parsb__add_or_replace(parsb_context* context, const char* id, const char* value,
int value_size, int line_number) {
line_number = context->options.line_directives ? line_number : 0;
const size_t idlen = strlen(id);
for (int i = 0; i < context->blocks.count; i++) {
if (strncmp(id, context->blocks.names[i], idlen) == 0) {
free(context->blocks.values[i]);
context->blocks.values[i] = strndup(value, value_size);
return context->blocks.values[i];
}
}
return parsb__list_add(&context->blocks, id, value, value_size, line_number);
}
static char* parsb__list_add(parsb__list* list, const char* name,
const char* value, int value_size, int line_number) {
if (value_size == 0) {
return NULL;
}
if (list->count == PARSB_MAX_NUM_BLOCKS) {
assert(false && "Please increase PARSB_MAX_NUM_BLOCKS.");
return NULL;
}
char* storage;
char* cursor;
if (line_number > 0) {
char line_directive[16] = {0};
int prefix_length = snprintf(line_directive, 16, "\n#line %d\n", line_number);
storage = (char*) calloc(1, prefix_length + value_size + 1);
memcpy(storage, line_directive, prefix_length);
cursor = storage + prefix_length;
} else {
storage = cursor = (char*) calloc(1, value_size + 1);
}
if (value) {
memcpy(cursor, value, value_size);
}
#if PARSB_ENABLE_TRIM
value_size--;
while (isspace(cursor[value_size])) {
cursor[value_size] = 0;
value_size--;
if (value_size == 0) {
break;
}
}
#endif
if (name) {
list->names[list->count] = strdup(name);
} else {
list->names[list->count] = 0;
}
list->values[list->count] = storage;
list->count++;
return storage;
}
static char* parsb__list_get(parsb__list* list, const char* name, int idlen) {
for (int i = 0; i < list->count; i++) {
if (strncmp(name, list->names[i], idlen) == 0) {
return list->values[i];
}
}
return NULL;
}
static void parsb__list_free(parsb__list* list) {
for (int i = 0; i < list->count; i++) {
free(list->names[i]);
free(list->values[i]);
}
list->count = 0;
}
#ifndef PARSB_NO_STDIO
void parsb_add_blocks_from_file(parsb_context* context, const char* filename) {
FILE* f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "Unable to open %s\n", filename);
return;
}
fseek(f, 0, SEEK_END);
int length = ftell(f);
fseek(f, 0, SEEK_SET);
char* buffer = (char*) malloc(length);
fread(buffer, 1, length, f);
fclose(f);
parsb_add_blocks(context, buffer, length);
free(buffer);
}
static void writefn(const char* line, void* userdata) {
fprintf((FILE*) userdata, "%s\n", line);
}
void parsb_write_blocks_to_file(parsb_context* context, const char* filename) {
FILE* f = fopen(filename, "w");
if (!f) {
fprintf(stderr, "Unable to open %s\n", filename);
return;
}
parsb_write_blocks(context, writefn, f);
fclose(f);
}
#endif
#endif // PAR_STRING_BLOCKS_IMPLEMENTATION
#endif // PAR_STRING_BLOCKS_H
// par_string_blocks is distributed under the MIT license:
//
// Copyright (c) 2020 Philip Rideout
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.