summaryrefslogtreecommitdiff
path: root/src/write.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/write.c')
-rw-r--r--src/write.c104
1 files changed, 83 insertions, 21 deletions
diff --git a/src/write.c b/src/write.c
index 3cf04ca..1bd83da 100644
--- a/src/write.c
+++ b/src/write.c
@@ -19,23 +19,23 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
+#include <string.h>
+#include "decompress.h"
+#include "extract.h"
+#include "file.h"
#include "write.h"
-/* Document */
-char *pop_file_name(uint32_t key, node *root) {
- /* The root node shouldn't contain anything. */
- if (root->next == NULL)
- return NULL;
-
- const char *file_name = NULL;
- node *current = root->next, *last = root;
+/* Finds an eliF entry with the given key in a linked list and returns
+ the associated filename, removing it from the linked list. */
+char *pop_file_name(uint32_t key, elif_node *root) {
+ char *file_name = NULL;
+ elif_node *current = root->next, *last = root;
for (;;) {
if (current->key == key) {
file_name = current->file_name;
last->next = current->next;
- free(current->file_name);
free(current);
break;
} else if (current->next == NULL) {
@@ -49,9 +49,42 @@ char *pop_file_name(uint32_t key, node *root) {
}
-/* Document */
-void defer_node(node *new, node *root) {
- node *current = root;
+/* Iterates through the linked list of file entries and writes every
+ entry to disk, according to information specified by the node. */
+void write_files(file_node *file_root, elif_node *elif_root, Bytef *start) {
+ FILE *output;
+ file_node *current;
+ Bytef *compressed_buffer, *decompressed_buffer;
+ for (current = file_root->next; current != NULL; current = current->next) {
+ char *file_name = pop_file_name(current->key, elif_root);
+ if (file_name == NULL) {
+ fprintf(stderr, "File found without matching eliF entry.\n");
+ continue;
+ }
+ compressed_buffer = malloc(current->compressed_size);
+ for (int i = 0; i < current->segment_count; i++)
+ free(current->segments[i]);
+ free(current->segments);
+ /* memcpy(compressed_buffer, start + current->offset, */
+ /* current->decompressed_size); */
+ /* if (current->compressed) { */
+ /* fprintf(stderr, "Not implemented :^)\n"); // don't leave this in lol */
+ /* continue; */
+ /* } else { */
+ /* output = fopen(file_name, "wb+"); */
+ /* fwrite(compressed_buffer, current->compressed_size, 1, output); */
+ /* fclose(output); */
+ /* } */
+
+ free(compressed_buffer);
+ break;
+ }
+}
+
+
+/* Inserts an eliF entry at the end of a linked list. */
+void defer_elif_node(elif_node *new, elif_node *root) {
+ elif_node *current = root;
for (;;) {
if (current->next == NULL) {
current->next = new;
@@ -63,20 +96,49 @@ void defer_node(node *new, node *root) {
}
-void test_linked_list(node *root) {
- for (node *current = root; current != NULL; current = current->next) {
- printf("NODE\n----\n");
+/* Inserts a File entry node at the end of a linked list. */
+void defer_file_node(file_node *new, file_node *root) {
+ file_node *current = root;
+ for (;;) {
+ if (current->next == NULL) {
+ current->next = new;
+ break;
+ } else {
+ current = current->next;
+ }
+ }
+}
+
+
+/* Iterates through the linked list and frees all entries. */
+void free_elif_nodes(elif_node *base) {
+ if (base->next != NULL)
+ free_elif_nodes(base->next);
+ free(base->file_name);
+ free(base);
+}
+
+
+/* Iterates through the linked list and frees all entries. */
+void free_file_nodes(file_node *base) {
+ if (base->next != NULL)
+ free_file_nodes(base->next);
+ free(base);
+}
+
+
+void test_elif_linked_list(elif_node *root) {
+ for (elif_node *current = root; current != NULL; current = current->next) {
+ printf("ELIF NODE\n---------\n");
printf("KEY: %" PRIx32 "\n", current->key);
printf("FILE_NAME: %s\n\n", current->file_name);
}
}
-/* Document */
-void free_node(node *base) {
- if (base->next != NULL) {
- free_node(base->next);
- return;
+void test_file_linked_list(file_node *root) {
+ for (file_node *current = root; current != NULL; current = current->next) {
+ printf("FILE NODE\n---------\n");
+ printf("KEY: %" PRIx32 "\n\n", current->key);
}
- free(base);
}