From 78a04430cecd23693a1767e2876b1ff462c482aa Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Wed, 14 Aug 2019 13:49:56 +0300 Subject: [PATCH] Add read-ini-file procedure Reads a .ini file into an association list. No [section] parsing yet. --- c/libraries.c | 5 ++- c/text_ini.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ scripts/build.sh | 2 ++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 c/text_ini.c diff --git a/c/libraries.c b/c/libraries.c index c851c57..6992660 100644 --- a/c/libraries.c +++ b/c/libraries.c @@ -39,8 +39,9 @@ #include "env.h" #include "opcodes.h" -#include "stringfuncs.h" +#include "builtins.h" #include "libraries.h" +#include "stringfuncs.h" struct builtin_procedure { char *name; @@ -85,6 +86,8 @@ static struct builtin_procedure builtin_procedures[] = { { "environment-stack", builtin_environment_stack, UP_2019 }, + { "read-ini-file", builtin_read_ini_file, UP_2019 }, + { 0, 0, 0 }, }; diff --git a/c/text_ini.c b/c/text_ini.c new file mode 100644 index 0000000..66786fd --- /dev/null +++ b/c/text_ini.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dtypes.h" +#include "utils.h" +#include "utf8.h" +#include "ios.h" +#include "socket.h" +#include "timefuncs.h" +#include "hashing.h" +#include "htable.h" +#include "htableh_inc.h" +#include "bitvector.h" +#include "os.h" +#include "random.h" +#include "llt.h" + +#include "flisp.h" + +#include "argcount.h" +#include "buf.h" + +static void push(value_t *tailp, value_t elt) +{ + value_t new_tail; + + new_tail = cdr_(*tailp) = fl_cons(elt, FL_NIL); + *tailp = new_tail; +} + +value_t builtin_read_ini_file(value_t *args, uint32_t nargs) +{ + static const char newline[] = "\n\r"; + static const char ends_var[] = "\n\r\""; + static const char anywhite[] = " \t\n\r"; + static const char linewhite[] = " \t"; + static const char varname[] = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + struct buf *b; + struct ios *s; + value_t head, tail, name, value; + + argcount("read-ini-file", nargs, 1); + fl_toiostream(args[0], "read-ini-file"); + s = value2c(struct ios *, args[0]); + b = buf_new(); + buf_put_ios(b, s); + head = tail = fl_cons(FL_NIL, FL_NIL); + for (;;) { + buf_scan_while(b, anywhite); + if (buf_scan_end(b)) { + break; + } + if (buf_scan_byte(b, '#')) { + buf_scan_while_not(b, newline); + continue; + } + buf_scan_mark(b); + buf_scan_while(b, varname); + name = string_from_cstrn(b->bytes + b->mark, b->scan - b->mark); + buf_scan_while(b, linewhite); + if (!buf_scan_byte(b, '=')) { + buf_scan_while_not(b, newline); + continue; + } + buf_scan_while(b, linewhite); + buf_scan_byte(b, '"'); + buf_scan_mark(b); + buf_scan_while_not(b, ends_var); + value = string_from_cstrn(b->bytes + b->mark, b->scan - b->mark); + buf_scan_while(b, linewhite); + push(&tail, fl_cons(name, value)); + if (!buf_scan_while(b, newline)) { + buf_scan_while_not(b, newline); + continue; + } + } + return head; +} + +value_t builtin_write_ini_file(value_t *args, uint32_t nargs) +{ + argcount("write-ini-file", nargs, 2); + (void)args; + return FL_NIL; +} diff --git a/scripts/build.sh b/scripts/build.sh index d9ccdf8..d75d911 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -25,6 +25,7 @@ o_files="$o_files random.o" o_files="$o_files socket.o" o_files="$o_files string.o" o_files="$o_files table.o" +o_files="$o_files text_ini.o" o_files="$o_files time_unix.o" o_files="$o_files utf8.o" default_cflags="-Wall -O2 -D NDEBUG -D USE_COMPUTED_GOTO -Wextra -std=gnu99 -Wno-strict-aliasing" @@ -103,6 +104,7 @@ $CC $CFLAGS -c ../c/random.c $CC $CFLAGS -c ../c/socket.c $CC $CFLAGS -c ../c/string.c $CC $CFLAGS -c ../c/table.c +$CC $CFLAGS -c ../c/text_ini.c $CC $CFLAGS -c ../c/time_unix.c $CC $CFLAGS -c ../c/utf8.c