home · contact · privacy
Strongly overhauled keybinding managemment. Window-specific keybindings and a window...
[plomrogue] / src / command_db.c
index d5af5c27f8bdf8f6944f384f790e931168bc3d45..d89319df70014332f1a663e6fcf3abaabec7d782 100644 (file)
@@ -1,25 +1,24 @@
 /* command.c */
 
 #include "command_db.h"
-#include <stdlib.h> /* for malloc() */
-#include <stdio.h> /* for FILE typedef, fopen(), fclose(), fgets() */
+#include <stdlib.h> /* for free() */
+#include <stdio.h> /* for FILE typedef, fgets() */
 #include <stdint.h> /* for uint8_t */
 #include <string.h> /* for strlen(), strtok() */
 #include "main.h" /* for World struct */
 #include "rexit.h" /* for exit_err() */
-#include "misc.h" /* for textfile_sizes() */
+#include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose() */
+#include "misc.h" /* for try_malloc() */
 
 
 
-/* Build string pointed to by "ch_ptr" from next token delimited by "delim",
- * exit_err() with "err" as error message on malloc() failure.
- */
-static void copy_tokenized_string(struct World * world,
-                                 char ** ch_ptr, char * delim, char * err)
+/* Build string pointed to by "ch_ptr" from next token delimited by "delim". */
+static void copy_tokenized_string(struct World * world, char ** ch_ptr,
+                                  char * delim)
 {
+    char * f_name = "copy_tokenized_string()";
     char * dsc_ptr = strtok(NULL, delim);
-    * ch_ptr = malloc(strlen(dsc_ptr) + 1);
-    exit_err(NULL == * ch_ptr, world, err);
+    * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, world, f_name);
     memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
 }
 
@@ -77,31 +76,32 @@ extern char * get_command_longdsc(struct World * world, char * dsc_short)
 
 extern void init_command_db(struct World * world)
 {
-    char * err_o = "Trouble in init_cmds() with fopen() on file 'commands'.";
+    char * f_name = "init_command_db()";
     char * err_s = "Trouble in init_cmds() with textfile_sizes().";
-    char * err_m = "Trouble in init_cmds() with malloc().";
-    char * err_c = "Trouble in init_cmds() with fclose() on file 'commands'.";
 
     char * path = "config/commands";
-    FILE * file = fopen(path, "r");
-    exit_err(NULL == file, world, err_o);
+    FILE * file = try_fopen(path, "r", world, f_name);
     uint16_t lines, linemax;
     exit_err(textfile_sizes(file, &linemax, &lines), world, err_s);
+    char line[linemax + 1];
 
-    char line[linemax];
-    struct Command * cmds = malloc(lines * sizeof(struct Command));
-    exit_err(NULL == line, world, err_m);
+    struct Command * cmds = try_malloc(lines * sizeof(struct Command),
+                                       world, f_name);
     uint8_t i = 0;
-    while (fgets(line, linemax, file))
+    while (fgets(line, linemax + 1, file))
     {
+        if ('\n' == line[0] || 0 == line[0])
+        {
+            break;
+        }
         cmds[i].id = atoi(strtok(line, " "));
-        copy_tokenized_string(world, &cmds[i].dsc_short, " ", err_m);
-        copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err_m);
+        copy_tokenized_string(world, &cmds[i].dsc_short, " ");
+        copy_tokenized_string(world, &cmds[i].dsc_long, "\n");
         i++;
     }
-    exit_err(fclose(file), world, err_c);
+    try_fclose(file, world, f_name);
 
-    world->cmd_db = malloc(sizeof(struct CommandDB));
+    world->cmd_db = try_malloc(sizeof(struct CommandDB), world, f_name);
     world->cmd_db->cmds = cmds;
     world->cmd_db->n = lines;
 }