home · contact · privacy
Mostly cosmetic changes to various file reading/writing functions for greater readibi...
[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_o = "Trouble in init_cmds() with fopen() on file 'commands'.";
81     char * err_s = "Trouble in init_cmds() with textfile_sizes().";
82     char * err_m = "Trouble in init_cmds() with malloc().";
83     char * err_c = "Trouble in init_cmds() with fclose() on file 'commands'.";
84
85     char * path = "config/commands";
86     FILE * file = fopen(path, "r");
87     exit_err(NULL == file, world, err_o);
88     uint16_t lines, linemax;
89     exit_err(textfile_sizes(file, &linemax, &lines), world, err_s);
90
91     char * line = malloc(linemax);
92     exit_err(NULL == line, world, err_m);
93     struct Command * cmds = malloc(lines * sizeof(struct Command));
94     exit_err(NULL == line, world, err_m);
95     uint8_t i = 0;
96     while (fgets(line, linemax, file))
97     {
98         cmds[i].id = atoi(strtok(line, " "));
99         copy_tokenized_string(world, &cmds[i].dsc_short, " ", err_m);
100         copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err_m);
101         i++;
102     }
103     free(line);
104     exit_err(fclose(file), world, err_c);
105
106     world->cmd_db = malloc(sizeof(struct CommandDB));
107     world->cmd_db->cmds = cmds;
108     world->cmd_db->n = lines;
109 }
110
111
112
113 extern void free_command_db(struct World * world)
114 {
115     uint8_t i = 0;
116     while (i < world->cmd_db->n)
117     {
118         free(world->cmd_db->cmds[i].dsc_short);
119         free(world->cmd_db->cmds[i].dsc_long);
120         i++;
121     }
122     free(world->cmd_db->cmds);
123     free(world->cmd_db);
124 }