From abf758cdbfe5937e2f8f02e9bdac00a378665511 Mon Sep 17 00:00:00 2001 From: nova Date: Mon, 3 Mar 2025 15:44:38 +0100 Subject: [PATCH] refractoring --- backend.c | 24 ++++++++++++++++++++---- backend.h | 1 + defines.h | 5 +++++ interactions.c | 8 +++++--- th | Bin 16888 -> 0 bytes 5 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 defines.h delete mode 100755 th diff --git a/backend.c b/backend.c index 949fed5..9ebd66b 100644 --- a/backend.c +++ b/backend.c @@ -1,11 +1,13 @@ #include #include +#include #include #include #include #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; } + diff --git a/backend.h b/backend.h index 94b23c6..7ab3b66 100644 --- a/backend.h +++ b/backend.h @@ -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); diff --git a/defines.h b/defines.h new file mode 100644 index 0000000..4e4234c --- /dev/null +++ b/defines.h @@ -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 diff --git a/interactions.c b/interactions.c index 1a0a9b1..19a679b 100644 --- a/interactions.c +++ b/interactions.c @@ -3,13 +3,15 @@ #include #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 { } diff --git a/th b/th deleted file mode 100755 index 71ce2ca7523e47ba10fb13f442c66bd957b319b2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16888 zcmeHOdvH_NnLm;daT`Dm!3mI;LZN{s2uy%L0!0P{mo5fMAWbKai?C%|jVyU(89U6f zHF4;oC`xJC*<~}cT~8moOwvi1^p(&wu_0?a-JOidB#pPTn-OfXBZX|@yp>1M{=Re0 zcXf4bb<^Gcvok&(={w)=dz|ll=iGDdmGAjxaO)1PupatDG>Iz6e ztP<1leWkcUOa(thVoDxR1X5K--Q~1e<5i$!H=8OG(90Dpm~wf+ z_Ly=$DJS$W(NtI-afG^*QS%I$p> zc9fUTT6mau>h>y)gLD4TcvD#tjqF{yd`YxvaWoR^>|WfxYUSdU%l#d3|1v43xB~dl zoZ7r?x4^LU6UM{o?os>&z^J%{OUl{5Tk}e)#*ShpIr*bPzVvWQO4kC(RUQtyM31np zkI`%Izx;Ig`8J3rEQ3I2P9PbpfKxlZ3G^SWpx;*kzXpybYWL;}`aiFLM=Icn3ixdm za9@RZ{<(twkqY>n3VyDDz7HQ~xu}BvVJJ?NpQ9Dw)|5ah78M zCW`aL3V1^WoR+nV#2n!Zpn}5dkQ@1#r=aq0bIYy+ZccL}UBihzlV# z-eoqn-eoq2B2fX)VbK_kcZ8cF3DKTxO@u>D<~QPqhM7h{N$L_7o<`lJwh$Cdv#~p5 zHb-KiC^bx$CbH;BymX{CE>bSv50HHnDRP2D-xMbtK8Q# z_OtK&HI}g|5z>6;+{ovs%_w*(ykGMCSUOALV|qViJge_hssl>SIP*0Qe7aBRjjmIa z)>0L*;?}a2- zYQyDyjnu~eZ^P9-p#%XN&U+F`3>(h5BYcMq$G}+SRvZ3#3l(CI4cE6|Qrc?6r`hz| zZ8#=|Rl05X3=0*a$A;@$H7Px8!!NSwAF<)~b?c}N*HcGY_1SPe)g*q*hS%EsAGhI` z*zhN8_@y?y--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