refractoring

This commit is contained in:
nova 2025-03-03 15:44:38 +01:00
parent bcd7acbd08
commit abf758cdbf
5 changed files with 31 additions and 7 deletions

View File

@ -1,11 +1,13 @@
#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;
@ -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;
}

View File

@ -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);

5
defines.h Normal file
View File

@ -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

View File

@ -3,13 +3,15 @@
#include <unistd.h>
#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 {
}

BIN
th

Binary file not shown.