home · contact · privacy
Load last world state from save file, not from re-stepping record file.
[plomrogue] / src / common / parse_file.c
index d6c6949af396ab8884a5e787e5881823564e3b59..8ce20ad55db79c7369225134e73d2f09a5d2b42a 100644 (file)
 
 #define _POSIX_C_SOURCE 200809L /* strdup() */
 #include "parse_file.h"
-#include <stddef.h> /* NULL */ // size_t
-#include <stdio.h> /* FILE, sprintf() */
-#include <stdint.h> /* uint8_t, uint32_t */
-#include <stdlib.h> /* free() */ // atoi()
-#include <string.h> /* strchr, strdup(), strlen() */ // strcmp()
+#include <stddef.h> /* size_t, NULL */
+#include <stdio.h> /* FILE, snprintf() */
+#include <stdint.h> /* int16_t,uint8_t,uint32_t, INT16_MIN, UINT{8,16,32}_MAX */
+#include <stdlib.h> /* atoi(), free() */
+#include <string.h> /* strchr, strcmp(), strdup(), strlen() */
 #include <unistd.h> /* access(), F_OK */
-#include "err_try_fgets.h" /* err_line(), err_try_fgets(),
-                            * reset_err_try_fgets_counter()
-                            */
-#include "readwrite.h" /* try_fopen(),try_fclose(), textfile_width() */
-#include "rexit.h" /* exit_err() */
+#include "readwrite.h" /* try_fopen(), try_fclose(), textfile_width() */
+#include "rexit.h" /* exit_err(), exit_trouble() */
 #include "try_malloc.h" /* try_malloc() */
 
 
 
-/* Return next token from "line" or NULL if none is found. Tokens either a)
- * start at the first non-whitespace character and end before the next
- * whitespace character after that; or b) if the first non-whitespace character
- * is a single quote followed by at least one other single quote some time later
- * on the line, the token starts after that first single quote and ends before
- * the second, with the next token_from_line() call starting its token search
- * after that second quote. The only way to return an empty string (instead of
- * NULL) as a token is to delimit the token by two succeeding single quotes.
- * */
-static char * token_from_line(char * line);
+/* Set by parse_file(), helps err_line() deciding what to do/output on error. */
+static uint32_t err_line_count = 0;
+static char * err_line_line = NULL;
+static char * err_line_intro = NULL;
+static uint8_t err_line_exit = 1;
+
+
 
 /* Determines the end of the token_from_line() token. */
 static void set_token_end(char ** start, char ** limit_char);
 
 
 
-static char * token_from_line(char * line)
+static void set_token_end(char ** start, char ** limit_char)
+{
+    char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
+    * start = (end_quote) ? * start + 1 : *start;
+    if (end_quote)
+    {
+        * end_quote = '\0';
+        * limit_char = end_quote;
+        return;
+    }
+    char * space = strchr(*start, ' ');
+    char * tab   = strchr(*start, '\t');
+    space = (!space || (tab && tab < space)) ? tab : space;
+    if (space)
+    {
+        * space = '\0';
+    }
+    *limit_char = strchr(*start, '\0');
+}
+
+
+
+extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
+{
+    char * f_name = "read_new_config_file()";
+    char * prefix = "Failed reading config file: \"";
+    char * affix = "\". ";
+    size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
+    char * errline_intro = try_malloc(size, f_name);
+    int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix);
+    exit_trouble(test < 0, f_name, "snprintf()");
+    exit_err(access(path, F_OK), errline_intro);
+    FILE * file = try_fopen(path, "r", f_name);
+    uint32_t linemax = textfile_width(file);
+    char * errline_line = try_malloc(linemax + 1, f_name);
+    set_err_line_options(errline_intro, errline_line, 0, 1);
+    err_line(0 == linemax, "File is empty.");
+    char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
+    char * token1 = NULL;
+    char * err_val = "No value given.";
+    while (try_fgets(errline_line, linemax + 1, file, f_name))
+    {
+        err_line_count++;
+        err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
+        char * line_copy = strdup(errline_line);
+        token0 = token_from_line(line_copy);
+        if (token0)
+        {
+            err_line(0 == (token1 = token_from_line(NULL)), err_val);
+            token_to_entry(token0, token1);
+            token0 = NULL;
+        }
+        free(line_copy);
+    }
+    token_to_entry(token0, token1);
+    try_fclose(file, f_name);
+    free(errline_line);
+    free(errline_intro);
+}
+
+
+
+extern void set_err_line_options(char * intro, char * line, uint32_t count,
+                                 uint8_t exit)
+{
+    err_line_count = count;
+    err_line_line = line;
+    err_line_intro = intro;
+    err_line_exit = exit;
+}
+
+
+
+extern uint8_t err_line(uint8_t test, char * msg)
+{
+    if (!test)
+    {
+        return 0;
+    }
+    char * f_name = "err_line()";
+    char * prefix = " Offending line ";
+    char * affix = ":\n";
+    size_t size =   strlen(err_line_intro) + strlen(msg) + strlen(prefix)
+                  + 10                 /* strlen for uint32_t representations */
+                  + strlen(affix) + strlen(err_line_line) + 1;
+    char * err = try_malloc(size, f_name);
+    int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
+                       err_line_count, affix, err_line_line);
+    exit_trouble(ret < 0, f_name, "snprintf()");
+    if (err_line_exit)
+    {
+        exit_err(1, err);
+    }
+    exit_trouble(0 > printf("%s\n", err), f_name, "printf()");
+    exit_trouble(EOF == fflush(stdout), f_name, "fflush()");
+    free(err);
+    return 1;
+}
+
+
+
+extern char * token_from_line(char * line)
 {
     static char * final_char = NULL;
     static char * limit_char = NULL;
@@ -48,6 +143,10 @@ static char * token_from_line(char * line)
             *(--final_char) = '\0';
         }
     }
