命令ハッシュ表周辺の調整
[YACASL2.git] / test / unit / print_cmdtype_code / print_cmdtype_code.c
1 #include "casl2.h"
2 #include "cerr.h"
3 #include <string.h>
4
5 int compare_code(const void *a, const void *b)
6 {
7     const CMDCODEARRAY ca = **(const CMDCODEARRAY **)a;
8     const CMDCODEARRAY cb = **(const CMDCODEARRAY **)b;
9     int diff;
10     if((diff = strcmp(ca.cmd, cb.cmd)) == 0) {
11         return ca.type - cb.type;
12     } else {
13         return diff;
14     }
15 }
16
17 /* 命令と命令タイプがキーのハッシュ表を表示する */
18 void print_cmdtype_code()
19 {
20     int i, j = 0;
21     CMDCODETAB *np;
22     CMDCODEARRAY **ar;
23     ar = malloc(sizeof(*ar) * cmdcodesize);
24     for(i = 0; i < cmdtabsize; i++) {
25         np = cmdtype_code[i];
26         while(np != NULL) {
27             ar[j++] = np->cca;
28             np = np->next;
29         }
30     }
31     qsort(ar, cmdcodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code);
32     for(i = 0; i < cmdcodesize; i++) {
33         fprintf(stdout, "%s\t0%02o\t#%04X\n", ar[i]->cmd, ar[i]->type, ar[i]->code);
34     }
35 }
36
37 int main()
38 {
39     create_cmdtype_code();
40     print_cmdtype_code();
41     free_cmdtype_code();
42     if(cerrno != 0) {
43         printf("\terror - %d: %s\n", cerrno, cerrmsg);
44         freecerr();
45         exit(-1);
46     }
47     return 0;
48 }