code base good enough to actually progress
This commit is contained in:
129
backend.c
129
backend.c
@ -1,100 +1,75 @@
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "defines.h"
|
||||
#include "sorting.h"
|
||||
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern file_data *content_l;
|
||||
extern file_data *content_m;
|
||||
extern file_data *content_r;
|
||||
extern char *path;
|
||||
|
||||
unsigned long file_count_l;
|
||||
unsigned long file_count_m;
|
||||
unsigned long file_count_r;
|
||||
|
||||
|
||||
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 = 0;
|
||||
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, file_data *dir_content){
|
||||
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width){
|
||||
DIR *dir = opendir(path);
|
||||
if (dir) {
|
||||
unsigned long index = 0;
|
||||
unsigned long entry_count = 0;
|
||||
unsigned long max_length;
|
||||
struct dirent *entry;
|
||||
while ( (entry=readdir(dir)) ) {
|
||||
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
dir_content[index].file_name = entry->d_name;
|
||||
dir_content[index].file_type = entry->d_type;
|
||||
index++;
|
||||
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
||||
dir_content[index].file_name = entry->d_name;
|
||||
dir_content[index].file_type = entry->d_type;
|
||||
index++;
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
unsigned int current_length = 0;
|
||||
unsigned int i = 0;
|
||||
for (; entry->d_name[i] != '\0'; i++) {
|
||||
current_length++;
|
||||
}
|
||||
if (current_length > max_length) {
|
||||
/*dynamic filename length to save on memory*/
|
||||
max_length = current_length;
|
||||
}
|
||||
entry_count++;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
dir_length_width[0] = entry_count;
|
||||
dir_length_width[1] = max_length;
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, unsigned long file_count, file_data *dir_content){
|
||||
for (unsigned long i = 0; i < file_count; i++ ){ //skip index 0 as it is used for metadata like file count
|
||||
if (dir_content[i].file_name) {
|
||||
wprintw(win, "%s",dir_content[i].file_name);
|
||||
wmove(win, i, 1);
|
||||
} else {
|
||||
wprintw(win, "NULL");
|
||||
wmove(win, i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
|
||||
char wh = (char)which;
|
||||
unsigned long longest_name = 0;
|
||||
|
||||
if (wh) {
|
||||
if (wh == 1) {
|
||||
free(content_m);
|
||||
get_dir_size(path, &file_count_m, &longest_name);
|
||||
content_m = (file_data*)calloc(file_count_m, sizeof(file_data));
|
||||
get_dir_content(path, file_count_m, longest_name, content_m);
|
||||
sort_dir(&file_count_m, &longest_name, content_m);
|
||||
} else {
|
||||
free(content_r);
|
||||
get_dir_size(path, &file_count_r, &longest_name);
|
||||
content_r = calloc(file_count_r, sizeof(file_data));
|
||||
get_dir_content(path, file_count_r, longest_name, content_r);
|
||||
sort_dir(&file_count_m, &longest_name, content_r);
|
||||
}
|
||||
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content){
|
||||
struct dirent **entry;
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||
dir_length_width[0] = scandir(path, &entry, NULL, alphasort);
|
||||
} else {
|
||||
free(content_l);
|
||||
get_dir_size(path, &file_count_l, &longest_name);
|
||||
content_l = calloc(file_count_l, sizeof(file_data));
|
||||
get_dir_content(path, file_count_l, longest_name, content_l);
|
||||
sort_dir(&file_count_m, &longest_name, content_l);
|
||||
dir_length_width[0] = scandir(path, &entry, skip_hidden_files, alphasort);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < dir_length_width[0]; i++ ) {
|
||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
} else {
|
||||
unsigned long j = 0;
|
||||
for (; entry[i]->d_name[j] != '\0'; j++) {
|
||||
dir_content[i * dir_length_width[1] + j] = entry[i]->d_name[j];
|
||||
}
|
||||
dir_width[i] = j;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < dir_length_width[0]; i++) {
|
||||
free(entry[i]);
|
||||
}
|
||||
free(entry);
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *file_width, char *dir_content){
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i<dir_length_width[0]; i++) {
|
||||
unsigned long j = 0;
|
||||
for (j = 0; j < file_width[i]; j++){
|
||||
mvwprintw(win, i, j, "%c", dir_content[i * dir_length_width[1] + j]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user