X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Fparse_file.c;h=ded2d5ad7c98bcf8200b4356d6b75e325e0c9ed8;hb=814778ca9bf76eeaf0520b7551103e818a9775d8;hp=9e55d16fa8eb53aa326d70220d34cbc683ec0df9;hpb=c53b42dfc7e4de104f9189428dd5b9a0d431c00a;p=plomrogue
diff --git a/src/common/parse_file.c b/src/common/parse_file.c
index 9e55d16..ded2d5a 100644
--- a/src/common/parse_file.c
+++ b/src/common/parse_file.c
@@ -1,23 +1,27 @@
-/* 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 /* 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 /* access(), F_OK */
-#include "readwrite.h" /* try_fopen(), try_fclose(), textfile_width() */
#include "rexit.h" /* exit_err(), exit_trouble() */
#include "try_malloc.h" /* try_malloc() */
-/* Set by parse_file(), used by err_line() for more informative messages. */
+/* 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;
@@ -48,63 +52,53 @@ static void set_token_end(char ** start, char ** limit_char)
-extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
+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()
{
- 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;
- err_line_intro = try_malloc(size, f_name);
- int test = snprintf(err_line_intro, size, "%s%s%s", prefix, path, affix);
- exit_trouble(test < 0, f_name, "snprintf()");
- exit_err(access(path, F_OK), err_line_intro);
- FILE * file = try_fopen(path, "r", f_name);
- uint32_t linemax = textfile_width(file);
- err_line_line = try_malloc(linemax + 1, f_name);
err_line_count = 0;
- err_line(0 == linemax, "File is empty.");
- char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
- char * token1;
- char * err_val = "No value given.";
- while (try_fgets(err_line_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(err_line_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(err_line_line);
- free(err_line_intro);
}
-extern void err_line(uint8_t test, char * msg)
+extern uint8_t err_line(uint8_t test, char * msg)
{
if (!test)
{
- return;
+ return 0;
}
- char * f_name = "err_line()";
char * prefix = " Offending line ";
- char * affix = ":\n";
+ 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, f_name);
+ 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, f_name, "snprintf()");
- exit_err(1, err);
+ 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;
}
@@ -124,6 +118,10 @@ extern 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++)
@@ -137,7 +135,7 @@ extern char * token_from_line(char * line)
}
if (empty)
{
- return start = NULL;
+ return NULL;
}
set_token_end(&start, &limit_char);
return start;
@@ -145,64 +143,88 @@ extern char * token_from_line(char * line)
-extern void test_for_int(char * string, char type)
+extern uint8_t parsetest_int(char * string, char type)
{
- char * err;
- if ('8' == type)
- {
- err = "Value must be proper representation of unsigned 8 bit integer.";
- }
- if ('i' == type)
- {
- err = "Value must be proper representation of signed 16 bit integer.";
- }
- err_line(strlen(string) < 1, err);
+ 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++)
{
char * err_many = "Value of too many characters.";
- err_line(string[i + 1] && UINT8_MAX == i, err_many);
+ 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'));
- err_line(!test, err);
+ ret = ret + err_line(!test, err);
}
- err_line(strlen(string) < 2 && ('-' == string[i] || '+' == string[i]), err);
- err_line('8'==type && (atoi(string) < 0 || atoi(string) > UINT8_MAX), err);
- test = 'i'==type && (atoi(string) < INT16_MIN || atoi(string) > INT16_MAX);
- err_line(test, err);
+ 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 uint8_t set_val(char * token0, char * token1, char * comparand,
- uint8_t * flags, uint8_t set_flag, char type,
- char * element)
+extern uint8_t parsetest_singlechar(char * string)
+{
+ return err_line(1 !=strlen(string),"Value must be single ASCII character.");
+}
+
+
+
+extern uint8_t parse_val(char * token0, char * token1, char * comparand,
+ char type, char * element)
{
if (!strcmp(token0, comparand))
{
- char * err_out = "Outside appropriate definition's context.";
- char * err_singlechar = "Value must be single ASCII character.";
- err_line(!(*flags & EDIT_STARTED), err_out);
- *flags = *flags | set_flag;
if ('s' == type)
{
+ free(* (char **) element);
* (char **) element = strdup(token1);
}
else if ('c' == type)
{
- err_line(1 != strlen(token1), err_singlechar);
- *element = (token1)[0];
- }
- else if ('8' == type)
- {
- test_for_int(token1, '8');
- * (uint8_t *) element = atoi(token1);
+ if (!parsetest_singlechar(token1))
+ {
+ *element = (token1)[0];
+ }
}
- else if ('i' == type)
+ else if (!parsetest_int(token1, type))
{
- test_for_int(token1, 'i');
- * (int16_t *) element = atoi(token1);
+ 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;
}