par_string_blocks.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include <assert.h>
  108. #include <ctype.h>
  109. #include <stdlib.h>
  110. #include <string.h>
  111. #ifndef PARSB_NO_STDIO
  112. #include <stdio.h>
  113. #endif
  114. #define PARSB_MIN(a, b) (a > b ? b : a)
  115. typedef struct {
  116. int count;
  117. char* values[PARSB_MAX_NUM_BLOCKS];
  118. char* names[PARSB_MAX_NUM_BLOCKS];
  119. } parsb__list;
  120. struct parsb_context_s {
  121. parsb_options options;
  122. parsb__list blocks;
  123. parsb__list results;
  124. };
  125. static char* parsb__add_or_replace(parsb_context*, const char* id, const char* value,
  126. int value_size, int line_number);
  127. static char* parsb__list_add(parsb__list*, const char* id, const char* value, int value_size,
  128. int line_number);
  129. static char* parsb__list_get(parsb__list*, const char* id, int idlen);
  130. static void parsb__list_free(parsb__list* );
  131. parsb_context* parsb_create_context(parsb_options options) {
  132. parsb_context* context = (parsb_context*) calloc(1, sizeof(parsb_context));
  133. context->options = options;
  134. return context;
  135. }
  136. void parsb_destroy_context(parsb_context* context) {
  137. parsb__list_free(&context->blocks);
  138. parsb__list_free(&context->results);
  139. free(context);
  140. }
  141. void parsb_add_blocks(parsb_context* context, const char* blob, int buffer_size) {
  142. const char* previous_block = 0;
  143. char previous_name[PARSB_MAX_NAME_LENGTH];
  144. int line_number = 0;
  145. int block_line_number = 0;
  146. for (int i = 0; i < buffer_size - 4; i++) {
  147. if (blob[i] != '-' || blob[i + 1] != '-' || blob[i + 2] != '-' || blob[i + 3] != ' ') {
  148. if (blob[i] == '\n') {
  149. line_number++;
  150. }
  151. continue;
  152. }
  153. if (previous_block) {
  154. parsb__add_or_replace(context, previous_name, previous_block,
  155. i - (previous_block - blob), block_line_number);
  156. }
  157. i += 4;
  158. const char* name = blob + i;
  159. const char* block_start = 0;
  160. for (; i < buffer_size; i++) {
  161. if (blob[i] == '\n') {
  162. line_number++;
  163. int name_length = i - (name - blob);
  164. memcpy(previous_name, name, name_length);
  165. block_line_number = line_number + 2;
  166. previous_name[name_length] = 0;
  167. block_start = blob + i + 1;
  168. break;
  169. }
  170. if (isspace(blob[i])) {
  171. int name_length = i - (name - blob);
  172. memcpy(previous_name, name, name_length);
  173. block_line_number = line_number + 2;
  174. previous_name[name_length] = 0;
  175. for (i++; i < buffer_size; i++) {
  176. if (blob[i] == '\n') {
  177. line_number++;
  178. block_start = blob + i + 1;
  179. break;
  180. }
  181. }
  182. break;
  183. }
  184. }
  185. if (block_start == 0) {
  186. return;
  187. }
  188. previous_block = block_start;
  189. }
  190. if (previous_block) {
  191. parsb__add_or_replace(context, previous_name, previous_block,
  192. buffer_size - (previous_block - blob), block_line_number);
  193. }
  194. }
  195. void parsb_add_block(parsb_context* context, const char* name, const char* body) {
  196. char* dup = strdup(body);
  197. parsb__add_or_replace(context, name, dup, 1 + strlen(body), 0);
  198. }
  199. const char* parsb_get_blocks(parsb_context* context, const char* block_names) {
  200. int len = strlen(block_names);
  201. const char* name = block_names;
  202. int name_length = 0;
  203. int result_length = 0;
  204. // First pass determines the amount of required memory.
  205. int num_names = 0;
  206. for (int i = 0; i < len; i++) {
  207. char c = block_names[i];
  208. if (isspace(c) || !c) {
  209. const char* block = parsb__list_get(&context->blocks, name, name_length);
  210. if (block) {
  211. result_length += strlen(block);
  212. num_names++;
  213. } else {
  214. return NULL;
  215. }
  216. name_length = 0;
  217. name = block_names + i + 1;
  218. } else {
  219. name_length++;
  220. }
  221. }
  222. const char* block = parsb__list_get(&context->blocks, name, name_length);
  223. if (block) {
  224. result_length += strlen(block);
  225. num_names++;
  226. }
  227. // If no concatenation is required, return early.
  228. if (num_names == 1) {
  229. return parsb__list_get(&context->blocks, name, name_length);
  230. }
  231. // Allocate storage for the result.
  232. char* result = parsb__list_add(&context->results, 0, 0, result_length, 0);
  233. char* cursor = result;
  234. // Second pass populates the result.
  235. name = block_names;
  236. name_length = 0;
  237. for (int i = 0; i < len; i++) {
  238. char c = block_names[i];
  239. if (isspace(c) || !c) {
  240. const char* block = parsb__list_get(&context->blocks, name, name_length);
  241. if (block) {
  242. memcpy(cursor, block, strlen(block));
  243. cursor += strlen(block);
  244. }
  245. name_length = 0;
  246. name = block_names + i + 1;
  247. } else {
  248. name_length++;
  249. }
  250. }
  251. block = parsb__list_get(&context->blocks, name, name_length);
  252. if (block) {
  253. memcpy(cursor, block, strlen(block));
  254. cursor += strlen(block);
  255. }
  256. return result;
  257. }
  258. int parsb_get_num_blocks(const parsb_context* context) {
  259. return context->blocks.count;
  260. }
  261. void parsb_get_block(const parsb_context* context, int index, const char** name,
  262. const char** body) {
  263. if (index < 0 || index >= context->blocks.count) {
  264. return;
  265. }
  266. *name = context->blocks.names[index];
  267. *body = context->blocks.values[index];
  268. }
  269. void parsb_write_blocks(parsb_context* context, parsb_write_line writefn, void* userdata) {
  270. char line[PARSB_MAX_LINE_LENGTH + 1] = {0};
  271. for (int i = 0; i < context->blocks.count; i++) {
  272. sprintf(line, "--- %s", context->blocks.names[i]);
  273. writefn(line, userdata);
  274. const char* cursor = context->blocks.values[i];
  275. const int blocklen = strlen(cursor);
  276. int previous = 0;
  277. for (int i = 0; i < blocklen; i++) {
  278. if (cursor[i] == '\n') {
  279. int line_length = PARSB_MIN(i - previous, PARSB_MAX_LINE_LENGTH);
  280. memcpy(line, cursor + previous, line_length);
  281. line[line_length] = 0;
  282. writefn(line, userdata);
  283. previous = i + 1;
  284. } else if (i == blocklen - 1) {
  285. int line_length = PARSB_MIN(1 + i - previous, PARSB_MAX_LINE_LENGTH);
  286. memcpy(line, cursor + previous, line_length);
  287. line[line_length] = 0;
  288. writefn(line, userdata);
  289. previous = i + 1;
  290. }
  291. }
  292. }
  293. }
  294. static char* parsb__add_or_replace(parsb_context* context, const char* id, const char* value,
  295. int value_size, int line_number) {
  296. line_number = context->options.line_directives ? line_number : 0;
  297. const size_t idlen = strlen(id);
  298. for (int i = 0; i < context->blocks.count; i++) {
  299. if (strncmp(id, context->blocks.names[i], idlen) == 0) {
  300. free(context->blocks.values[i]);
  301. context->blocks.values[i] = strndup(value, value_size);
  302. return context->blocks.values[i];
  303. }
  304. }
  305. return parsb__list_add(&context->blocks, id, value, value_size, line_number);
  306. }
  307. static char* parsb__list_add(parsb__list* list, const char* name,
  308. const char* value, int value_size, int line_number) {
  309. if (value_size == 0) {
  310. return NULL;
  311. }
  312. if (list->count == PARSB_MAX_NUM_BLOCKS) {
  313. assert(false && "Please increase PARSB_MAX_NUM_BLOCKS.");
  314. return NULL;
  315. }
  316. char* storage;
  317. char* cursor;
  318. if (line_number > 0) {
  319. char line_directive[16] = {0};
  320. int prefix_length = snprintf(line_directive, 16, "\n#line %d\n", line_number);
  321. storage = (char*) calloc(1, prefix_length + value_size + 1);
  322. memcpy(storage, line_directive, prefix_length);
  323. cursor = storage + prefix_length;
  324. } else {
  325. storage = cursor = (char*) calloc(1, value_size + 1);
  326. }
  327. if (value) {
  328. memcpy(cursor, value, value_size);
  329. }
  330. #if PARSB_ENABLE_TRIM
  331. value_size--;
  332. while (isspace(cursor[value_size])) {
  333. cursor[value_size] = 0;
  334. value_size--;
  335. if (value_size == 0) {
  336. break;
  337. }
  338. }
  339. #endif
  340. if (name) {
  341. list->names[list->count] = strdup(name);
  342. } else {
  343. list->names[list->count] = 0;
  344. }
  345. list->values[list->count] = storage;
  346. list->count++;
  347. return storage;
  348. }
  349. static char* parsb__list_get(parsb__list* list, const char* name, int idlen) {
  350. for (int i = 0; i < list->count; i++) {
  351. if (strncmp(name, list->names[i], idlen) == 0) {
  352. return list->values[i];
  353. }
  354. }
  355. return NULL;
  356. }
  357. static void parsb__list_free(parsb__list* list) {
  358. for (int i = 0; i < list->count; i++) {
  359. free(list->names[i]);
  360. free(list->values[i]);
  361. }
  362. list->count = 0;
  363. }
  364. #ifndef PARSB_NO_STDIO
  365. void parsb_add_blocks_from_file(parsb_context* context, const char* filename) {
  366. FILE* f = fopen(filename, "r");
  367. if (!f) {
  368. fprintf(stderr, "Unable to open %s\n", filename);
  369. return;
  370. }
  371. fseek(f, 0, SEEK_END);
  372. int length = ftell(f);
  373. fseek(f, 0, SEEK_SET);
  374. char* buffer = (char*) malloc(length);
  375. fread(buffer, 1, length, f);
  376. fclose(f);
  377. parsb_add_blocks(context, buffer, length);
  378. free(buffer);
  379. }
  380. static void writefn(const char* line, void* userdata) {
  381. fprintf((FILE*) userdata, "%s\n", line);
  382. }
  383. void parsb_write_blocks_to_file(parsb_context* context, const char* filename) {
  384. FILE* f = fopen(filename, "w");
  385. if (!f) {
  386. fprintf(stderr, "Unable to open %s\n", filename);
  387. return;
  388. }
  389. parsb_write_blocks(context, writefn, f);
  390. fclose(f);
  391. }
  392. #endif
  393. #endif // PAR_STRING_BLOCKS_IMPLEMENTATION
  394. #endif // PAR_STRING_BLOCKS_H
  395. // par_string_blocks is distributed under the MIT license:
  396. //
  397. // Copyright (c) 2020 Philip Rideout
  398. //
  399. // Permission is hereby granted, free of charge, to any person obtaining a copy
  400. // of this software and associated documentation files (the "Software"), to deal
  401. // in the Software without restriction, including without limitation the rights
  402. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  403. // copies of the Software, and to permit persons to whom the Software is
  404. // furnished to do so, subject to the following conditions:
  405. //
  406. // The above copyright notice and this permission notice shall be included in
  407. // all copies or substantial portions of the Software.
  408. //
  409. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  410. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  411. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  412. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  413. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  414. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  415. // SOFTWARE.