From a3c8dd9de99c7c77ad8218c3767abd4475c3dab6 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 7 Apr 2014 00:04:47 +0200
Subject: [PATCH] Minor refactoring in file parsing.

---
 src/client/command_db.c     |  6 +++---
 src/client/interface_conf.c | 12 ++++--------
 src/common/parse_file.c     |  9 +++++++++
 src/common/parse_file.h     |  3 +++
 src/server/configfile.c     | 10 ++++------
 5 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/src/client/command_db.c b/src/client/command_db.c
index ef84c49..9e99f5e 100644
--- a/src/client/command_db.c
+++ b/src/client/command_db.c
@@ -7,7 +7,8 @@
 #include <stdlib.h> /* free() */
 #include <string.h> /* memset(), strcmp(), strdup() */
 #include "../common/parse_file.h" /* EDIT_STARTED, parse_file(), set_val(),
-                                   * token_from_line(), err_line()
+                                   * token_from_line(), err_line(),
+                                   * finalize_by_readyflag()
                                    */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "array_append.h" /* array_append() */
@@ -48,8 +49,7 @@ static void tokens_into_entries(char * token0, char * token1)
     static struct Command * cmd = NULL;
     if (!token0 || !strcmp(token0, str_cmd))
     {
-        char * err_fin = "Last definition block not finished yet.";
-        err_line((cmd_flags & READY_CMD) ^ READY_CMD, err_fin);
+        finalize_by_readyflag(&cmd_flags, READY_CMD);
         if (cmd)
         {
             array_append(world.commandDB.n, sizeof(struct Command),
diff --git a/src/client/interface_conf.c b/src/client/interface_conf.c
index 69ef9eb..afc3ede 100644
--- a/src/client/interface_conf.c
+++ b/src/client/interface_conf.c
@@ -10,7 +10,7 @@
 #include <string.h> /* memset(), strchr(), strcmp(), strdup(), strlen() */
 #include <unistd.h> /* optarg, getopt() */
 #include "../common/parse_file.h" /* EDIT_STARTED, parse_file(), set_val(),
-                                   * token_from_line()
+                                   * token_from_line(), finalize_by_readyflag()
                                    */
 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
                                   * try_fwrite()
@@ -187,14 +187,10 @@ static void tokens_into_entries(char * token0, char * token1)
     if (!token0 || !strcmp(token0, str_win) || !strcmp(token0, str_ord)
                 || !strcmp(token0, str_kbd))
     {
-        err_line(   ((win_flags & READY_WIN) ^ READY_WIN)
-                 || ((ord_flags & READY_ORD) ^ READY_ORD)
-                 || ((kbd_flags & READY_KBD) ^ READY_KBD),
-                 "Last definition block not yet finished yet.");
+        finalize_by_readyflag(&win_flags, READY_WIN);
+        finalize_by_readyflag(&ord_flags, READY_ORD);
+        finalize_by_readyflag(&kbd_flags, READY_KBD);
         write_if_win(&win);
-        ord_flags = READY_ORD;
-        win_flags = READY_WIN;
-        kbd_flags = READY_KBD;
     }
     if (!token0)
     {
diff --git a/src/common/parse_file.c b/src/common/parse_file.c
index 9e55d16..3632f40 100644
--- a/src/common/parse_file.c
+++ b/src/common/parse_file.c
@@ -208,3 +208,12 @@ extern uint8_t set_val(char * token0, char * token1, char * comparand,
     }
     return 0;
 }
+
+
+
+extern void finalize_by_readyflag(uint8_t * flags, uint8_t ready_flag)
+{
+    char * err_fin = "Last definition block not finished yet.";
+    err_line((*flags & ready_flag) ^ ready_flag, err_fin);
+    *flags = ready_flag;
+}
diff --git a/src/common/parse_file.h b/src/common/parse_file.h
index b10f3f3..5c002fc 100644
--- a/src/common/parse_file.h
+++ b/src/common/parse_file.h
@@ -52,6 +52,9 @@ extern uint8_t set_val(char * token0, char * token1, char * comparand,
                        uint8_t * flags, uint8_t set_flag, char type,
                        char * element);
 
+/* Check "ready_flag" is set in "flags", re-set "flags" to "ready_flag" only. */
+extern void finalize_by_readyflag(uint8_t * flags, uint8_t ready_flag);
+
 
 
 #endif
diff --git a/src/server/configfile.c b/src/server/configfile.c
index 38404bd..026f658 100644
--- a/src/server/configfile.c
+++ b/src/server/configfile.c
@@ -6,7 +6,8 @@
 #include <stdlib.h> /* atoi(), free() */
 #include <string.h> /* strcmp() */
 #include "../common/parse_file.h" /* EDIT_STARTED, set_val(), test_for_int(),
-                                   * err_line(), parse_file(),token_from_line()
+                                   * err_line(), parse_file(),token_from_line(),
+                                   * finalize_by_readyflag()
                                    */
 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
@@ -110,13 +111,10 @@ static void tokens_into_entries(char * token0, char * token1)
     static struct EntryHead * mod = NULL;
     if (!token0 || !strcmp(token0,str_act) || !strcmp(token0,str_obj))
     {
-        err_line(   ((action_flags & READY_ACT) ^ READY_ACT)
-                 || ((object_flags & READY_OBJ) ^ READY_OBJ),
-                 "Last definitino block not finished yet.");
+        finalize_by_readyflag(&action_flags, READY_ACT);
+        finalize_by_readyflag(&object_flags, READY_OBJ);
         write_if_entry(&moa, (struct EntryHead ***) &moa_p_p);
         write_if_entry(&mod, (struct EntryHead ***) &mod_p_p);
-        action_flags = READY_ACT;
-        object_flags = READY_OBJ;
     }
     err_line(token0 && NULL != token_from_line(NULL), "Too many values.");
     if (   token0
-- 
2.30.2