From cc5bb9bc343018442fee18b8af598dea54004ca7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 14 Dec 2015 11:55:22 +0000 Subject: [PATCH] Sort commands printed in help output. Also fix const-correctness. Hopefully. --- src/main/findactions.c | 27 +++++++++++++++++++++++++-- src/main/findactions.h | 1 + src/main/main.c | 24 +++++++++--------------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/main/findactions.c b/src/main/findactions.c index 16f08e0..27d6059 100644 --- a/src/main/findactions.c +++ b/src/main/findactions.c @@ -15,14 +15,15 @@ * along with "dindexer". If not, see . */ +#include "findactions.h" +#include "dindexerConfig.h" +#include "helpers/lengthof.h" #include #include #include #include #include #include -#include "dindexerConfig.h" -#include "helpers/lengthof.h" typedef struct ActionListStruct { char** list; @@ -33,6 +34,7 @@ typedef struct ActionListStruct { static void increase_actionlist ( 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 int strcmp_wrapper ( const void* parA, const void* parB ); void find_actions (char*** parOut, size_t* parCount) { ActionList list; @@ -47,6 +49,7 @@ void find_actions (char*** parOut, size_t* parCount) { list.list = *parOut; list.used = 0; foreach_dir(&push_action, &list); + qsort((void*)list.list, list.count, sizeof(char*), &strcmp_wrapper); *parCount = list.count; } @@ -131,3 +134,23 @@ static void push_action (const char* parName, ActionList* parList) { strcpy(parList->list[parList->used], parName); ++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; +} diff --git a/src/main/findactions.h b/src/main/findactions.h index e6bb609..f26a4b0 100644 --- a/src/main/findactions.h +++ b/src/main/findactions.h @@ -22,5 +22,6 @@ void find_actions ( char*** parOut, size_t* parCount ); void free_actions ( char** parActions, size_t parCount ); +const char* get_actionname ( const char* parAction ); #endif diff --git a/src/main/main.c b/src/main/main.c index b8d431b..41740f5 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -24,9 +24,9 @@ #include "findactions.h" #include "helpers/lengthof.h" -static size_t foreach_avail_action ( int(*parFunc)(char*, char*), char** parList, size_t parCount, char* parPass ); -static int printf_stderr ( char* parMsg, char* parUnused ); -static int same_action ( char* parAction1, char* parAction2 ); +static size_t foreach_avail_action ( int(*parFunc)(const char*, const char*), char** parList, size_t parCount, char* parPass ); +static int printf_stderr ( const char* parMsg, const char* parUnused ); +static int same_action ( const char* parAction1, const char* parAction2 ); int main (int parArgc, char* parArgv[]) { size_t z; @@ -96,21 +96,15 @@ int main (int parArgc, char* parArgv[]) { 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; - char* cmd_name_start; + const char* cmd_name_start; int stop; for (z = 0; z < parCount; ++z) { - cmd_name_start = strchr(parList[z], '/'); - if (not cmd_name_start) { - cmd_name_start = parList[z]; - } - else { - ++cmd_name_start; - } + cmd_name_start = get_actionname(parList[z]); - stop = (*parFunc)(cmd_name_start + lengthof(ACTION_PREFIX) - 1, parPass); + stop = (*parFunc)(cmd_name_start, parPass); if (stop) { return z; } @@ -118,12 +112,12 @@ static size_t foreach_avail_action(int(*parFunc)(char*, char*), char** parList, 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); 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)) { return 1; }