th/backend.c

95 lines
2.6 KiB
C

#include <curses.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
extern unsigned int settings;
extern unsigned long longest_name;
extern unsigned long file_count;
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);
if (dir) {
unsigned long index = 0;
struct dirent *entry;
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !(settings & 1)) { //bit 0 = show_hidden_files
index++;
if ((unsigned long)sizeof(entry->d_name) > *longest_name) {
*longest_name = sizeof(entry->d_name);
}
} else if (settings & 1){ //bit 0 = show_hidden_files
index++;
if ((unsigned long*)sizeof(entry->d_name) > (unsigned long*)longest_name) {
longest_name = (unsigned long*)sizeof(entry->d_name);
}
}
}
*file_count = index;
}
closedir(dir);
}
void get_dir_content(char *path, char **dir_content){
DIR *dir = opendir(path);
char content[file_count][longest_name];
memset(content,0,sizeof(content));
if (dir) {
int index = 0;
struct dirent *entry;
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !(settings & 1)) { //bit 0 = show_hidden_files
strcpy(dir_content[index], entry->d_name);
index++;
} else if (settings & 1){ //bit 0 = show_hidden_files
strcpy(dir_content[index], entry->d_name);
index++;
}
}
}
closedir(dir);
}
void print_dir(WINDOW *win, char **dir_content){
for (unsigned long i = 0; i < (unsigned long)file_count; i++ ){
wprintw(win, "%s",dir_content[i]);
wmove(win, i, 1);
}
}
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
char wh = (char)which;
if (wh) {
if (wh==1) {
get_dir_size(path, &file_count, &longest_name);
content_m = calloc(file_count, sizeof(*content_m));
for (unsigned long i = 0; i<file_count; i++) {
content_m[i] = calloc(longest_name, sizeof(content_m[i]));
}
get_dir_content(path, content_m);
} else {
get_dir_size(path, &file_count, &longest_name);
content_r = calloc(file_count, sizeof(*content_r));
for (unsigned long i = 0; i<file_count; i++) {
content_r[i] = calloc(longest_name, sizeof(content_r[i]));
}
get_dir_content(path, content_r);
}
} else {
get_dir_size(path, &file_count, &longest_name);
content_l = calloc(file_count, sizeof(*content_l));
for (unsigned long i = 0; i<file_count; i++) {
content_l[i] = calloc(longest_name, sizeof(content_l));
}
get_dir_content(path, content_l);
}
return NULL;
}