home · contact · privacy
Made sure client is not confused by newlines at the end of config files.
[plomrogue] / src / client / command_db.c
1 /* src/client/command_db.c */
2
3 #include "command_db.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint32_t */
6 #include <stdio.h> /* FILE */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* memcpy(), strlen(), strtok(), strcmp() */
9 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets()
10                                   * textfile_sizes()
11                                   */
12 #include "../common/rexit.h" /* for exit_err() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "world.h" /* global world */
16
17
18
19 /* Point "ch_ptr" to next strtok() string in "line" delimited by "delim".*/
20 static void copy_tokenized_string(char * line, char ** ch_ptr, char * delim);
21
22
23
24 static void copy_tokenized_string(char * line, char ** ch_ptr, char * delim)
25 {
26     char * f_name = "copy_tokenized_string()";
27     char * dsc_ptr = strtok(line, delim);
28     * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
29     memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
30 }
31
32
33
34 extern struct Command * get_command(char * dsc_short)
35 {
36     struct Command * cmd_ptr = world.commandDB.cmds;
37     uint8_t i = 0;
38     while (i < world.commandDB.n)
39     {
40         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
41         {
42             break;
43         }
44         cmd_ptr = &cmd_ptr[1];
45         i++;
46     }
47     char * err_start = "get_command_data() failed on request for: ";
48     char err[strlen(err_start) + strlen(dsc_short) + 1];
49     sprintf(err, "%s%s", err_start, dsc_short);
50     exit_err(i == world.commandDB.n, err);
51     return cmd_ptr;
52 }
53
54
55
56 extern void init_command_db()
57 {
58     char * f_name = "init_command_db()";
59     FILE * file = try_fopen(world.path_commands, "r", f_name);
60     uint32_t lines;
61     uint32_t linemax = textfile_sizes(file, &lines);
62     char line[linemax + 1];
63     uint8_t i = 0;
64     char * delim = " ";
65     while (try_fgets(line, linemax + 1, file, f_name))
66     {
67         if ('\n' == line[0] || 0 == line[0])
68         {
69             break;
70         }
71         struct Command cmd;
72         copy_tokenized_string(line, &cmd.dsc_short, delim);
73         copy_tokenized_string(NULL, &cmd.server_msg, delim);
74         if (!strcmp("0", cmd.server_msg))
75         {                          /* A .server_msg == NULL helps control.c's */
76             free(cmd.server_msg);  /* try_key() and try_server_command() to   */
77             cmd.server_msg = NULL; /* differentiate server commands from      */
78         }                          /* non-server commands.                    */
79         char * arg_string = strtok(NULL, delim);
80         cmd.arg = arg_string[0];
81         copy_tokenized_string(NULL, &cmd.dsc_long, "\n");
82         uint32_t old_size = i * sizeof(struct Command);
83         uint32_t new_size = old_size + sizeof(struct Command);
84         struct Command * new_cmds = try_malloc(new_size, f_name);
85         memcpy(new_cmds, world.commandDB.cmds, old_size);
86         new_cmds[i] = cmd;
87         free(world.commandDB.cmds);
88         world.commandDB.cmds = new_cmds;
89         i++;
90     }
91     try_fclose(file, f_name);
92     world.commandDB.n = i;
93     set_cleanup_flag(CLEANUP_COMMANDS);
94 }
95
96
97
98 extern void free_command_db()
99 {
100     uint8_t i = 0;
101     while (i < world.commandDB.n)
102     {
103         free(world.commandDB.cmds[i].dsc_short);
104         free(world.commandDB.cmds[i].dsc_long);
105         free(world.commandDB.cmds[i].server_msg);
106         i++;
107     }
108     free(world.commandDB.cmds);
109 }