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