1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-29 01:33:46 +00:00

Sort commands printed in help output.

Also fix const-correctness. Hopefully.
This commit is contained in:
King_DuckZ 2015-12-14 11:55:22 +00:00
parent ebdd24a8f5
commit cc5bb9bc34
3 changed files with 35 additions and 17 deletions

View file

@ -15,14 +15,15 @@
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>. * along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "findactions.h"
#include "dindexerConfig.h"
#include "helpers/lengthof.h"
#include <dirent.h> #include <dirent.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <iso646.h> #include <iso646.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "dindexerConfig.h"
#include "helpers/lengthof.h"
typedef struct ActionListStruct { typedef struct ActionListStruct {
char** list; char** list;
@ -33,6 +34,7 @@ typedef struct ActionListStruct {
static void increase_actionlist ( const char* parName, ActionList* parList ); static void increase_actionlist ( const char* parName, ActionList* parList );
static void push_action ( const char* parName, ActionList* parList ); static void push_action ( const char* parName, ActionList* parList );
static void foreach_dir ( void(*parAction)(const char*, ActionList*), ActionList* parList ); static void foreach_dir ( void(*parAction)(const char*, ActionList*), ActionList* parList );
static int strcmp_wrapper ( const void* parA, const void* parB );
void find_actions (char*** parOut, size_t* parCount) { void find_actions (char*** parOut, size_t* parCount) {
ActionList list; ActionList list;
@ -47,6 +49,7 @@ void find_actions (char*** parOut, size_t* parCount) {
list.list = *parOut; list.list = *parOut;
list.used = 0; list.used = 0;
foreach_dir(&push_action, &list); foreach_dir(&push_action, &list);
qsort((void*)list.list, list.count, sizeof(char*), &strcmp_wrapper);
*parCount = list.count; *parCount = list.count;
} }
@ -131,3 +134,23 @@ static void push_action (const char* parName, ActionList* parList) {
strcpy(parList->list[parList->used], parName); strcpy(parList->list[parList->used], parName);
++parList->used; ++parList->used;
} }
static int strcmp_wrapper (const void* parA, const void* parB) {
const char* const arg1 = *(const char**)parA;
const char* const arg2 = *(const char**)parB;
return strcmp(get_actionname(arg1), get_actionname(arg2));
}
const char* get_actionname (const char* parAction) {
const char* cmd_name_start;
cmd_name_start = strchr(parAction, '/');
if (not cmd_name_start) {
cmd_name_start = parAction;
}
else {
++cmd_name_start;
}
return cmd_name_start + lengthof(ACTION_PREFIX) - 1;
}

View file

@ -22,5 +22,6 @@
void find_actions ( char*** parOut, size_t* parCount ); void find_actions ( char*** parOut, size_t* parCount );
void free_actions ( char** parActions, size_t parCount ); void free_actions ( char** parActions, size_t parCount );
const char* get_actionname ( const char* parAction );
#endif #endif

View file

@ -24,9 +24,9 @@
#include "findactions.h" #include "findactions.h"
#include "helpers/lengthof.h" #include "helpers/lengthof.h"
static size_t foreach_avail_action ( int(*parFunc)(char*, char*), char** parList, size_t parCount, char* parPass ); static size_t foreach_avail_action ( int(*parFunc)(const char*, const char*), char** parList, size_t parCount, char* parPass );
static int printf_stderr ( char* parMsg, char* parUnused ); static int printf_stderr ( const char* parMsg, const char* parUnused );
static int same_action ( char* parAction1, char* parAction2 ); static int same_action ( const char* parAction1, const char* parAction2 );
int main (int parArgc, char* parArgv[]) { int main (int parArgc, char* parArgv[]) {
size_t z; size_t z;
@ -96,21 +96,15 @@ int main (int parArgc, char* parArgv[]) {
return 0; return 0;
} }
static size_t foreach_avail_action(int(*parFunc)(char*, char*), char** parList, size_t parCount, char* parPass) { static size_t foreach_avail_action(int(*parFunc)(const char*, const char*), char** parList, size_t parCount, char* parPass) {
size_t z; size_t z;
char* cmd_name_start; const char* cmd_name_start;
int stop; int stop;
for (z = 0; z < parCount; ++z) { for (z = 0; z < parCount; ++z) {
cmd_name_start = strchr(parList[z], '/'); cmd_name_start = get_actionname(parList[z]);
if (not cmd_name_start) {
cmd_name_start = parList[z];
}
else {
++cmd_name_start;
}
stop = (*parFunc)(cmd_name_start + lengthof(ACTION_PREFIX) - 1, parPass); stop = (*parFunc)(cmd_name_start, parPass);
if (stop) { if (stop) {
return z; return z;
} }
@ -118,12 +112,12 @@ static size_t foreach_avail_action(int(*parFunc)(char*, char*), char** parList,
return z; return z;
} }
static int printf_stderr (char* parMsg, char* parUnused) { static int printf_stderr (const char* parMsg, const char* parUnused) {
fprintf(stderr, "\t%s\n", parMsg); fprintf(stderr, "\t%s\n", parMsg);
return 0; return 0;
} }
static int same_action (char* parAction1, char* parAction2) { static int same_action (const char* parAction1, const char* parAction2) {
if (0 == strcmp(parAction1, parAction2)) { if (0 == strcmp(parAction1, parAction2)) {
return 1; return 1;
} }