cleaned up sorting functionality, sorting by extension pending
This commit is contained in:
1 parent
4b1c8c0de4
commit
cdfa9fec4e
1 file changed
+119
-69
+119
-69
@@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <linux/limits.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <string.h>
|
||||||
#include "sorting.h"
|
#include "sorting.h"
|
||||||
#include "type_definitions.h"
|
#include "type_definitions.h"
|
||||||
#include "arguments.h"
|
#include "arguments.h"
|
||||||
@@ -13,11 +15,27 @@
|
|||||||
#include "stringing.h"
|
#include "stringing.h"
|
||||||
|
|
||||||
/* function prototypes */
|
/* function prototypes */
|
||||||
int comp_name_case_insensitive(
|
int compare_string_case_insensitive(
|
||||||
const void* operand_a,
|
const void* operand_a,
|
||||||
const void* operand_b
|
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_a,
|
||||||
const void* operand_b
|
const void* operand_b
|
||||||
);
|
);
|
||||||
@@ -37,17 +55,38 @@ int sort_entries(
|
|||||||
if ( !entry_table ) return -EINVAL;
|
if ( !entry_table ) return -EINVAL;
|
||||||
if ( 0 > num_entries ) return -EINVAL;
|
if ( 0 > num_entries ) return -EINVAL;
|
||||||
|
|
||||||
if ( true == args->group_directories )
|
__compar_fn_t compare_function;
|
||||||
|
|
||||||
|
switch ( args->sorting_mode )
|
||||||
{
|
{
|
||||||
sort_directories_first( entry_table, num_entries );
|
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;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
qsort(
|
qsort(
|
||||||
( void * )entry_table,
|
( void* )entry_table,
|
||||||
( size_t )num_entries,
|
( size_t )num_entries,
|
||||||
sizeof( *entry_table ),
|
sizeof( *entry_table ),
|
||||||
comp_name_case_insensitive
|
compare_function
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( true == args->group_directories )
|
||||||
|
{
|
||||||
|
qsort(
|
||||||
|
( void* )entry_table,
|
||||||
|
( size_t )num_entries,
|
||||||
|
sizeof( *entry_table ),
|
||||||
|
compare_directory
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +96,7 @@ int sort_entries(
|
|||||||
/*
|
/*
|
||||||
* compare entries case insensitive
|
* 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 )
|
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
|
* compare entries case sensitive
|
||||||
*/
|
*/
|
||||||
int comp_name_case_sensitive(
|
int compare_string_case_sensitive(
|
||||||
const void* operand_a,
|
const void* operand_a,
|
||||||
const void* operand_b
|
const void* operand_b
|
||||||
)
|
)
|
||||||
@@ -91,75 +130,86 @@ int comp_name_case_sensitive(
|
|||||||
return compare_strings( a->name, b->name, NAME_MAX, true );
|
return compare_strings( a->name, b->name, NAME_MAX, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* group directories first */
|
/* compare directory entries
|
||||||
int sort_directories_first(
|
*
|
||||||
directory_entry_t** const entry_table,
|
*
|
||||||
int num_entries
|
*/
|
||||||
|
int compare_directory(
|
||||||
|
const void* operand_a,
|
||||||
|
const void* operand_b
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if ( !entry_table ) return -EINVAL;
|
if ( !operand_a || !operand_b )
|
||||||
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 )
|
|
||||||
{
|
{
|
||||||
free( directory_table );
|
log_error( "invalid parameter to compare function" );
|
||||||
return -ENOMEM;
|
exit( -EINVAL );
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int n = 0; n < num_entries; n++ )
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||||
{
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||||
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" );
|
|
||||||
|
|
||||||
qsort(
|
return ( b->stats.st_mode & S_IFDIR ) - ( a->stats.st_mode & S_IFDIR );
|
||||||
( 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
|
|
||||||
);
|
|
||||||
|
|
||||||
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 ];
|
log_error( "invalid parameter to compare function" );
|
||||||
}
|
exit( -EINVAL );
|
||||||
for ( int n = 0; n < non_dir_counter; n++ )
|
|
||||||
{
|
|
||||||
entry_table[ directory_counter + n ] = non_dir_table[ n ];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free( directory_table );
|
// directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||||
free( non_dir_table );
|
// 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;
|
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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user