X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Fparse_file.c;h=ded2d5ad7c98bcf8200b4356d6b75e325e0c9ed8;hb=a5b35a816a4a52235aa5c56386193ebdf64619db;hp=d6c6949af396ab8884a5e787e5881823564e3b59;hpb=6245b251886853269262bc77d0f0378c28e241bc;p=plomrogue
diff --git a/src/common/parse_file.c b/src/common/parse_file.c
index d6c6949..ded2d5a 100644
--- a/src/common/parse_file.c
+++ b/src/common/parse_file.c
@@ -1,39 +1,109 @@
-/* src/common/parse_file.c */
-
-#define _POSIX_C_SOURCE 200809L /* strdup() */
+/* src/common/parse_file.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
+
+#define _POSIX_C_SOURCE 200809L /* strdup(), snprintf() */
#include "parse_file.h"
-#include /* NULL */ // size_t
-#include /* FILE, sprintf() */
-#include /* uint8_t, uint32_t */
-#include /* free() */ // atoi()
-#include /* strchr, strdup(), strlen() */ // strcmp()
-#include /* 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 /* size_t, NULL */
+#include /* FILE, snprintf() */
+#include /* int16_t,uint8_t,uint32_t, INT16_MIN, UINT{8,16,32}_MAX */
+#include /* atoi(), free() */
+#include /* strchr, strcmp(), strdup(), strlen() */
+#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 set_err_line_options(char * intro, char * line, uint8_t exit)
+{
+ err_line_line = line;
+ err_line_intro = intro;
+ err_line_exit = exit;
+}
+
+
+
+extern void err_line_inc()
+{
+ err_line_count++;
+ err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
+}
+
+
+
+extern void err_line_zero()
+{
+ err_line_count = 0;
+}
+
+
+
+extern uint8_t err_line(uint8_t test, char * msg)
+{
+ if (!test)
+ {
+ return 0;
+ }
+ char * prefix = " Offending line ";
+ char * affix = ": ";
+ 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, __func__);
+ 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, __func__, "snprintf");
+ if (err_line_exit)
+ {
+ exit_err(1, err);
+ }
+ exit_trouble(0 > printf("%s\n", err), __func__, "printf");
+ exit_trouble(EOF == fflush(stdout), __func__, "fflush");
+ free(err);
+ return 1;
+}
+
+
+
+extern char * token_from_line(char * line)
{
static char * final_char = NULL;
static char * limit_char = NULL;
@@ -48,6 +118,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 +135,7 @@ static char * token_from_line(char * line)
}
if (empty)
{
- return start = NULL;
+ return NULL;
}
set_token_end(&start, &limit_char);
return start;
@@ -69,117 +143,90 @@ 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)
+ 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++)
{
- *end_quote = '\0';
- *limit_char = end_quote;
- return;
+ 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);
}
- 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');
+ 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 uint8_t parsetest_singlechar(char * string)
{
- 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);
+ return err_line(1 !=strlen(string),"Value must be single ASCII character.");
}
-extern uint8_t set_val(struct Context * context, char * comparand,
- uint8_t * flags, uint8_t set_flag, char type,
- char * element)
+extern uint8_t parse_val(char * token0, char * token1, char * comparand,
+ char type, char * element)
{
- char * err_out = "Outside appropriate definition's context.";
- char * err_singlechar = "Value must be single ASCII character.";
- if (!strcmp(context->token0, comparand))
+ 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);
+ free(* (char **) element);
+ * (char **) element = strdup(token1);
}
else if ('c' == type)
{
- err_line(1 != strlen(context->token1),
- context->line, context->err_pre, err_singlechar);
- * element = (context->token1)[0];
+ if (!parsetest_singlechar(token1))
+ {
+ *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;
}
return 0;
}
-
-
-
-extern void parse_file(char * path, void (* token_to_entry) (struct Context *))
-{
- 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, ""))
- {
- 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);
- }
- token_to_entry(&context);
- try_fclose(file, f_name);
- free(context.line);
- free(context.err_pre);
-}