basic sorting implemented, segfaults in natural sort
This commit is contained in:
parent
d6a827ba74
commit
2a1d273bc0
14
backend.c
14
backend.c
@ -5,6 +5,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
#include "sorting.h"
|
||||||
|
|
||||||
extern unsigned int settings;
|
extern unsigned int settings;
|
||||||
extern unsigned int file_modifiers;
|
extern unsigned int file_modifiers;
|
||||||
@ -13,6 +14,7 @@ extern char **content_m;
|
|||||||
extern char **content_r;
|
extern char **content_r;
|
||||||
extern char *path;
|
extern char *path;
|
||||||
|
|
||||||
|
|
||||||
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
|
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
|
||||||
DIR *dir = opendir(path);
|
DIR *dir = opendir(path);
|
||||||
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
|
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
|
||||||
@ -58,18 +60,6 @@ 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
|
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
|
||||||
char wh = (char)which;
|
char wh = (char)which;
|
||||||
unsigned long file_count = 0;
|
unsigned long file_count = 0;
|
||||||
|
@ -6,3 +6,10 @@
|
|||||||
|
|
||||||
#define FILE_MODIFIERS_HIDDEN_FILES 1
|
#define FILE_MODIFIERS_HIDDEN_FILES 1
|
||||||
#define FILE_MODIFIERS_SORT_BITMASK 126 // 00000000000000000000000001111110
|
#define FILE_MODIFIERS_SORT_BITMASK 126 // 00000000000000000000000001111110
|
||||||
|
#define FILE_MODIFIERS_SORT_ALPHABETIC 2
|
||||||
|
#define FILE_MODIFIERS_SORT_TYPE 4
|
||||||
|
#define FILE_MODIFIERS_SORT_EXTENSION 8
|
||||||
|
#define FILE_MODIFIERS_SORT_SIZE 16
|
||||||
|
#define FILE_MODIFIERS_SORT_RANDOM 32
|
||||||
|
#define FILE_MODIFIERS_SORT_REVERSE 64
|
||||||
|
//FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0
|
||||||
|
59
sorting.c
Normal file
59
sorting.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include <curses.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include "defines.h"
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int alphabetic(const void *str1, const void *str2){
|
||||||
|
const char *rec1 = *(char**)str1;
|
||||||
|
const char *rec2 = *(char**)str2;
|
||||||
|
int ret = strcmp(str1, rec2);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **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);
|
||||||
|
|
||||||
|
} else if (file_modifiers & FILE_MODIFIERS_SORT_ALPHABETIC) {
|
||||||
|
qsort(dir_content, *file_count, sizeof(longest_name), alphabetic);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
11
sorting.h
Normal file
11
sorting.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <curses.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "defines.h"
|
||||||
|
#include "sorting.c"
|
||||||
|
|
||||||
|
void sort_dir(unsigned long *file_count, unsigned long *longest_name, char **dir_content);
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user