X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fcommon%2Fparse_file.c;h=ded2d5ad7c98bcf8200b4356d6b75e325e0c9ed8;hb=a5b35a816a4a52235aa5c56386193ebdf64619db;hp=8ce20ad55db79c7369225134e73d2f09a5d2b42a;hpb=1452d43c6d7c89219cda91362da53ac8e4acb887;p=plomrogue
diff --git a/src/common/parse_file.c b/src/common/parse_file.c
index 8ce20ad..ded2d5a 100644
--- a/src/common/parse_file.c
+++ b/src/common/parse_file.c
@@ -1,14 +1,17 @@
-/* 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() */
@@ -49,53 +52,26 @@ 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)
{
- 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);
+ err_line_line = line;
+ err_line_intro = intro;
+ err_line_exit = exit;
}
-extern void set_err_line_options(char * intro, char * line, uint32_t count,
- uint8_t exit)
+extern void err_line_inc()
{
- err_line_count = count;
- err_line_line = line;
- err_line_intro = intro;
- err_line_exit = exit;
+ err_line_count++;
+ err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
+}
+
+
+
+extern void err_line_zero()
+{
+ err_line_count = 0;
}
@@ -106,22 +82,21 @@ extern uint8_t err_line(uint8_t test, char * msg)
{
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_trouble(ret < 0, __func__, "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()");
+ exit_trouble(0 > printf("%s\n", err), __func__, "printf");
+ exit_trouble(EOF == fflush(stdout), __func__, "fflush");
free(err);
return 1;
}
@@ -208,13 +183,6 @@ extern uint8_t parsetest_int(char * string, char type)
-extern void parsetest_defcontext(uint8_t flags)
-{
- 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.");
@@ -222,38 +190,6 @@ extern uint8_t parsetest_singlechar(char * string)
-extern void parsetest_too_many_values()
-{
- err_line(NULL != token_from_line(NULL), "Too many values.");
-}
-
-
-
-extern void parse_id_uniq(int test)
-{
- 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)
{
@@ -261,11 +197,15 @@ extern uint8_t parse_val(char * token0, char * token1, char * comparand,
{
if ('s' == type)
{
+ free(* (char **) element);
* (char **) element = strdup(token1);
}
- else if ('c' == type && !parsetest_singlechar(token1))
+ else if ('c' == type)
{
- *element = (token1)[0];
+ if (!parsetest_singlechar(token1))
+ {
+ *element = (token1)[0];
+ }
}
else if (!parsetest_int(token1, type))
{
@@ -290,27 +230,3 @@ extern uint8_t parse_val(char * token0, char * token1, char * comparand,
}
return 0;
}
-
-
-
-extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
- uint8_t * flags, uint8_t set_flag, char type,
- char * element)
-{
- if (parse_val(token0, token1, comparand, type, element))
- {
- parsetest_defcontext(*flags);
- *flags = *flags | set_flag;
- return 1;
- }
- 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;
-}