home · contact · privacy
25fcd24e90076da9ca8c5c75689c7b487525a6f3
[plomrogue] / src / command_db.c
1 /* command.c */
2
3 #include "command_db.h"
4 #include <stdlib.h> /* for malloc() */
5 #include <stdio.h> /* for FILE typedef, fopen(), fclose(), fgets() */
6 #include <stdint.h> /* for uint8_t */
7 #include <string.h> /* for strlen(), strtok() */
8 #include "main.h" /* for World struct */
9 #include "rexit.h" /* for exit_err() */
10 #include "misc.h" /* for textfile_sizes() */
11
12
13
14 /* Build string pointed to by "ch_ptr" from next token delimited by "delim",
15  * exit_err() with "err" as error message on malloc() failure.
16  */
17 static void copy_tokenized_string(struct World * world,
18                                  char ** ch_ptr, char * delim, char * err)
19 {
20     char * dsc_ptr = strtok(NULL, delim);
21     * ch_ptr = malloc(strlen(dsc_ptr) + 1);
22     exit_err(NULL == * ch_ptr, world, err);
23     memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
24 }
25
26
27
28 extern uint8_t is_command_id_shortdsc(struct World * world,
29                                       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(struct World * world, 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(struct World * world, 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(struct World * world)
79 {
80     char * err = "Trouble in init_cmds() with fopen() on file 'commands'.";
81     FILE * file = fopen("config/commands", "r");
82     exit_err(NULL == file, world, err);
83     uint16_t lines, linemax;
84     err = "Trouble in init_cmds() with textfile_sizes().";
85     exit_err(textfile_sizes(file, &linemax, &lines), world, err);
86     err = "Trouble in init_cmds() with malloc().";
87     char * line = malloc(linemax);
88     exit_err(NULL == line, world, err);
89     struct Command * cmds = malloc(lines * sizeof(struct Command));
90     exit_err(NULL == line, world, err);
91     uint8_t i = 0;
92     while (fgets(line, linemax, file))
93     {
94         cmds[i].id = atoi(strtok(line, " "));
95         copy_tokenized_string(world, &cmds[i].dsc_short, " ", err);
96         copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err);
97         i++;
98     }
99     free(line);
100     world->cmd_db = malloc(sizeof(struct CommandDB));
101     world->cmd_db->cmds = cmds;
102     world->cmd_db->n = lines;
103     err = "Trouble in init_cmds() with fclose() on file 'commands'.";
104     exit_err(fclose(file), world, err);
105 }
106
107
108
109 extern void free_command_db(struct World * world)
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 }