par_string_blocks.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // STRING_BLOCKS :: https://github.com/prideout/par
  2. // String extraction and concatenation, especially useful for snippets of GLSL or Lua.
  3. //
  4. // This little library extracts blocks of text from a memory blob or file, then lets you retrieve
  5. // them by name. It also makes it easy to glue together a sequence of blocks.
  6. //
  7. // Each block of text is assigned a name using a prefix line that starts with three dash characters,
  8. // such as "--- the_name" or "--- my.block".
  9. //
  10. // For example, suppose you have a file called "shaders.glsl" that looks like this:
  11. //
  12. // --- my_shader
  13. // void main() { ... }
  14. // --- common
  15. // uniform vec4 resolution;
  16. // uniform vec4 color;
  17. //
  18. // You can use this library to read in the file and extract one of the blocks:
  19. //
  20. // parsb_context* blocks = parsb_create_context((parsb_options){});
  21. // parsb_add_blocks_from_file(blocks, "shaders.glsl");
  22. // const char* single = parsb_get_blocks(blocks, "my_shader");
  23. //
  24. // You can also concatenate blocks using a space-delimited list of names:
  25. //
  26. // const char* concatenated = parsb_get_blocks(blocks, "common my_shader");
  27. //
  28. // You can also add or replace blocks on the fly:
  29. //
  30. // parsb_add_block(blocks, "prefix", "#version 330\n");
  31. // const char* concatenated = parsb_get_blocks(blocks, "prefix common my_shader");
  32. //
  33. // The "blocks" context in the above examples holds a cache of generated strings, so be sure to
  34. // destroy it when you're done:
  35. //
  36. // parsb_destroy_context(blocks);
  37. //
  38. // Distributed under the MIT License, see bottom of file.
  39. #ifndef PAR_STRING_BLOCKS_H
  40. #define PAR_STRING_BLOCKS_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #include <stdbool.h>
  45. // OPTIONS
  46. // -------
  47. // line_directives ... adds #line annotations into concatenated strings for better error messages.
  48. typedef struct parsb_options {
  49. bool line_directives;
  50. } parsb_options;
  51. // CONTEXT CREATION AND DESTRUCTION
  52. // --------------------------------
  53. // A context is an opaque handle to a memory arena. All generated strings are owned by the context
  54. // and freed when the context is destroyed.
  55. typedef struct parsb_context_s parsb_context;
  56. parsb_context* parsb_create_context(parsb_options);
  57. void parsb_destroy_context(parsb_context*);
  58. // ADDING AND REPLACING BLOCKS
  59. // ---------------------------
  60. // When using the plural form (add_blocks), the submitted buffer may contain multiple blocks, each
  61. // with a name defined by its closest preceding triple-dash line. If a block with the specified name
  62. // already exists, it gets replaced.
  63. //
  64. // The singular form (add_block) adds a single block whose name is explicitly specified as an
  65. // argument. Again, if a block with the given name already exists, it gets replaced.
  66. //
  67. // These functions do not retain the passed-in strings so clients can free them after pushing them.
  68. void parsb_add_blocks(parsb_context*, const char* buffer, int buffer_size);
  69. void parsb_add_block(parsb_context*, const char* name, const char* body);
  70. #ifndef PARSB_NO_STDIO
  71. void parsb_add_blocks_from_file(parsb_context* context, const char* filename);
  72. #endif
  73. // EXTRACTING AND CONCATENATING BLOCKS
  74. // -----------------------------------
  75. // The block_names string is a space-separated list of block names that are being requested. The
  76. // returned string is owned by the context, so please make a copy if you need it to outlive the
  77. // context. If the returned string is null, then one or more of the block names could not be found.
  78. const char* parsb_get_blocks(parsb_context*, const char* block_names);
  79. // GETTING BLOCKS BY INDEX
  80. // -----------------------
  81. int parsb_get_num_blocks(const parsb_context*);
  82. void parsb_get_block(const parsb_context*, int index, const char** name, const char** body);
  83. // SAVING THE BLOCK LIST
  84. // ---------------------
  85. // These functions export the entire "database" of atomic blocks.
  86. typedef void (*parsb_write_line)(const char* line, void* userdata);
  87. void parsb_write_blocks(parsb_context*, parsb_write_line writefn, void* user);
  88. #ifndef PARSB_NO_STDIO
  89. void parsb_write_blocks_to_file(parsb_context*, const char* filename);
  90. #endif
  91. #ifndef PARSB_MAX_NUM_BLOCKS
  92. #define PARSB_MAX_NUM_BLOCKS 128
  93. #endif
  94. #ifndef PARSB_MAX_NAME_LENGTH
  95. #define PARSB_MAX_NAME_LENGTH 256
  96. #endif
  97. #ifndef PARSB_MAX_LINE_LENGTH
  98. #define PARSB_MAX_LINE_LENGTH 256
  99. #endif
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. // -----------------------------------------------------------------------------
  104. // END PUBLIC API
  105. // -----------------------------------------------------------------------------
  106. #ifdef PAR_STRING_BLOCKS_IMPLEMENTATION
  107. #ifndef _GNU_SOURCE
  108. #define _GNU_SOURCE // required for str(n)dup
  109. #endif // _GNU_SOURCE
  110. #include <assert.h>
  111. #include <ctype.h>
  112. #include <stdlib.h>
  113. #include <string.h>
  114. #ifndef PARSB_NO_STDIO
  115. #include <stdio.h>
  116. #endif
  117. #define PARSB_MIN(a, b) (a > b ? b : a)
  118. typedef struct {
  119. int count;
  120. char* values[PARSB_MAX_NUM_BLOCKS];
  121. char* names[PARSB_MAX_NUM_BLOCKS];
  122. } parsb__list;
  123. struct parsb_context_s {
  124. parsb_options options;
  125. parsb__list blocks;
  126. parsb__list results;
  127. };
  128. static char* parsb__add_or_replace(parsb_context*, const char* id, const char* value,
  129. int value_size, int line_number);
  130. static char* parsb__list_add(parsb__list*, const char* id, const char* value, int value_size,
  131. int line_number);
  132. static char* parsb__list_get(parsb__list*, const char* id, int idlen);
  133. static void parsb__list_free(parsb__list* );
  134. parsb_context* parsb_create_context(parsb_options options) {
  135. parsb_context* context = (parsb_context*) calloc(1, sizeof(parsb_context));
  136. context->options = options;
  137. return context;
  138. }
  139. void parsb_destroy_context(parsb_context* context) {
  140. parsb__list_free(&context->blocks);
  141. parsb__list_free(&context->results);
  142. free(context);
  143. }
  144. void parsb_add_blocks(parsb_context* context, const char* blob, int buffer_size) {
  145. const char* previous_block = 0;
  146. char previous_name[PARSB_MAX_NAME_LENGTH];
  147. int line_number = 0;
  148. int block_line_number = 0;
  149. for (int i = 0; i < buffer_size - 4; i++) {
  150. if (blob[i] != '-' || blob[i + 1] != '-' || blob[i + 2] != '-' || blob[i + 3] != ' ') {
  151. if (blob[i] == '\n') {
  152. line_number++;
  153. }
  154. continue;
  155. }
  156. if (previous_block) {
  157. parsb__add_or_replace(context, previous_name, previous_block,
  158. i - (previous_block - blob), block_line_number);
  159. }
  160. i += 4;
  161. const char* name = blob + i;
  162. const char* block_start = 0;
  163. for (; i < buffer_size; i++) {
  164. if (blob[i] == '\n') {
  165. line_number++;
  166. int name_length = i - (name - blob);
  167. memcpy(previous_name, name, name_length);
  168. block_line_number = line_number + 2;
  169. previous_name[name_length] = 0;
  170. block_start = blob + i + 1;
  171. break;
  172. }
  173. if (isspace(blob[i])) {
  174. int name_length = i - (name - blob);
  175. memcpy(previous_name, name, name_length);
  176. block_line_number = line_number + 2;
  177. previous_name[name_length] = 0;
  178. for (i++; i < buffer_size; i++) {
  179. if (blob[i] == '\n') {
  180. line_number++;
  181. block_start = blob + i + 1;
  182. break;
  183. }
  184. }
  185. break;
  186. }
  187. }
  188. if (block_start == 0) {
  189. return;
  190. }
  191. previous_block = block_start;
  192. }
  193. if (previous_block) {
  194. parsb__add_or_replace(context, previous_name, previous_block,
  195. buffer_size - (previous_block - blob), block_line_number);
  196. }
  197. }
  198. void parsb_add_block(parsb_context* context, const char* name, const char* body) {
  199. char* dup = strdup(body);
  200. parsb__add_or_replace(context, name, dup, 1 + strlen(body), 0);
  201. }
  202. const char* parsb_get_blocks(parsb_context* context, const char* block_names) {
  203. int len = strlen(block_names);
  204. const char* name = block_names;
  205. int name_length = 0;
  206. int result_length = 0;
  207. // First pass determines the amount of required memory.
  208. int num_names = 0;
  209. for (int i = 0; i < len; i++) {
  210. char c = block_names[i];
  211. if (isspace(c) || !c) {
  212. const char* block = parsb__list_get(&context->blocks, name, name_length);
  213. if (block) {
  214. result_length += strlen(block);
  215. num_names++;
  216. } else {
  217. return NULL;
  218. }
  219. name_length = 0;
  220. name = block_names + i + 1;
  221. } else {
  222. name_length++;
  223. }
  224. }
  225. const char* block = parsb__list_get(&context->blocks, name, name_length);
  226. if (block) {
  227. result_length += strlen(block);
  228. num_names++;
  229. }
  230. // If no concatenation is required, return early.
  231. if (num_names == 1) {
  232. return parsb__list_get(&context->blocks, name, name_length);
  233. }
  234. // Allocate storage for the result.
  235. char* result = parsb__list_add(&context->results, 0, 0, result_length, 0);
  236. char* cursor = result;
  237. // Second pass populates the result.
  238. name = block_names;
  239. name_length = 0;
  240. for (int i = 0; i < len; i++) {
  241. char c = block_names[i];
  242. if (isspace(c) || !c) {
  243. const char* block = parsb__list_get(&context->blocks, name, name_length);
  244. if (block) {
  245. memcpy(cursor, block, strlen(block));
  246. cursor += strlen(block);
  247. }
  248. name_length = 0;
  249. name = block_names + i + 1;
  250. } else {
  251. name_length++;
  252. }
  253. }
  254. block = parsb__list_get(&context->blocks, name, name_length);
  255. if (block) {
  256. memcpy(cursor, block, strlen(block));
  257. cursor += strlen(block);
  258. }
  259. return result;
  260. }
  261. int parsb_get_num_blocks(const parsb_context* context) {
  262. return context->blocks.count;
  263. }
  264. void parsb_get_block(const parsb_context* context, int index, const char** name,
  265. const char** body) {
  266. if (index < 0 || index >= context->blocks.count) {
  267. return;
  268. }
  269. *name = context->blocks.names[index];
  270. *body = context->blocks.values[index];
  271. }
  272. void parsb_write_blocks(parsb_context* context, parsb_write_line writefn, void* userdata) {
  273. char line[PARSB_MAX_LINE_LENGTH + 1] = {0};
  274. for (int i = 0; i < context->blocks.count; i++) {
  275. sprintf(line, "--- %s", context->blocks.names[i]);
  276. writefn(line, userdata);
  277. const char* cursor = context->blocks.values[i];
  278. const int blocklen = strlen(cursor);
  279. int previous = 0;
  280. for (int i = 0; i < blocklen; i++) {
  281. if (cursor[i] == '\n') {
  282. int line_length = PARSB_MIN(i - previous, PARSB_MAX_LINE_LENGTH);
  283. memcpy(line, cursor + previous, line_length);
  284. line[line_length] = 0;
  285. writefn(line, userdata);
  286. previous = i + 1;
  287. } else if (i == blocklen - 1) {
  288. int line_length = PARSB_MIN(1 + i - previous, PARSB_MAX_LINE_LENGTH);
  289. memcpy(line, cursor + previous, line_length);
  290. line[line_length] = 0;
  291. writefn(line, userdata);
  292. previous = i + 1;
  293. }
  294. }
  295. }
  296. }
  297. static char* parsb__add_or_replace(parsb_context* context, const char* id, const char* value,
  298. int value_size, int line_number) {
  299. line_number = context->options.line_directives ? line_number : 0;
  300. const size_t idlen = strlen(id);
  301. for (int i = 0; i < context->blocks.count; i++) {
  302. if (strncmp(id, context->blocks.names[i], idlen) == 0) {
  303. free(context->blocks.values[i]);
  304. context->blocks.values[i] = strndup(value, value_size);
  305. return context->blocks.values[i];
  306. }
  307. }
  308. return parsb__list_add(&context->blocks, id, value, value_size, line_number);
  309. }
  310. static char* parsb__list_add(parsb__list* list, const char* name,
  311. const char* value, int value_size, int line_number) {
  312. if (value_size == 0) {
  313. return NULL;
  314. }
  315. if (list->count == PARSB_MAX_NUM_BLOCKS) {
  316. assert(false && "Please increase PARSB_MAX_NUM_BLOCKS.");
  317. return NULL;
  318. }
  319. char* storage;
  320. char* cursor;
  321. if (line_number > 0) {
  322. char line_directive[16] = {0};
  323. int prefix_length = snprintf(line_directive, 16, "\n#line %d\n", line_number);
  324. storage = (char*) calloc(1, prefix_length + value_size + 1);
  325. memcpy(storage, line_directive, prefix_length);
  326. cursor = storage + prefix_length;
  327. } else {
  328. storage = cursor = (char*) calloc(1, value_size + 1);
  329. }
  330. if (value) {
  331. memcpy(cursor, value, value_size);
  332. }
  333. #if PARSB_ENABLE_TRIM
  334. value_size--;
  335. while (isspace(cursor[value_size])) {
  336. cursor[value_size] = 0;
  337. value_size--;
  338. if (value_size == 0) {
  339. break;
  340. }
  341. }
  342. #endif
  343. if (name) {
  344. list->names[list->count] = strdup(name);
  345. } else {
  346. list->names[list->count] = 0;
  347. }
  348. list->values[list->count] = storage;
  349. list->count++;
  350. return storage;
  351. }
  352. static char* parsb__list_get(parsb__list* list, const char* name, int idlen) {
  353. for (int i = 0; i < list->count; i++) {
  354. if (strncmp(name, list->names[i], idlen) == 0) {
  355. return list->values[i];
  356. }
  357. }
  358. return NULL;
  359. }
  360. static void parsb__list_free(parsb__list* list) {
  361. for (int i = 0; i < list->count; i++) {
  362. free(list->names[i]);
  363. free(list->values[i]);
  364. }
  365. list->count = 0;
  366. }
  367. #ifndef PARSB_NO_STDIO
  368. void parsb_add_blocks_from_file(parsb_context* context, const char* filename) {
  369. FILE* f = fopen(filename, "r");
  370. if (!f) {
  371. fprintf(stderr, "Unable to open %s\n", filename);
  372. return;
  373. }
  374. fseek(f, 0, SEEK_END);
  375. int length = ftell(f);
  376. fseek(f, 0, SEEK_SET);
  377. char* buffer = (char*) malloc(length);
  378. fread(buffer, 1, length, f);
  379. fclose(f);
  380. parsb_add_blocks(context, buffer, length);
  381. free(buffer);
  382. }
  383. static void writefn(const char* line, void* userdata) {
  384. fprintf((FILE*) userdata, "%s\n", line);
  385. }
  386. void parsb_write_blocks_to_file(parsb_context* context, const char* filename) {
  387. FILE* f = fopen(filename, "w");
  388. if (!f) {
  389. fprintf(stderr, "Unable to open %s\n", filename);
  390. return;
  391. }
  392. parsb_write_blocks(context, writefn, f);
  393. fclose(f);
  394. }
  395. #endif
  396. #endif // PAR_STRING_BLOCKS_IMPLEMENTATION
  397. #endif // PAR_STRING_BLOCKS_H
  398. // par_string_blocks is distributed under the MIT license:
  399. //
  400. // Copyright (c) 2020 Philip Rideout
  401. //
  402. // Permission is hereby granted, free of charge, to any person obtaining a copy
  403. // of this software and associated documentation files (the "Software"), to deal
  404. // in the Software without restriction, including without limitation the rights
  405. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  406. // copies of the Software, and to permit persons to whom the Software is
  407. // furnished to do so, subject to the following conditions:
  408. //
  409. // The above copyright notice and this permission notice shall be included in
  410. // all copies or substantial portions of the Software.
  411. //
  412. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  413. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  414. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  415. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  416. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  417. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  418. // SOFTWARE.