back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/data_struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'data_struct.c')
-rw-r--r--data_struct.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/data_struct.c b/data_struct.c
new file mode 100644
index 0000000..bd551ab
--- /dev/null
+++ b/data_struct.c
@@ -0,0 +1,153 @@
+#include "data_struct.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+void add_new_item(struct file_item **list, struct file_item **entry,
+ const char *file_name)
+{
+ *entry = malloc(sizeof(struct file_item));
+ if(!*entry) {
+ perror("malloc");
+ exit(1);
+ }
+ (*entry)->filename = malloc(strlen(file_name)+1);
+ if(!(*entry)->filename) {
+ perror("malloc");
+ exit(1);
+ }
+ /* safety */
+ strcpy((*entry)->filename, file_name);
+ (*entry)->next = *list;
+ (*entry)->info_line = NULL;
+ *list = *entry;
+}
+/*
+ * Initialize arg_arrays: allocate memory for directories and files
+ * arrays and zero them
+ */
+void init_arg_arrays(struct arg_arrays *split, int argc)
+{
+ split->dirs = malloc(argc * sizeof(char*));
+ if(!split->dirs) {
+ perror("malloc");
+ exit(1);
+ }
+ split->files = malloc(argc * sizeof(char*));
+ if(!split->files) {
+ perror("malloc");
+ exit(1);
+ }
+
+ memset(split->dirs, 0, argc * sizeof(char*));
+ memset(split->files, 0, argc * sizeof(char*));
+}
+
+void init_item_array(struct file_item_array *arr)
+{
+ arr->array = NULL;
+ arr->length = 0;
+}
+
+static size_t get_list_length(struct file_item *list)
+{
+ size_t counter = 0;
+
+ while(list) {
+ ++counter;
+ list = list->next;
+ }
+ return counter;
+}
+
+void copy_list_to_array(struct file_item *list, struct file_item_array *arr)
+{
+ size_t list_length = get_list_length(list);
+ size_t i;
+
+ arr->array = malloc(list_length * sizeof(struct file_item*));
+ if(!arr->array) {
+ perror("malloc");
+ exit(1);
+ }
+ arr->length = list_length;
+
+ for(i = 0; i < list_length; list = list->next, ++i)
+ arr->array[i] = list;
+}
+
+void free_file_item_array(struct file_item_array *arr, struct file_item **list)
+{
+ size_t i;
+
+ for(i = 0; i < arr->length; ++i) {
+ free(arr->array[i]->filename);
+ free(arr->array[i]->info_line);
+ free(arr->array[i]);
+ }
+ if(arr->array)
+ free(arr->array);
+ arr->array = NULL;
+ arr->length = 0;
+ *list = NULL;
+}
+
+void free_arg_arrays(struct arg_arrays *split)
+{
+ free(split->dirs);
+ free(split->files);
+}
+
+int is_empty_array(const char **paths_array, int argc)
+{
+ int empty_flag = 1;
+ size_t i;
+
+ for(i = 0; i < argc; ++i)
+ if(paths_array[i] != 0) {
+ empty_flag = 0;
+ break;
+ }
+ return empty_flag;
+}
+
+int is_last_arr_element(const char **paths_array, int argc, size_t pos)
+{
+ int last_flag = 1;
+
+ for(++pos ; pos < argc; ++pos)
+ if(paths_array[pos]) {
+ last_flag = 0;
+ break;
+ }
+ return last_flag;
+}
+
+static void swap(struct file_item **prev_element,
+ struct file_item **next_element)
+{
+ struct file_item *tmp_element = NULL;
+
+ if(prev_element != next_element)
+ if(strcmp((*prev_element)->filename, (*next_element)->filename) > 0) {
+ tmp_element = *prev_element;
+ *prev_element = *next_element;
+ *next_element = tmp_element;
+ }
+}
+
+/*
+ * bubble sort
+ */
+void sort_array_by_names(struct file_item_array *arr)
+{
+ size_t i, j;
+
+ if(arr->length == 1)
+ return;
+
+ for(i = arr->length-1; i > 0; --i)
+ for(j = 1; j <= i; ++j)
+ swap(arr->array+j-1, arr->array+j);
+}