home · contact · privacy
Minor refactoring in file parsing.
[plomrogue] / src / client / command_db.c
index 19cd4064e09c7878d49d841ed1e86d30536ffc58..9e99f5e6e93fb5aeb43b2910865999f27ed3bfc9 100644 (file)
@@ -1,54 +1,84 @@
 /* src/client/command_db.c */
 
+#define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "command_db.h"
 #include <stddef.h> /* NULL */
-#include <stdint.h> /* uint8_t, uint32_t */
-#include <stdio.h> /* FILE, sprintf() */
+#include <stdint.h> /* uint8_t */
 #include <stdlib.h> /* free() */
-#include <string.h> /* memcpy(), strlen(), strtok(), strcmp() */
-#include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets()
-                                  * textfile_sizes()
-                                  */
-#include "../common/rexit.h" /* for exit_err() */
+#include <string.h> /* memset(), strcmp(), strdup() */
+#include "../common/parse_file.h" /* EDIT_STARTED, parse_file(), set_val(),
+                                   * token_from_line(), err_line(),
+                                   * finalize_by_readyflag()
+                                   */
 #include "../common/try_malloc.h" /* try_malloc() */
-#include "cleanup.h" /* set_cleanup_flag() */
-#include "misc.h" /* array_append() */
+#include "array_append.h" /* array_append() */
 #include "world.h" /* global world */
+#include "cleanup.h" /* set_cleanup_flag() */
 
 
 
-/* Helpers to init_command_db(). */
-static uint8_t copy_tokenized_string(char * line, char ** ch_ptr, char * delim);
-static char * init_command_db_err(char * line_copy, uint8_t line_number);
+/* Flags for defining state of command DB config file entries. */
+enum cmd_flag
+{
+    DESC_SET      = 0x02,
+    SERVERCMD_SET = 0x04,
+    SERVERARG_SET = 0x08,
+    READY_CMD = DESC_SET
+};
 
 
-static uint8_t copy_tokenized_string(char * line, char ** ch_ptr, char * delim)
-{
-    char * f_name = "copy_tokenized_string()";
-    char * dsc_ptr = strtok(line, delim);
-    if (!dsc_ptr)
-    {
-        return 1;
-    }
-    * ch_ptr = try_malloc(strlen(dsc_ptr) + 1, f_name);
-    memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1);
-    return 0;
-}
+
+/* Get tokens from "context" and, by their order (in the individual context and
+ * in subsequent calls of this function), interpret them as data to write into
+ * the CommandDB.
+ *
+ * Individual CommandDB entries are put together line by line before being
+ * written. Writing happens after all necessary members of an entry have been
+ * assembled, and when additionally a) a new entry is started by a
+ * context->token0 of "COMMAND"; or b) a NULL context->token0 is passed.
+ */
+static void tokens_into_entries(char * token0, char * token1);
 
 
 
-static char * init_command_db_err(char * line_copy, uint8_t line_number)
+static void tokens_into_entries(char * token0, char * token1)
 {
-    char * f_name = "init_command_db_err";
-    char * err_start = "Failed reading command config file at ";
-    char * err_middle = " due to malformed line ";
-    line_copy[strlen(line_copy) - 1] = '\0';
-    char * err = try_malloc(strlen(err_start) + strlen(world.path_commands) +
-                            strlen(err_middle) + 3 + 2 + strlen(line_copy) + 1,
-                            f_name);
-    sprintf(err, "%s%s%s%d: %s", err_start, world.path_commands, err_middle,
-            line_number, line_copy);
-    return err;
+    char * f_name = "tokens_into_entries()";
+    char * str_cmd = "COMMAND";
+    static uint8_t cmd_flags = READY_CMD;
+    static struct Command * cmd = NULL;
+    if (!token0 || !strcmp(token0, str_cmd))
+    {
+        finalize_by_readyflag(&cmd_flags, READY_CMD);
+        if (cmd)
+        {
+            array_append(world.commandDB.n, sizeof(struct Command),
+                         (void *) cmd, (void **) &world.commandDB.cmds);
+            world.commandDB.n++;
+            free(cmd);
+            cmd = NULL;
+        }
+    }
+    err_line(token0 && NULL != token_from_line(NULL), "Too many values.");
+    if (token0 && !strcmp(token0, str_cmd))
+    {
+        char * err_uniq = "Declaration of ID already used.";
+        cmd_flags = EDIT_STARTED;
+        cmd = try_malloc(sizeof(struct Command), f_name);
+        memset(cmd, 0, sizeof(struct Command));
+        cmd->dsc_short = strdup(token1);
+        err_line(NULL != get_command(cmd->dsc_short), err_uniq);
+    }
+    else if (   token0
+             && !(   set_val(token0, token1, "DESCRIPTION", &cmd_flags,
+                             DESC_SET, 's', (char *) &cmd->dsc_long)
+                  || set_val(token0, token1, "SERVER_COMMAND", &cmd_flags,
+                             SERVERCMD_SET, 's', (char *) &cmd->server_msg)
+                  || set_val(token0, token1, "SERVER_ARGUMENT", &cmd_flags,
+                             SERVERARG_SET, 'c', (char *) &cmd->arg)))
+    {
+        err_line(1, "Unknown arguemnt.");
+    }
 }
 
 
@@ -61,57 +91,19 @@ extern struct Command * get_command(char * dsc_short)
     {
         if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
         {
-            break;
+            return cmd_ptr;
         }
         cmd_ptr = &cmd_ptr[1];
         i++;
     }
-    char * err_start = "get_command_data() failed on request for: ";
-    char err[strlen(err_start) + strlen(dsc_short) + 1];
-    sprintf(err, "%s%s", err_start, dsc_short);
-    exit_err(i == world.commandDB.n, err);
-    return cmd_ptr;
+    return NULL;
 }
 
 
 
 extern void init_command_db()
 {
-    char * f_name = "init_command_db()";
-    FILE * file = try_fopen(world.path_commands, "r", f_name);
-    uint32_t lines;
-    uint32_t linemax = textfile_sizes(file, &lines);
-    char line[linemax + 1];
-    uint8_t i = 0;
-    char * delim = " ";
-    while (try_fgets(line, linemax + 1, file, f_name))
-    {
-        if ('\n' == line[0] || 0 == line[0])
-        {
-            break;
-        }
-        char line_copy[strlen(line) + 1];
-        sprintf(line_copy, "%s", line);
-        struct Command cmd;
-        char * arg_string;
-        exit_err((  copy_tokenized_string(line, &cmd.dsc_short, delim)
-                 || copy_tokenized_string(NULL, &cmd.server_msg, delim)
-                 || NULL == (arg_string = strtok(NULL, delim))
-                 || strlen(arg_string) > 1
-                 || copy_tokenized_string(NULL, &cmd.dsc_long, "\n")),
-                 init_command_db_err(line_copy, i + 1));
-        cmd.arg = arg_string[0];
-        if (!strcmp("0", cmd.server_msg))
-        {                          /* A .server_msg == NULL helps control.c's */
-            free(cmd.server_msg);  /* try_key() and try_server_command() to   */
-            cmd.server_msg = NULL; /* differentiate server commands from      */
-        }                          /* non-server commands.                    */
-        array_append(i, sizeof(struct Command), (void *) &cmd,
-                     (void **) &world.commandDB.cmds);
-        i++;
-    }
-    try_fclose(file, f_name);
-    world.commandDB.n = i;
+    parse_file(world.path_commands, tokens_into_entries);
     set_cleanup_flag(CLEANUP_COMMANDS);
 }