viewing paste qsort atc2 | Diff

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
diff --git a/src/common/conf.h b/src/common/conf.h
index 666853b..d223505 100644
--- a/src/common/conf.h
+++ b/src/common/conf.h
@@ -5,7 +5,7 @@
 #define _CONF_H_
 
 #include "../common/cbasetypes.h"
-#include "libconfig.h"
+#include "../../3rdparty/libconfig/libconfig.h"
 
 int conf_read_file(config_t *config, const char *config_filename);
 int config_setting_copy(config_setting_t *parent, const config_setting_t *src);
diff --git a/src/common/utils.c b/src/common/utils.c
index d799285..6986d9a 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -321,3 +321,15 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B)
 
        return (unsigned int)floor(result);
 }
+
+///comparator for qsort
+//comparing int
+int compare_int(const void *a,const void *b) {
+       int *x = (int *) a;
+       int *y = (int *) b;
+       return *x - *y;
+}
+//comparing string
+int compare_str(const void *a,const void *b) {
+       return (strcmp((char *)a,(char *)b));
+}
diff --git a/src/common/utils.h b/src/common/utils.h
index 6ce1639..b98452d 100644
--- a/src/common/utils.h
+++ b/src/common/utils.h
@@ -30,5 +30,7 @@ extern uint16 MakeWord(uint8 byte0, uint8 byte1);
 extern uint32 MakeDWord(uint16 word0, uint16 word1);
 
 int date2version(int date);
+int compare_int(const void *a,const void *b);
+int compare_str(const void *a,const void *b);
 
 #endif /* _UTILS_H_ */
diff --git a/src/map/atcommand.c b/src/map/atcommand.c
index 80bc5d0..fcfd94b 100644
--- a/src/map/atcommand.c
+++ b/src/map/atcommand.c
@@ -8486,6 +8486,11 @@ static int atcommand_mutearea_sub(struct block_list *bl,va_list ap)
        return 0;
 }
 
+//comparing atcommand
+int compare_atc(const void *a,const void *b) {
+       return compare_str(((struct AtCommandInfo*)a)->command,((struct AtCommandInfo*)b)->command);
+}
+
 /*==========================================
  * type: 1 = commands (@), 2 = charcommands (#)
  *------------------------------------------*/
@@ -8493,18 +8498,18 @@ static void atcommand_commands_sub(struct map_session_data* sd, const int fd, At
 {
        char line_buff[CHATBOX_SIZE];
        char* cur = line_buff;
-       AtCommandInfo* cmd;
+       AtCommandInfo *cmd, **commands_li;
        DBIterator *iter = db_iterator(atcommand_db);
-       int count = 0;
+       int count = 0, i=0;
 
+       CREATE(commands_li, AtCommandInfo *, db_size(atcommand_db));
+      
        memset(line_buff,' ',CHATBOX_SIZE);
        line_buff[CHATBOX_SIZE-1] = 0;
 
        clif_displaymessage(fd, msg_txt(sd,273)); // "Commands available:"
 
        for (cmd = dbi_first(iter); dbi_exists(iter); cmd = dbi_next(iter)) {
-               unsigned int slen = 0;
-
                switch( type ) {
                        case COMMAND_CHARCOMMAND:
                                if( cmd->char_groups[sd->group_pos] == 0 )
@@ -8517,9 +8522,14 @@ static void atcommand_commands_sub(struct map_session_data* sd, const int fd, At
                        default:
                                continue;
                }
-
-
-               slen = strlen(cmd->command);
+               strcpy(commands_li[count]->command,cmd->command);
+               count++;
+       }
+       dbi_destroy(iter);
+      
+       qsort(commands_li,count,sizeof(struct AtCommandInfo),compare_atc);
+       for(i=0; i<count; i++){
+               unsigned int slen = strlen(commands_li[i]->command);
 
                // flush the text buffer if this command won't fit into it
                if (slen + cur - line_buff >= CHATBOX_SIZE) {
@@ -8528,13 +8538,10 @@ static void atcommand_commands_sub(struct map_session_data* sd, const int fd, At
                        memset(line_buff,' ',CHATBOX_SIZE);
                        line_buff[CHATBOX_SIZE-1] = 0;
                }
-
-               memcpy(cur,cmd->command,slen);
+               memcpy(cur,commands_li[i]->command,slen);
                cur += slen+(10-slen%10);
-
-               count++;
        }
-       dbi_destroy(iter);
+       aFree(commands_li);
        clif_displaymessage(fd,line_buff);
 
        sprintf(atcmd_output, msg_txt(sd,274), count); // "%d commands found."
@@ -9307,7 +9314,7 @@ void atcommand_basecommands(void) {
                ACMD_DEF(langtype),
        };
        AtCommandInfo* atcommand;
-       int i;
+       int i = 0;
 
        for( i = 0; i < ARRAYLENGTH(atcommand_base); i++ ) {
                if(atcommand_exists(atcommand_base[i].command)) { // Should not happen if atcommand_base[] array is OK
 
Viewed 692 times, submitted by lighta.