last commit before large rewrite
This commit is contained in:
parent
2a1d273bc0
commit
931d7026ea
72
backend.c
72
backend.c
@ -9,16 +9,20 @@
|
||||
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern char **content_l;
|
||||
extern char **content_m;
|
||||
extern char **content_r;
|
||||
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 = 1; //always makes the array at least 1 big, used for metadata like the amount of files
|
||||
unsigned long index = 0;
|
||||
if (dir) {
|
||||
struct dirent *entry;
|
||||
while ( (entry=readdir(dir)) ) {
|
||||
@ -33,19 +37,19 @@ void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char **dir_content){
|
||||
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, file_data *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
|
||||
unsigned long index = 0;
|
||||
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);
|
||||
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) {
|
||||
strcpy(dir_content[index], entry->d_name);
|
||||
dir_content[index].file_name = entry->d_name;
|
||||
dir_content[index].file_type = entry->d_type;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
@ -53,48 +57,42 @@ void get_dir_content(char *path, unsigned long file_count, unsigned long longest
|
||||
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);
|
||||
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 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);
|
||||
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, &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);
|
||||
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);
|
||||
}
|
||||
} 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);
|
||||
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);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
|
||||
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 get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, file_data *dir_content);
|
||||
void print_dir(WINDOW *win, unsigned long file_count, file_data *dir_content);
|
||||
//void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content);
|
||||
void *populate_dir(void *dir);
|
||||
|
@ -13,3 +13,11 @@
|
||||
#define FILE_MODIFIERS_SORT_RANDOM 32
|
||||
#define FILE_MODIFIERS_SORT_REVERSE 64
|
||||
//FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0
|
||||
|
||||
#ifndef HEADER_GUARD
|
||||
#define HEADER_GUARD
|
||||
typedef struct file_data {
|
||||
char *file_name;
|
||||
unsigned char file_type;
|
||||
} file_data;
|
||||
#endif
|
||||
|
@ -4,16 +4,22 @@
|
||||
#include "defines.h"
|
||||
|
||||
|
||||
extern unsigned int file_modifiers;
|
||||
|
||||
void user_interactions(char *input, unsigned int *status, unsigned int *settings, unsigned int *file_modifiers) {
|
||||
void user_interactions(char *input, unsigned int *status, unsigned int *settings) {
|
||||
if (*input == 'q') {
|
||||
*status ^= STATUS_QUIT_PROGRAM;
|
||||
} else if (*input == *"KEY_BACKSPACE") {
|
||||
*file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
} else if (*input == 'a') {
|
||||
*file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
} else if (*input == 'o') {
|
||||
file_modifiers ^= FILE_MODIFIERS_SORT_BITMASK;
|
||||
} else if (*input == 'e') {
|
||||
file_modifiers ^= FILE_MODIFIERS_SORT_ALPHABETIC;
|
||||
} else if (*input == 'u') {
|
||||
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
}
|
||||
*status ^= STATUS_RUN_BACKEND;
|
||||
*status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
||||
}
|
||||
|
@ -2,4 +2,4 @@
|
||||
#include <pthread.h>
|
||||
#include "interactions.c"
|
||||
|
||||
void user_interactions(char *input, unsigned int *status, unsigned int *settings, unsigned int *file_modifiers);
|
||||
void user_interactions(char *input, unsigned int *status, unsigned int *settings);
|
||||
|
25
main.c
25
main.c
@ -15,9 +15,9 @@ unsigned int settings;
|
||||
unsigned int file_modifiers;
|
||||
unsigned int status; //bit 0 = enable
|
||||
unsigned short cpu_cores; //amount of cores/threads the host system reports to have
|
||||
char **content_l; //content of parent dir, used in left window
|
||||
char **content_m; //content of current dir, used in main window
|
||||
char **content_r; //content of child dir, used in right window
|
||||
file_data *content_l; //content of parent dir, used in left window
|
||||
file_data *content_m; //content of current dir, used in main window
|
||||
file_data *content_r; //content of child dir, used in right window
|
||||
char *path = ".";
|
||||
char input = 0;
|
||||
|
||||
@ -52,7 +52,8 @@ int main() {
|
||||
pthread_join(populate_l, NULL);
|
||||
pthread_join(populate_m, NULL);
|
||||
pthread_join(populate_r, NULL);
|
||||
status ^= STATUS_UPDATE_SCREEN_0;
|
||||
status ^= STATUS_RUN_BACKEND;
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
|
||||
|
||||
@ -67,12 +68,13 @@ int main() {
|
||||
}
|
||||
|
||||
wmove(stdscr,0,0);
|
||||
status &= ~STATUS_UPDATE_SCREEN_MASK;
|
||||
//status &= ~STATUS_UPDATE_SCREEN_MASK;
|
||||
status = 0;
|
||||
}
|
||||
|
||||
|
||||
if ((input = getch())) {
|
||||
user_interactions(&input, &status, &settings, &file_modifiers);
|
||||
user_interactions(&input, &status, &settings);
|
||||
}
|
||||
}
|
||||
free(content_l);
|
||||
@ -88,11 +90,12 @@ int main() {
|
||||
void init() {
|
||||
|
||||
cpu_cores = get_nprocs();
|
||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
status ^= STATUS_RUN_BACKEND;
|
||||
//file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);
|
||||
file_modifiers = FILE_MODIFIERS_SORT_BITMASK;
|
||||
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
||||
|
||||
content_l = calloc(1, sizeof(*content_l)); //allocation in order to allow a more streamlined backend
|
||||
content_m = calloc(1, sizeof(*content_m));
|
||||
content_r = calloc(1, sizeof(*content_r));
|
||||
content_l = (file_data*)calloc(1, sizeof(file_data)); //allocation in order to allow a more streamlined backend
|
||||
content_m = (file_data*)calloc(1, sizeof(file_data));
|
||||
content_r = (file_data*)calloc(1, sizeof(file_data));
|
||||
|
||||
}
|
||||
|
68
sorting.c
68
sorting.c
@ -12,46 +12,58 @@
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern unsigned short cpu_cores; //amount of cores/threads the host system reports to have
|
||||
extern char **content_l;
|
||||
extern char **content_m;
|
||||
extern char **content_r;
|
||||
extern file_data *content_l;
|
||||
extern file_data *content_m;
|
||||
extern file_data *content_r;
|
||||
extern char *path;
|
||||
|
||||
int natural(const void *file0, const void *file1){
|
||||
const char *rec1 = *(char**)file0;
|
||||
const char *rec2 = *(char**)file1;
|
||||
int ret = 0;
|
||||
struct stat f0;
|
||||
struct stat f1;
|
||||
stat(rec1, &f0);
|
||||
stat(rec2, &f1);
|
||||
|
||||
|
||||
if (S_ISDIR(f0.st_mode) > S_ISDIR(f1.st_mode)) {
|
||||
ret = 1;
|
||||
} else if (S_ISDIR(f0.st_mode) < S_ISDIR(f1.st_mode)) {
|
||||
ret = -1;
|
||||
int type(const void *f0, const void *f1){
|
||||
const char *file0_name = ((file_data*)f0)->file_name;
|
||||
const char *file1_name = ((file_data*)f1)->file_name;
|
||||
const char file0_type = ((file_data*)f0)->file_type;
|
||||
const char file1_type = ((file_data*)f1)->file_type;
|
||||
if (file0_type > file1_type) {
|
||||
return -1;
|
||||
} else if (file0_type < file1_type) {
|
||||
return 1;
|
||||
} else {
|
||||
return strcmp(file0_name, file1_name);
|
||||
}
|
||||
}
|
||||
int natural(const void *f0, const void *f1){
|
||||
const char *file0_name = ((file_data*)f0)->file_name;
|
||||
const char *file1_name = ((file_data*)f1)->file_name;
|
||||
const char file0_type = ((file_data*)f0)->file_type;
|
||||
const char file1_type = ((file_data*)f1)->file_type;
|
||||
if (S_ISDIR(file0_type) || S_ISDIR(file1_type)) {
|
||||
if (file0_type == file1_type) {
|
||||
return strcmp(file0_name, file1_name);
|
||||
} else if (S_ISDIR(file1_type)) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
//return strcmp(file0_name, file1_name);
|
||||
}
|
||||
|
||||
int alphabetic(const void *str1, const void *str2){
|
||||
const char *rec1 = *(char**)str1;
|
||||
const char *rec2 = *(char**)str2;
|
||||
int ret = strcmp(str1, rec2);
|
||||
int alphabetic(const void *f0, const void *f1){
|
||||
const char *file0_name = ((file_data*)f0)->file_name;
|
||||
const char *file1_name = ((file_data*)f1)->file_name;
|
||||
return strcmp(file0_name, file1_name);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **dir_content){
|
||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content){
|
||||
|
||||
if (file_modifiers & ~FILE_MODIFIERS_SORT_BITMASK) {
|
||||
qsort(dir_content, *file_count, sizeof(longest_name), alphabetic);
|
||||
qsort(dir_content, *file_count, sizeof(longest_name), natural);
|
||||
if ((file_modifiers & FILE_MODIFIERS_SORT_BITMASK) == ~FILE_MODIFIERS_SORT_BITMASK) {
|
||||
qsort(dir_content, *file_count, sizeof(file_data), natural);
|
||||
|
||||
} else if (file_modifiers & FILE_MODIFIERS_SORT_ALPHABETIC) {
|
||||
qsort(dir_content, *file_count, sizeof(longest_name), alphabetic);
|
||||
qsort(dir_content, *file_count, sizeof(file_data), alphabetic);
|
||||
|
||||
} else if (file_modifiers & FILE_MODIFIERS_SORT_TYPE) {
|
||||
qsort(dir_content, *file_count, sizeof(file_data), type);
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,5 +7,5 @@
|
||||
#include "defines.h"
|
||||
#include "sorting.c"
|
||||
|
||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **dir_content);
|
||||
void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content);
|
||||
|
||||
|
4
structs.h
Normal file
4
structs.h
Normal file
@ -0,0 +1,4 @@
|
||||
typedef struct data {
|
||||
char *file_name;
|
||||
unsigned char file_type;
|
||||
} file_data;
|
16
window.c
16
window.c
@ -4,12 +4,12 @@
|
||||
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned long file_count_l;
|
||||
extern unsigned long file_count_m;
|
||||
extern unsigned long file_count_r;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
|
||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
||||
|
||||
//WINDOW *win = (window_data)window_data.win;
|
||||
unsigned int local_width;
|
||||
@ -24,14 +24,14 @@ void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char *
|
||||
|
||||
wmove(win, 1, 1);
|
||||
|
||||
print_dir(win, dir_content);
|
||||
print_dir(win, file_count_m, dir_content);
|
||||
|
||||
|
||||
|
||||
box(win,0,0);
|
||||
wrefresh(win);
|
||||
}
|
||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
|
||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
||||
|
||||
unsigned int local_width;
|
||||
unsigned int local_height;
|
||||
@ -45,13 +45,13 @@ void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char *
|
||||
|
||||
wmove(win, 1, 1);
|
||||
|
||||
print_dir(win, dir_content);
|
||||
print_dir(win, file_count_l, dir_content);
|
||||
|
||||
|
||||
box(win,0,0);
|
||||
wrefresh(win);
|
||||
}
|
||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
|
||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content){
|
||||
|
||||
wmove(win, 0, 0);
|
||||
unsigned int local_width;
|
||||
|
6
window.h
6
window.h
@ -1,7 +1,7 @@
|
||||
#include <curses.h>
|
||||
#include "window.c"
|
||||
|
||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
|
||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
|
||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
|
||||
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
||||
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
||||
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, file_data *dir_content);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user