+    if (final_char < start)
+    {
+        return NULL;
+    }
     uint8_t empty = 1;
     uint32_t i;
     for (i = 0; '\0' != start[i]; i++)
@@ -61,7 +160,7 @@ static char * token_from_line(char * line)
     }
     if (empty)
     {
-        return start = NULL;
+        return NULL;
     }
     set_token_end(&start, &limit_char);
     return start;
@@ -69,74 +168,123 @@ static char * token_from_line(char * line)
 
 
 
-static void set_token_end(char ** start, char ** limit_char)
+extern uint8_t parsetest_int(char * string, char type)
 {
-    char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL;
-    *start = (end_quote) ? *start + 1 : *start;
-    if (end_quote)
-    {
-        *end_quote = '\0';
-        *limit_char = end_quote;
-        return;
-    }
-    char * space = strchr(*start, ' ');
-    char * tab   = strchr(*start, '\t');
-    space = (!space || (tab && tab < space)) ? tab : space;
-    if (space)
+    char * err_8 = "Value must represent proper unsigned 8 bit integer.";
+    char * err_i = "Value must represent proper signed 16 bit integer.";
+    char * err_u = "Value must represent proper unsigned 16 bit integer.";
+    char * err_U = "Value must represent proper unsigned 32 bit integer.";
+    char * err = ('8' == type) ? err_8 : err_U;
+    err = ('i' == type) ? err_i : err;
+    err = ('u' == type) ? err_u : err;
+    uint8_t ret = err_line(strlen(string) < 1, err);
+    uint8_t i;
+    uint8_t test;
+    for (i = 0; '\0' != string[i]; i++)
     {
-        * space = '\0';
+        char * err_many = "Value of too many characters.";
+        ret = ret + err_line(string[i + 1] && UINT8_MAX == i, err_many);
+        test = (   (0 == i && ('-' == string[i] || '+' == string[i]))
+                || ('0' <= string[i]  && string[i] <= '9'));
+        ret = ret + err_line(!test, err);
     }
-    *limit_char = strchr(*start, '\0');
+    ret = ret + err_line(   strlen(string) < 2
+                         && ('-' == string[i] || '+' == string[i]), err);
+    test =     (   '8' == type
+                && (   strlen(string) > 4
+                    || atoi(string) < 0 || atoi(string) > UINT8_MAX))
+            || (   'i' == type
+                && (   strlen(string) > 6
+                    || atol(string) < INT16_MIN || atol(string) > INT16_MAX))
+            || (   'u' == type
+                && (   strlen(string) > 6
+                    || atoll(string) < 0 || atol(string) > UINT16_MAX))
+            || (   'U' == type
+                && (   strlen(string) > 11
+                    || atoll(string) < 0 || atoll(string) > UINT32_MAX));
+    ret = ret + err_line(test, err);
+    return ret;
 }
 
 
 
-extern void set_uint8(struct Context * context, uint8_t * target)
+extern void parsetest_defcontext(uint8_t flags)
 {
-    char * err_uint8 = "Value not unsigned decimal number between 0 and 255.";
-    uint8_t i;
-    uint8_t is_uint8 = 1;
-    for (i = 0; '\0' != context->token1[i]; i++)
-    {
-        if (i > 2 || '0' > context->token1[i] || '9' < context->token1[i])
-        {
-            is_uint8 = 0;
-        }
-    }
-    if (is_uint8 && atoi(context->token1) > UINT8_MAX)
-    {
-        is_uint8 = 0;
-    }
-    err_line(!(is_uint8), context->line, context->err_pre, err_uint8);
-    *target = atoi(context->token1);
+    err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
+}
+
+
+
+extern uint8_t parsetest_singlechar(char * string)
+{
+    return err_line(1 !=strlen(string),"Value must be single ASCII character.");
+}
+
+
+
+extern void parsetest_too_many_values()
+{
+    err_line(NULL != token_from_line(NULL), "Too many values.");
 }
 
 
 
