par_string_blocks.h 15 KB

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