From cdfa9fec4ec54dfd5ca7df6eaa8e45337c7c909b Mon Sep 17 00:00:00 2001 From: zegonix Date: Sun, 19 Jul 2026 22:34:04 +0200 Subject: [PATCH] cleaned up sorting functionality, sorting by extension pending --- src/sorting.c | 188 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 119 insertions(+), 69 deletions(-) diff --git a/src/sorting.c b/src/sorting.c index ff54367..1ba84b4 100644 --- a/src/sorting.c +++ b/src/sorting.c @@ -4,8 +4,10 @@ #include #include +#include #include #include +#include #include "sorting.h" #include "type_definitions.h" #include "arguments.h" @@ -13,11 +15,27 @@ #include "stringing.h" /* function prototypes */ -int comp_name_case_insensitive( +int compare_string_case_insensitive( const void* operand_a, const void* operand_b ); -int comp_name_case_sensitive( +int compare_string_case_sensitive( + const void* operand_a, + const void* operand_b +); +int compare_directory( + const void* operand_a, + const void* operand_b +); +int compare_extension( + const void* operand_a, + const void* operand_b +); +int compare_timestamp( + const void* operand_a, + const void* operand_b +); +int compare_size( const void* operand_a, const void* operand_b ); @@ -37,17 +55,38 @@ int sort_entries( if ( !entry_table ) return -EINVAL; if ( 0 > num_entries ) return -EINVAL; + __compar_fn_t compare_function; + + switch ( args->sorting_mode ) + { + case sort_by_name: + compare_function = compare_string_case_insensitive; + break; + case sort_by_extension: + compare_function = compare_extension; + break; + case sort_by_time: + compare_function = compare_timestamp; + break; + case sort_by_size: + compare_function = compare_size; + break; + } + + qsort( + ( void* )entry_table, + ( size_t )num_entries, + sizeof( *entry_table ), + compare_function + ); + if ( true == args->group_directories ) - { - sort_directories_first( entry_table, num_entries ); - } - else { qsort( - ( void * )entry_table, + ( void* )entry_table, ( size_t )num_entries, sizeof( *entry_table ), - comp_name_case_insensitive + compare_directory ); } @@ -57,7 +96,7 @@ int sort_entries( /* * compare entries case insensitive */ -int comp_name_case_insensitive( const void* operand_a, const void* operand_b ) +int compare_string_case_insensitive( const void* operand_a, const void* operand_b ) { if ( !operand_a || !operand_b ) { @@ -74,7 +113,7 @@ int comp_name_case_insensitive( const void* operand_a, const void* operand_b ) /* * compare entries case sensitive */ -int comp_name_case_sensitive( +int compare_string_case_sensitive( const void* operand_a, const void* operand_b ) @@ -91,75 +130,86 @@ int comp_name_case_sensitive( return compare_strings( a->name, b->name, NAME_MAX, true ); } -/* group directories first */ -int sort_directories_first( - directory_entry_t** const entry_table, - int num_entries +/* compare directory entries + * + * + */ +int compare_directory( + const void* operand_a, + const void* operand_b ) { - if ( !entry_table ) return -EINVAL; - if ( 0 >= num_entries ) return -EINVAL; - - int directory_counter = 0; - int non_dir_counter = 0; - directory_entry_t** directory_table = - ( directory_entry_t** )calloc( - ( size_t )num_entries, - sizeof( directory_entry_t* ) - ); - if ( !directory_table ) return -ENOMEM; - - directory_entry_t** non_dir_table = - ( directory_entry_t** )calloc( - ( size_t )num_entries, - sizeof( directory_entry_t* ) - ); - if ( !non_dir_table ) + if ( !operand_a || !operand_b ) { - free( directory_table ); - return -ENOMEM; + log_error( "invalid parameter to compare function" ); + exit( -EINVAL ); } - for ( int n = 0; n < num_entries; n++ ) - { - if ( entry_table[ n ]->stats.st_mode & S_IFDIR ) - { - directory_table[ directory_counter ] = entry_table[ n ]; - directory_counter++; - } - else - { - non_dir_table[ non_dir_counter ] = entry_table[ n ]; - non_dir_counter++; - } - } - if ( num_entries != directory_counter + non_dir_counter ) - log_error( "`group_directories()`: counting is difficult" ); + directory_entry_t* a = *( directory_entry_t** )operand_a; + directory_entry_t* b = *( directory_entry_t** )operand_b; - qsort( - ( void * )directory_table, - ( size_t )directory_counter, - sizeof( *directory_table ), - comp_name_case_insensitive - ); - qsort( - ( void * )non_dir_table, - ( size_t )non_dir_counter, - sizeof( *non_dir_table ), - comp_name_case_insensitive - ); + return ( b->stats.st_mode & S_IFDIR ) - ( a->stats.st_mode & S_IFDIR ); +} - for ( int n = 0; n < directory_counter; n++ ) +int compare_extension( + const void* operand_a, + const void* operand_b +) +{ + if ( !operand_a || !operand_b ) { - entry_table[ n ] = directory_table[ n ]; - } - for ( int n = 0; n < non_dir_counter; n++ ) - { - entry_table[ directory_counter + n ] = non_dir_table[ n ]; + log_error( "invalid parameter to compare function" ); + exit( -EINVAL ); } - free( directory_table ); - free( non_dir_table ); + // directory_entry_t* a = *( directory_entry_t** )operand_a; + // directory_entry_t* b = *( directory_entry_t** )operand_b; + // char* extension; + // + // size_t len_a = strnlen(a->name, NAME_MAX); + // size_t len_b = strnlen(b->name, NAME_MAX); + // + // char* entry_a = ( char* )calloc( + // len_b, + // sizeof( char ) + // ); + // char* entry_b = ( char* )calloc( + // len_b, + // sizeof( char ) + // ); + // + // extension = strrchr( a->name, '.' ); + // + // strncpy( &entry_a[ 0 ], ) + // + // free( entry_a ); + // free( entry_b ); return 0; } + +int compare_timestamp( + const void* operand_a, + const void* operand_b +) +{ + if ( !operand_a || !operand_b ) + { + log_error( "invalid parameter to compare function" ); + exit( -EINVAL ); + } + return 0; +} + +int compare_size( + const void* operand_a, + const void* operand_b +) +{ + if ( !operand_a || !operand_b ) + { + log_error( "invalid parameter to compare function" ); + exit( -EINVAL ); + } + return 0; +}