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(
}
// 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(
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;
+}