th/sorting.c
2025-03-30 15:58:05 +02:00

72 lines
2.0 KiB
C

#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 file_data *content_l;
extern file_data *content_m;
extern file_data *content_r;
extern char *path;
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 strcmp(file0_name, file1_name);
}
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);
}
void sort_dir(unsigned long *file_count, unsigned long *longest_name, file_data *dir_content){
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(file_data), alphabetic);
} else if (file_modifiers & FILE_MODIFIERS_SORT_TYPE) {
qsort(dir_content, *file_count, sizeof(file_data), type);
}
}