From 4bdda8c1eff783577d3a4228ea80a665a843fa5a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 6 Feb 2025 14:03:23 +0900 Subject: [PATCH] Refactoring Formalize "string_parts" into a helper struct/fns for use in archiver.c. --- src/archiver.c | 140 ++++++++++++------------------------------------- src/helpers.c | 62 ++++++++++++++++++++++ src/helpers.h | 23 ++++++++ 3 files changed, 118 insertions(+), 107 deletions(-) diff --git a/src/archiver.c b/src/archiver.c index 04e0815..05ec94e 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -8121,70 +8121,32 @@ int simple_archiver_parse_archive_version_2(FILE *in_f, int_fast8_t do_extract, return SDAS_INTERNAL_ERROR; } - // TODO separate out string_parts stuff into a helper function(s). - __attribute__((cleanup(simple_archiver_list_free))) - SDArchiverLinkedList *string_parts = simple_archiver_list_init(); + __attribute__((cleanup(simple_archiver_helper_string_parts_free))) + SAHelperStringParts string_parts = + simple_archiver_helper_string_parts_init(); + + simple_archiver_helper_string_parts_add(string_parts, abs_path_dir); - simple_archiver_list_add(string_parts, strdup(abs_path_dir), NULL); if (abs_path_dir[strlen(abs_path_dir) - 1] != '/') { - simple_archiver_list_add( - string_parts, - "/", - simple_archiver_helper_datastructure_cleanup_nop); + simple_archiver_helper_string_parts_add(string_parts, "/"); } + if (state && state->parsed->prefix) { - simple_archiver_list_add(string_parts, - strdup(state->parsed->prefix), - NULL); + simple_archiver_helper_string_parts_add(string_parts, + state->parsed->prefix); } - simple_archiver_list_add(string_parts, strdup(buf), NULL); - size_t size = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - size += strlen(node->data); - } - ++size; + simple_archiver_helper_string_parts_add(string_parts, buf); __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) - char *abs_dir_path = malloc(size); - size_t idx = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - const size_t part_len = strlen(node->data); - memcpy(abs_dir_path + idx, node->data, part_len); - idx += part_len; - } - abs_dir_path[size - 1] = 0; - - simple_archiver_list_add( - string_parts, - "/UNUSED", - simple_archiver_helper_datastructure_cleanup_nop); - - size = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - size += strlen(node->data); - } - ++size; + char *abs_dir_path = + simple_archiver_helper_string_parts_combine(string_parts); + + simple_archiver_helper_string_parts_add(string_parts, "/UNUSED"); __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) - char *abs_dir_path_with_suffix = malloc(size); - idx = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - const size_t part_len = strlen(node->data); - memcpy(abs_dir_path_with_suffix + idx, node->data, part_len); - idx += part_len; - } - abs_dir_path_with_suffix[size - 1] = 0; - //fprintf(stderr, "DEBUG: abs_dir_path_with_suffix is \"%s\"!\n", - // abs_dir_path_with_suffix); + char *abs_dir_path_with_suffix = + simple_archiver_helper_string_parts_combine(string_parts); if (do_extract) { int ret = simple_archiver_helper_make_dirs_perms( @@ -9845,68 +9807,32 @@ int simple_archiver_parse_archive_version_3(FILE *in_f, } // TODO separate out string_parts stuff into a helper function(s). - __attribute__((cleanup(simple_archiver_list_free))) - SDArchiverLinkedList *string_parts = simple_archiver_list_init(); + __attribute__((cleanup(simple_archiver_helper_string_parts_free))) + SAHelperStringParts string_parts = + simple_archiver_helper_string_parts_init(); + + simple_archiver_helper_string_parts_add(string_parts, abs_path_dir); - simple_archiver_list_add(string_parts, strdup(abs_path_dir), NULL); if (abs_path_dir[strlen(abs_path_dir) - 1] != '/') { - simple_archiver_list_add( - string_parts, - "/", - simple_archiver_helper_datastructure_cleanup_nop); + simple_archiver_helper_string_parts_add(string_parts, "/"); } + if (state && state->parsed->prefix) { - simple_archiver_list_add(string_parts, - strdup(state->parsed->prefix), - NULL); + simple_archiver_helper_string_parts_add(string_parts, + state->parsed->prefix); } - simple_archiver_list_add(string_parts, strdup((char *)buf), NULL); - size_t size = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - size += strlen(node->data); - } - ++size; + simple_archiver_helper_string_parts_add(string_parts, (char *)buf); + __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) - char *abs_dir_path = malloc(size); - size_t idx = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - const size_t part_len = strlen(node->data); - memcpy(abs_dir_path + idx, node->data, part_len); - idx += part_len; - } - abs_dir_path[size - 1] = 0; - - simple_archiver_list_add( - string_parts, - "/UNUSED", - simple_archiver_helper_datastructure_cleanup_nop); - - size = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - size += strlen(node->data); - } - ++size; + char *abs_dir_path = + simple_archiver_helper_string_parts_combine(string_parts); + + simple_archiver_helper_string_parts_add(string_parts, "/UNUSED"); __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) - char *abs_dir_path_with_suffix = malloc(size); - idx = 0; - for (SDArchiverLLNode *node = string_parts->head->next; - node->data != NULL; - node = node->next) { - const size_t part_len = strlen(node->data); - memcpy(abs_dir_path_with_suffix + idx, node->data, part_len); - idx += part_len; - } - abs_dir_path_with_suffix[size - 1] = 0; - //fprintf(stderr, "DEBUG: abs_dir_path_with_suffix is \"%s\"!\n", - // abs_dir_path_with_suffix); + char *abs_dir_path_with_suffix = + simple_archiver_helper_string_parts_combine(string_parts); if (do_extract) { int ret = simple_archiver_helper_make_dirs_perms( diff --git a/src/helpers.c b/src/helpers.c index be9bfa8..0b41a56 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -549,3 +549,65 @@ char *simple_archiver_helper_real_path_to_name(const char *filename) { return result; } } + +SAHelperStringParts simple_archiver_helper_string_parts_init(void) { + SAHelperStringParts parts; + parts.parts = simple_archiver_list_init(); + return parts; +} + +void simple_archiver_helper_string_parts_free(SAHelperStringParts *string_parts) +{ + if (string_parts && string_parts->parts) { + simple_archiver_list_free(&string_parts->parts); + } +} + +void simple_archiver_helper_string_part_free(void *v_part) { + SAHelperStringPart *part = v_part; + if (part) { + if (part->buf) { + free(part->buf); + } + free(part); + } +} + +void simple_archiver_helper_string_parts_add(SAHelperStringParts string_parts, + const char *c_string) { + if (!c_string || c_string[0] == 0) { + return; + } + SAHelperStringPart *part = malloc(sizeof(SAHelperStringPart)); + part->buf = strdup(c_string); + part->size = strlen(part->buf); + simple_archiver_list_add(string_parts.parts, + part, + simple_archiver_helper_string_part_free); +} + +char *simple_archiver_helper_string_parts_combine( + SAHelperStringParts string_parts) { + size_t size = 0; + for (SDArchiverLLNode *node = string_parts.parts->head->next; + node->next != NULL; + node = node->next) { + if (node->data) { + SAHelperStringPart *part = node->data; + size += part->size; + } + } + char *buf = malloc(size + 1); + size_t idx = 0; + for (SDArchiverLLNode *node = string_parts.parts->head->next; + node->next != NULL; + node = node->next) { + if (node->data) { + SAHelperStringPart *part = node->data; + memcpy(buf + idx, part->buf, part->size); + idx += part->size; + } + } + buf[size] = 0; + return buf; +} diff --git a/src/helpers.h b/src/helpers.h index 3e757c8..f1bd808 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -23,6 +23,9 @@ #include #include +// Local includes. +#include "data_structures/linked_list.h" + static const uint32_t MAX_SYMBOLIC_LINK_SIZE = 512; /// Returns non-zero if this system is big-endian. @@ -96,4 +99,24 @@ void simple_archiver_helper_cleanup_uint32(uint32_t **uint); void simple_archiver_helper_datastructure_cleanup_nop(void *unused); +typedef struct SAHelperStringParts { + SDArchiverLinkedList *parts; +} SAHelperStringParts; + +typedef struct SAHelperStringPart { + char *buf; + size_t size; +} SAHelperStringPart; + +// Must be free'd by `simple_archiver_helper_string_parts_free`. +SAHelperStringParts simple_archiver_helper_string_parts_init(void); +void simple_archiver_helper_string_parts_free( + SAHelperStringParts *string_parts); +// Makes a copy of the given c-string. +void simple_archiver_helper_string_parts_add(SAHelperStringParts string_parts, + const char *c_string); +// Returned c-string must be free'd. +char *simple_archiver_helper_string_parts_combine( + SAHelperStringParts string_parts); + #endif -- 2.49.0