diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2025-06-17 15:58:46 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2025-06-18 16:04:11 -0400 |
commit | b00eab814c978f977845743694100a919cb5d684 (patch) | |
tree | 90d7db4930bf8f9766cc91c6e33128af17f5c754 | |
parent | 151b32e7715b8f96739b7732df58b622c6ee52ae (diff) | |
download | stasis-b00eab814c978f977845743694100a919cb5d684.tar.gz |
Move grow() function to utils.c
-rw-r--r-- | src/lib/core/include/utils.h | 37 | ||||
-rw-r--r-- | src/lib/core/template.c | 17 | ||||
-rw-r--r-- | src/lib/core/utils.c | 31 |
3 files changed, 64 insertions, 21 deletions
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index b405b2a..583a8b8 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -419,4 +419,41 @@ int gen_file_extension_str(char *filename, const char *extension); char *remove_extras(char *s); void debug_hexdump(char *data, int len); + +/** + * Realloc helper + * + * @code{.c} + * #include <stdio.h> + * #include <stdlib.h> + * #include <string.h> + * #include "utils.h" + * + * int main(int argc, char *argv[]) { + * size_t sz = 10; + * char *data = calloc(sz, sizeof(*data)); + * + * // populate data + * strncat(data, "/path/to/", sz - 1); + * + * // Double the allocation size for data + * if (grow(sz * 2, &sz, &data)) { + * // memory error + * } + * + * // sz is now 20 + * strncat(data, "filename", sz - 1 - strlen(data)); + * + * puts(data); + * // output: "/path/to/filename" + * } + * @endcode + * + * @param size_new increase by `size_new` bytes + * @param size_orig address of variable containing the original allocation size (modified) + * @param data address to write data + * @return 0 on success + * @return -1 on error + */ +int grow(size_t size_new, size_t *size_orig, char **data); #endif //STASIS_UTILS_H diff --git a/src/lib/core/template.c b/src/lib/core/template.c index ba45a5a..68d20c9 100644 --- a/src/lib/core/template.c +++ b/src/lib/core/template.c @@ -137,23 +137,6 @@ struct tplfunc_frame *tpl_getfunc(char *key) { return result; } -static int grow(size_t z, size_t *output_bytes, char **output) { - if (z >= *output_bytes) { - size_t new_size = *output_bytes + z + 1; - SYSDEBUG("template output buffer new size: %zu\n", new_size); - - char *tmp = realloc(*output, new_size); - if (!tmp) { - perror("realloc failed"); - return -1; - } else if (tmp != *output) { - *output = tmp; - } - *output_bytes = new_size; - } - return 0; -} - char *tpl_render(char *str) { if (!str) { return NULL; diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index a9c9ea5..89d0baa 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -892,10 +892,11 @@ int gen_file_extension_str(char *filename, const char *extension) { return replace_text(ext_orig, ext_orig, extension, 0); } +#define DEBUG_HEXDUMP_FMT_BYTES 6 #define DEBUG_HEXDUMP_ADDR_MAXLEN 20 #define DEBUG_HEXDUMP_BYTES_MAXLEN (16 * 3 + 2) #define DEBUG_HEXDUMP_ASCII_MAXLEN (16 + 1) -#define DEBUG_HEXDUMP_OUTPUT_MAXLEN (DEBUG_HEXDUMP_ADDR_MAXLEN + DEBUG_HEXDUMP_BYTES_MAXLEN + DEBUG_HEXDUMP_ASCII_MAXLEN + 1) +#define DEBUG_HEXDUMP_OUTPUT_MAXLEN (DEBUG_HEXDUMP_FMT_BYTES + DEBUG_HEXDUMP_ADDR_MAXLEN + DEBUG_HEXDUMP_BYTES_MAXLEN + DEBUG_HEXDUMP_ASCII_MAXLEN + 1) void debug_hexdump(char *data, int len) { int count = 0; @@ -936,10 +937,32 @@ void debug_hexdump(char *data, int len) { // Add group padding strcat(bytes, " "); } - int padding = 16 - count; + const int padding = 16 - count; for (int i = 0; i < padding; i++) { strcat(bytes, " "); } - sprintf(output, "%s | %s | %s", addr, bytes, ascii); + snprintf(output, 6 + sizeof(addr) + sizeof(bytes) + sizeof(ascii), "%s | %s | %s", addr, bytes, ascii); puts(output); -}
\ No newline at end of file +} + +int grow(const size_t size_new, size_t *size_orig, char **data) { + if (!*data) { + return 0; + } + if (size_new >= *size_orig) { + const size_t new_size = *size_orig + size_new + 1; + SYSDEBUG("template data buffer new size: %zu\n", new_size); + + char *tmp = realloc(*data, new_size); + if (!tmp) { + perror("realloc failed"); + return -1; + } + if (tmp != *data) { + *data = tmp; + } + *size_orig = new_size; + } + return 0; +} + |