1 /* src/client/command_db.c */
3 #include "command_db.h"
4 #include <stdint.h> /* uint8_t, uint32_t */
5 #include <stdio.h> /* FILE */
6 #include <stdlib.h> /* free() */
7 #include <string.h> /* memcpy(), strlen(), strtok() */
8 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets()
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "cleanup.h" /* set_cleanup_flag() */
13 #include "world.h" /* global world */
17 /* Point "ch_ptr" to next strtok() string in "line" delimited by "delim".*/
18 static void copy_tokenized_string(char * line, char ** ch_ptr, char * delim);
22 static void copy_tokenized_string(char * line, char ** ch_ptr, char * delim)
24 char * f_name = "copy_tokenized_string()";
25 char * dsc_ptr = strtok(line, delim);
26 * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
27 memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
32 extern char * get_command_longdsc(char * dsc_short)
34 struct Command * cmd_ptr = world.cmd_db.cmds;
37 if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
39 return cmd_ptr->dsc_long;
41 cmd_ptr = &cmd_ptr[1];
47 extern void init_command_db()
49 char * f_name = "init_command_db()";
50 char * path = "confclient/commands";
51 FILE * file = try_fopen(path, "r", f_name);
53 uint32_t linemax = textfile_sizes(file, &lines);
54 char line[linemax + 1];
55 world.cmd_db.cmds = try_malloc(lines * sizeof(struct Command), f_name);
57 while (try_fgets(line, linemax + 1, file, f_name))
59 if ('\n' == line[0] || 0 == line[0])
63 copy_tokenized_string(line, &world.cmd_db.cmds[i].dsc_short, " ");
64 copy_tokenized_string(NULL, &world.cmd_db.cmds[i].dsc_long, "\n");
67 try_fclose(file, f_name);
68 world.cmd_db.n = lines;
69 set_cleanup_flag(CLEANUP_COMMANDS);
74 extern void free_command_db()
77 while (i < world.cmd_db.n)
79 free(world.cmd_db.cmds[i].dsc_short);
80 free(world.cmd_db.cmds[i].dsc_long);
83 free(world.cmd_db.cmds);