foxygit / doom Log in
commit 5f3eb6fee3165a44ca032c9642130dddb2128e1d
Author:     Fabian Greffrath <fabian@greffrath.com>
AuthorDate: Thu Sep 11 12:37:28 2014 +0200
Commit:     Fabian Greffrath <fabian@greffrath.com>
CommitDate: Thu Sep 11 12:37:28 2014 +0200

    improvements to the [STRINGS] section parser

    - restructure DEH_ReadLine() to avoid use of "goto"
    - bex_string_t type name ends in "_t"
    - declare constant table as "static const"
    - add magic comment *allow-extended-strings* and corresponding
      variable deh_allow_extended_strings
    - fix logical error when no [STRINGS] section is registered to which
      corrent_section could be compared
---
 src/deh_io.c          | 31 ++++++++++++++++---------------
 src/deh_main.c        | 21 ++++++++++++++++++++-
 src/deh_main.h        |  1 +
 src/doom/deh_bexstr.c |  4 ++--
 4 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/src/deh_io.c b/src/deh_io.c
index a502a2d1..46f4c9fb 100644
--- a/src/deh_io.c
+++ b/src/deh_io.c
@@ -28,6 +28,7 @@

 #include "deh_defs.h"
 #include "deh_io.h"
+#include "deh_main.h"

 typedef enum
 {
@@ -229,6 +230,7 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)
 {
     int c;
     int pos;
+    boolean escaped = false;

     for (pos = 0;;)
     {
@@ -249,9 +251,8 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)
         }

         // extended string support
-        if (extended && c == '\\')
+        if (deh_allow_extended_strings && extended && c == '\\')
         {
-            escaped:
             c = DEH_GetChar(context);

             // "\n" in the middle of a string indicates an internal linefeed
@@ -266,22 +267,22 @@ char *DEH_ReadLine(deh_context_t *context, boolean extended)
             // each line that is to be continued with a backslash
             if (c == '\n')
             {
-                // blanks before the backslash are included in the string
-                // but indentation after the linefeed is not
-                do
-                {
-                    c = DEH_GetChar(context);
-                } while (isspace(c) && c != '\n');
-
-                // if the next non-space character after an escaped linefeed
-                // is another backslash, repeat again
-                if (c == '\\')
-                {
-                    goto escaped;
-                }
+                escaped = true;
+                continue;
             }
         }

+        // blanks before the backslash are included in the string
+        // but indentation after the linefeed is not
+        if (escaped && isspace(c) && c != '\n')
+        {
+            continue;
+        }
+        else
+        {
+            escaped = false;
+        }
+
         if (c == '\n')
         {
             // end of line: a full line has been read
diff --git a/src/deh_main.c b/src/deh_main.c
index 4ab9a8f3..55e26c70 100644
--- a/src/deh_main.c
+++ b/src/deh_main.c
@@ -33,6 +33,10 @@ extern char *deh_signatures[];

 static boolean deh_initialized = false;

+// If true, we can parse [STRINGS] sections in BEX format.
+
+boolean deh_allow_extended_strings = false;
+
 // If true, we can do long string replacements.

 boolean deh_allow_long_strings = false;
@@ -220,6 +224,16 @@ static void DEH_ParseComment(char *comment)
     {
         deh_allow_long_cheats = true;
     }
+
+    // Allow magic comments to allow parsing [STRINGS] section
+    // that are usually only found in BEX format files. This allows
+    // for substitution of map and episode names when loading
+    // Freedoom/FreeDM IWADs.
+
+    if (strstr(comment, "*allow-extended-strings*") != NULL)
+    {
+        deh_allow_extended_strings = true;
+    }
 }

 // Parses a dehacked file by reading from the context
@@ -230,6 +244,7 @@ static void DEH_ParseContext(deh_context_t *context)
     char section_name[20];
     void *tag = NULL;
     char *line;
+    deh_section_t *bexstr;

     // Read the header and check it matches the signature

@@ -242,9 +257,13 @@ static void DEH_ParseContext(deh_context_t *context)

     for (;;)
     {
+        // extended string support required?
+
+        bexstr = GetSectionByName("[STRINGS]");
+
         // read a new line

-        line = DEH_ReadLine(context, current_section == GetSectionByName("[STRINGS]"));
+        line = DEH_ReadLine(context, bexstr && current_section == bexstr);

         // end of file?

diff --git a/src/deh_main.h b/src/deh_main.h
index 9689d1c2..4b3f9d8a 100644
--- a/src/deh_main.h
+++ b/src/deh_main.h
@@ -39,6 +39,7 @@ boolean DEH_ParseAssignment(char *line, char **variable_name, char **value);

 void DEH_Checksum(sha1_digest_t digest);

+extern boolean deh_allow_extended_strings;
 extern boolean deh_allow_long_strings;
 extern boolean deh_allow_long_cheats;
 extern boolean deh_apply_cheats;
diff --git a/src/doom/deh_bexstr.c b/src/doom/deh_bexstr.c
index 5adc2fd6..e29b71dc 100644
--- a/src/doom/deh_bexstr.c
+++ b/src/doom/deh_bexstr.c
@@ -28,10 +28,10 @@
 typedef struct {
     char *macro;
     char *string;
-} bex_string;
+} bex_string_t;

 // mnemonic keys table
-static bex_string bex_stringtable[] = {
+static const bex_string_t bex_stringtable[] = {
     // part 1 - general initialization and prompts
     {"D_DEVSTR", D_DEVSTR},
     {"D_CDROM", D_CDROM},