]> git.seodisparate.com - SimpleArchiver/commitdiff
Refactoring
authorStephen Seo <seo.disparate@gmail.com>
Thu, 6 Feb 2025 05:03:23 +0000 (14:03 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 6 Feb 2025 05:03:23 +0000 (14:03 +0900)
Formalize "string_parts" into a helper struct/fns for use in archiver.c.

src/archiver.c
src/helpers.c
src/helpers.h

index 04e0815e7080797cb3cb35e4f376db7658e91e60..05ec94efcee44405c1e133a0d5012ff30ccff4e7 100644 (file)
@@ -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(
index be9bfa804fe545f89cd74aef06972f78911f73c2..0b41a56a8fdb28324047919440bf6ee14dc0af6a 100644 (file)
@@ -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;
+}
index 3e757c8003e4c2d07ef08c2a13de30846aea685b..f1bd808aa0ae65d35cf4036d4bd19a2f96f8227b 100644 (file)
@@ -23,6 +23,9 @@
 #include <stdint.h>
 #include <stdio.h>
 
+// 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