home · contact · privacy
MAJOR re-write. Split plomrogue into a server and a client. Re-wrote large parts
[plomrogue] / src / client / command_db.c
1 /* src/client/command_db.c */
2
3 #include "command_db.h"
4 #include <stdint.h> /* uint8_t, uint32_t */
5 #include <stdio.h> /* FILE */
6 #include <stdlib.h> /* free() */
7 #include <string.h> /* memcpy(), strlen(), strtok(), strcmp() */
8 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets()
9                                   * textfile_sizes()
10                                   */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "cleanup.h" /* set_cleanup_flag() */
13 #include "world.h" /* global world */
14
15
16
17 /* Build string pointed to by "ch_ptr" from next token delimited by "delim". */
18 static void copy_tokenized_string(char ** ch_ptr, char * delim);
19
20
21
22 static void copy_tokenized_string(char ** ch_ptr, char * delim)
23 {
24     char * f_name = "copy_tokenized_string()";
25     char * dsc_ptr = strtok(NULL, delim);
26     * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
27     memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
28 }
29
30
31
32 extern uint8_t is_command_id_shortdsc(uint8_t id, char * shortdsc)
33 {
34     struct Command * cmd_ptr = world.cmd_db.cmds;
35     while (1)
36     {
37         if (id == cmd_ptr->id)
38         {
39             if (strcmp(shortdsc, cmd_ptr->dsc_short))
40             {
41                 return 0;
42             }
43             return 1;
44         }
45         cmd_ptr = &cmd_ptr[1];
46     }
47 }
48
49
50
51 extern uint8_t get_command_id(char * dsc_short)
52 {
53     struct Command * cmd_ptr = world.cmd_db.cmds;
54     while (1)
55     {
56         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
57         {
58             return cmd_ptr->id;
59         }
60         cmd_ptr = &cmd_ptr[1];
61     }
62 }
63
64
65
66 extern char * get_command_longdsc(char * dsc_short)
67 {
68     struct Command * cmd_ptr = world.cmd_db.cmds;
69     while (1)
70     {
71         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
72         {
73             return cmd_ptr->dsc_long;
74         }
75         cmd_ptr = &cmd_ptr[1];
76     }
77 }
78
79
80
81 extern void init_command_db()
82 {
83     char * f_name = "init_command_db()";
84     char * path = "confclient/commands";
85     FILE * file = try_fopen(path, "r", f_name);
86     uint32_t lines;
87     uint32_t linemax = textfile_sizes(file, &lines);
88     char line[linemax + 1];
89     world.cmd_db.cmds = try_malloc(lines * sizeof(struct Command), f_name);
90     uint8_t i = 0;
91     while (try_fgets(line, linemax + 1, file, f_name))
92     {
93         if ('\n' == line[0] || 0 == line[0])
94         {
95             break;
96         }
97         world.cmd_db.cmds[i].id = atoi(strtok(line, " "));
98         copy_tokenized_string(&world.cmd_db.cmds[i].dsc_short, " ");
99         copy_tokenized_string(&world.cmd_db.cmds[i].dsc_long, "\n");
100         i++;
101     }
102     try_fclose(file, f_name);
103     world.cmd_db.n = lines;
104     set_cleanup_flag(CLEANUP_COMMANDS);
105 }
106
107
108
109 extern void free_command_db()
110 {
111     uint8_t i = 0;
112     while (i < world.cmd_db.n)
113     {
114         free(world.cmd_db.cmds[i].dsc_short);
115         free(world.cmd_db.cmds[i].dsc_long);
116         i++;
117     }
118     free(world.cmd_db.cmds);
119 }