Add read-ini-file procedure

Reads a .ini file into an association list. No [section] parsing yet.
This commit is contained in:
Lassi Kortela 2019-08-14 13:49:56 +03:00
parent 50c9fb2448
commit 78a04430ce
3 changed files with 100 additions and 1 deletions

View File

@ -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 },
};

94
c/text_ini.c Normal file
View File

@ -0,0 +1,94 @@
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -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