-extern uint8_t set_val(struct Context * context, char * comparand,
-                       uint8_t * flags, uint8_t set_flag, char type,
-                       char * element)
+extern void parse_id_uniq(int test)
 {
-    char * err_out = "Outside appropriate definition's context.";
-    char * err_singlechar = "Value must be single ASCII character.";
-    if (!strcmp(context->token0, comparand))
+    err_line(0 != test, "Declaration of ID already used.");
+}
+
+
+
+extern void parse_unknown_arg()
+{
+    err_line(1, "Unknown argument.");
+}
+
+
+
+extern char * parse_init_entry(uint8_t * flags, size_t size)
+{
+    char * f_name = "parse_init_entry()";
+    *flags = EDIT_STARTED;
+    char * p = try_malloc(size, f_name);
+    memset(p, 0, size);
+    return p;
+}
+
+
+
+extern uint8_t parse_val(char * token0, char * token1, char * comparand,
+                         char type, char * element)
+{
+    if (!strcmp(token0, comparand))
     {
-        err_line(!(* flags & EDIT_STARTED),
-                 context->line, context->err_pre, err_out);
-        * flags = * flags | set_flag;
         if      ('s' == type)
         {
-            * (char **) element = strdup(context->token1);
+            * (char **) element = strdup(token1);
         }
-        else if ('c' == type)
+        else if ('c' == type && !parsetest_singlechar(token1))
         {
-            err_line(1 != strlen(context->token1),
-                     context->line, context->err_pre, err_singlechar);
-            * element = (context->token1)[0];
+            *element = (token1)[0];
         }
-        else if ('8' == type)
+        else if (!parsetest_int(token1, type))
         {
-            set_uint8(context, (uint8_t *) element);
+            if ('8' == type)
+            {
+                * (uint8_t *) element = atoi(token1);
+            }
+            else if ('i' == type)
+            {
+                * (int16_t *) element = atoi(token1);
+            }
+            else if ('u' == type)
+            {
+                * (uint16_t *) element = atol(token1);
+            }
+            else if ('U' == type)
+            {
+                * (uint32_t *) element = atoll(token1);
+            }
         }
         return 1;
     }
@@ -145,41 +293,24 @@ extern uint8_t set_val(struct Context * context, char * comparand,
 
 
 
-extern void parse_file(char * path, void (* token_to_entry) (struct Context *))
+extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
+                             uint8_t * flags, uint8_t set_flag, char type,
+                             char * element)
 {
-    char * f_name = "read_new_config_file()";
-    struct Context context;
-    char * err_pre_prefix = "Failed reading config file: \"";
-    char * err_pre_affix = "\". ";
-    context.err_pre = try_malloc(strlen(err_pre_prefix) + strlen(path)
-                                 + strlen(err_pre_affix) + 1, f_name);
-    sprintf(context.err_pre, "%s%s%s", err_pre_prefix, path, err_pre_affix);
-    exit_err(access(path, F_OK), context.err_pre);
-    FILE * file = try_fopen(path, "r", f_name);
-    uint32_t linemax = textfile_width(file);
-    context.line = try_malloc(linemax + 1, f_name);
-    reset_err_try_fgets_counter();
-    err_line(0 == linemax, context.line, context.err_pre, "File is empty.");
-    context.token0 = NULL;      /* For token_to_entry() if while() stagnates. */
-    char * err_val = "No value given.";
-    char * err_many = "Too many values.";
-    while (err_try_fgets(context.line, linemax + 1, file, context.err_pre, ""))
+    if (parse_val(token0, token1, comparand, type, element))
     {
-        char * line_copy = strdup(context.line);
-        context.token0 = token_from_line(line_copy);
-        if (context.token0)
-        {
-            err_line(0 == (context.token1 = token_from_line(NULL)),
-                     context.line, context.err_pre, err_val);
-            err_line(NULL != token_from_line(NULL),
-                     context.line, context.err_pre, err_many);
-            token_to_entry(&context);
-            context.token0 = NULL;
-        }
-        free(line_copy);
+        parsetest_defcontext(*flags);
+        *flags = *flags | set_flag;
+        return 1;
     }
-    token_to_entry(&context);
-    try_fclose(file, f_name);
-    free(context.line);
-    free(context.err_pre);
+    return 0;
+}
+
+
+
+extern void parse_and_reduce_to_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;
 }