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