th/backend.c
2025-03-03 15:44:38 +01:00

113 lines
3.5 KiB
C

#include <curses.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include "defines.h"
extern unsigned int settings;
extern unsigned int file_modifiers;
extern char **content_l;
extern char **content_m;
extern char **content_r;
extern char *path;
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
DIR *dir = opendir(path);
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
unsigned long index = 1; //always makes the array at least 1 big, used for metadata like the amount of files
if (dir) {
struct dirent *entry;
while ( (entry=readdir(dir)) ) {
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
index++;
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES){
index++;
}
}
}
*file_count = index;
closedir(dir);
}
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char **dir_content){
DIR *dir = opendir(path);
char content[file_count][longest_name];
memset(content,0,sizeof(content));
if (dir) {
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] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
strcpy(dir_content[index], entry->d_name);
index++;
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
strcpy(dir_content[index], entry->d_name);
index++;
}
}
}
closedir(dir);
}
void print_dir(WINDOW *win, char **dir_content){
for (unsigned long i = 1; i < *dir_content[0]; i++ ){ //skip index 0 as it is used for metadata like file count
wprintw(win, "%s",dir_content[i]);
wmove(win, i, 1);
}
}
// 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;
unsigned long longest_name = 0;
if (wh) {
if (wh == 1) {
free(content_m);
get_dir_size(path, &file_count, &longest_name);
content_m = calloc(file_count+1, sizeof(*content_m)); //index 0 is used for metadata like file count in dir
for (unsigned long i = 0; i<file_count+1; i++) {
content_m[i] = calloc(longest_name, sizeof(content_m[i]));
}
*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);
content_r = calloc(file_count+1, sizeof(*content_r));
for (unsigned long i = 0; i<file_count+1; i++) {
content_r[i] = calloc(longest_name, sizeof(content_r[i]));
}
*content_r[0] = file_count;
get_dir_content(path, file_count, longest_name, content_r);
}
} else {
free(content_l);
get_dir_size(path, &file_count, &longest_name);
content_l = calloc(file_count+1, sizeof(*content_l));
for (unsigned long i = 0; i<file_count+1; i++) {
content_l[i] = calloc(longest_name, sizeof(content_l));
}
*content_l[0] = file_count;
get_dir_content(path, file_count, longest_name, content_l);
}
return NULL;
}