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