diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-02-13 00:07:28 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-02-13 00:07:28 -0500 |
commit | 350d21afffc4b77f65fea7409c4f00b56d4f97e4 (patch) | |
tree | c992c6e2c94a95a95908b41fbf1db78d0404e765 /src/template.c | |
parent | deaff24e42c8f3687ffe6bae549a04bc2f0a35aa (diff) | |
download | stasis-350d21afffc4b77f65fea7409c4f00b56d4f97e4.tar.gz |
Implement delivery_mission_render_files
Diffstat (limited to 'src/template.c')
-rw-r--r-- | src/template.c | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/src/template.c b/src/template.c index fe7ef11..646f5f9 100644 --- a/src/template.c +++ b/src/template.c @@ -56,13 +56,13 @@ char *tpl_getval(char *key) { } static int grow(size_t z, size_t *output_bytes, char **output) { - if (z > *output_bytes) { + if (z >= *output_bytes) { size_t new_size = *output_bytes + z + 1; #ifdef DEBUG fprintf(stderr, "template output buffer new size: %zu\n", *output_bytes); #endif char *tmp = realloc(*output, new_size); - if (!*tmp) { + if (!tmp) { perror("realloc failed"); return -1; } @@ -77,13 +77,13 @@ char *tpl_render(char *str) { if (!str) { return NULL; } - size_t output_bytes = strlen(str); + size_t output_bytes = strlen(str) * 2; char *output = NULL; char *b_close = NULL; char *pos = NULL; pos = str; - output = calloc(output_bytes + 1, sizeof(*output)); + output = calloc(output_bytes, sizeof(*output)); if (!output) { perror("unable to allocate output buffer"); return NULL; @@ -94,6 +94,7 @@ char *tpl_render(char *str) { char key[255] = {0}; char *value = NULL; + memset(key, 0, sizeof(key)); grow(z, &output_bytes, &output); // At opening brace if (!strncmp(pos, "{{", 2)) { @@ -105,6 +106,9 @@ char *tpl_render(char *str) { // Read key name size_t key_len = 0; while (isalnum(*pos) || *pos != '}') { + if (isspace(*pos) || isblank(*pos)) { + break; + } key[key_len] = *pos; key_len++; pos++; @@ -129,7 +133,7 @@ char *tpl_render(char *str) { continue; } // Jump past closing brace - pos = b_close + 2; + pos += (b_close + 2 - pos); if (do_env) { char *k = type_stop + 1; @@ -145,11 +149,11 @@ char *tpl_render(char *str) { } if (value) { - // Append replacement value - grow(z + strlen(value), &output_bytes, &output); - strcat(output, value); // Set output iterator to end of replacement value z += strlen(value); + // Append replacement value + grow(z, &output_bytes, &output); + strcat(output, value); } #ifdef DEBUG @@ -166,26 +170,26 @@ char *tpl_render(char *str) { return output; } -/* -const char *TESTDATA = "follow the {{ env:PATH }}\ni have a {{ COLOR}} pencil box\n\nthe cup is full of {{LIQUID }}\n{{GOAT}}, the barn is {{ COLOR }} too!\n"; -int main() { - char *name = "couch"; - char *liquid = "milk"; - char *color = "brown"; - char *meh = "MEHHHHHHHH"; - - tpl_register("THING", name); - tpl_register("LIQUID", liquid); - tpl_register("COLOR", color); - tpl_register("GOAT", meh); - - puts(TESTDATA); - puts("----------\n"); - - char *output = tpl_render(TESTDATA, strlen(TESTDATA) + 1024); - printf("%s\n", output); - free(output); - tpl_free(); +int tpl_render_to_file(char *str, const char *filename) { + char *result; + FILE *fp; + + // Render the input string + result = tpl_render(str); + if (!result) { + return -1; + } + + // Open the destination file for writing + fp = fopen(filename, "w+"); + if (!fp) { + return -1; + } + + // Write rendered string to file + fprintf(fp, "%s", result); + fclose(fp); + + guard_free(result); return 0; -} - */ +}
\ No newline at end of file |