diff --git a/backend.c b/backend.c index 949fed5..9ebd66b 100644 --- a/backend.c +++ b/backend.c @@ -1,11 +1,13 @@ #include #include +#include #include #include #include #include "defines.h" extern unsigned int settings; +extern unsigned int file_modifiers; extern char **content_l; extern char **content_m; extern char **content_r; @@ -18,9 +20,9 @@ void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_ if (dir) { struct dirent *entry; while ( (entry=readdir(dir)) ) { - if (entry->d_name[0] != '.' && !(settings & SETTINGS_HIDDEN_FILES)) { + if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) { index++; - } else if (settings & SETTINGS_HIDDEN_FILES){ + } else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES){ index++; } } @@ -37,10 +39,10 @@ void get_dir_content(char *path, unsigned long file_count, unsigned long longest int index = 1; //skip index 0 as it is used for metadata like file count struct dirent *entry; while ( (entry=readdir(dir)) ) { - if (entry->d_name[0] != '.' && !(settings & SETTINGS_HIDDEN_FILES)) { + if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) { strcpy(dir_content[index], entry->d_name); index++; - } else if (settings & SETTINGS_HIDDEN_FILES) { + } else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { strcpy(dir_content[index], entry->d_name); index++; } @@ -56,6 +58,18 @@ void print_dir(WINDOW *win, char **dir_content){ } } +// Comparison function to sort strings in ascending order +int compare(const void *a, const void *b) { + return strcasecmp(a, b); +} +void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **dir_content){ + char content[*file_count][*longest_name]; + memset(content,0,sizeof(content)); + if ((file_modifiers & FILE_MODIFIERS_SORT_BITMASK) == 0) {//natural; first dirs, then files + qsort(dir_content, *file_count, *longest_name, compare); + } + +} void *populate_dir(void *which){ // 0=left, 1=main, 2=right char wh = (char)which; unsigned long file_count = 0; @@ -71,6 +85,7 @@ void *populate_dir(void *which){ // 0=left, 1=main, 2=right } *content_m[0] = file_count; get_dir_content(path, file_count, longest_name, content_m); + sort_dir(&file_count, &longest_name, content_m); } else { free(content_r); get_dir_size(path, &file_count, &longest_name); @@ -94,3 +109,4 @@ void *populate_dir(void *which){ // 0=left, 1=main, 2=right return NULL; } + diff --git a/backend.h b/backend.h index 94b23c6..7ab3b66 100644 --- a/backend.h +++ b/backend.h @@ -5,4 +5,5 @@ void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name); void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char **dir_content); void print_dir(WINDOW *win, char **dir_content); +void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **dir_content); void *populate_dir(void *dir); diff --git a/defines.h b/defines.h new file mode 100644 index 0000000..4e4234c --- /dev/null +++ b/defines.h @@ -0,0 +1,5 @@ +#define STATUS_QUIT_PROGRAM 1 +#define STATUS_RUN_BACKEND 2 + +#define FILE_MODIFIERS_HIDDEN_FILES 1 +#define FILE_MODIFIERS_SORT_BITMASK 126 // 00000000000000000000000001111110 diff --git a/interactions.c b/interactions.c index 1a0a9b1..19a679b 100644 --- a/interactions.c +++ b/interactions.c @@ -3,13 +3,15 @@ #include #include "defines.h" -void user_interactions(char *input, unsigned int *status, unsigned int *settings) { + + +void user_interactions(char *input, unsigned int *status, unsigned int *settings, unsigned int *file_modifiers) { if (*input == 'q') { *status ^= STATUS_QUIT_PROGRAM; } else if (*input == *"KEY_BACKSPACE") { - *settings ^= SETTINGS_HIDDEN_FILES; + *file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES; } else if (*input == 'a') { - *settings ^= SETTINGS_HIDDEN_FILES; + *file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES; } else { } diff --git a/th b/th deleted file mode 100755 index 71ce2ca..0000000 Binary files a/th and /dev/null differ