saait

Simple static page generator
git clone https://git.sinitax.com/codemadness/saait
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit 3b251472cd77ba5993054bb9100b9e48c8dadb49
parent ee26bbcc1843409f40404ce7523ed2e0e3e6bf3d
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  4 Dec 2016 15:09:33 +0100

improve .cfg parsing, allow \r\n line-endings

Diffstat:
MTODO | 1+
Msaait.c | 29++++++++++++++---------------
2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/TODO b/TODO @@ -1,3 +1,4 @@ +x allow \r\n line endings in .cfg files. x cfgfile: spaces around = skip x optimize readfile: don't copy data twice (memcpy). x reduce memory usage. diff --git a/saait.c b/saait.c @@ -218,34 +218,33 @@ parsevars(const char *s) const char *keystart, *keyend, *valuestart, *valueend; for (; *s; ) { - /* comment */ + /* comment start with #, skip to newline */ if (*s == '#') { - for (; *s; s++) { - if (*s == '\n') { - s++; - break; - } - } + s = &s[strcspn(s + 1, "\n")]; + continue; + } + if (*s == '\r' || *s == '\n') { + s++; continue; } keystart = s; - s = &s[strcspn(s, "=\n")]; + s = &s[strcspn(s, "=\r\n")]; if (*s != '=') { s++; continue; } - for (keyend = s; keyend > keystart && isspace(keyend[-1]); keyend--) - ; - for (s++; *s && *s != '\n' && isspace(*s); s++) + for (keyend = s; keyend > keystart && (keyend[-1] == ' ' || keyend[-1] == '\t'); keyend--) ; - valuestart = s; - valueend = &s[strcspn(s, "\n")]; - - /* no variable: skip */ + /* no variable name: skip */ if (keystart == keyend) continue; + for (s++; *s && (*s == ' ' || *s == '\t'); s++) + ; + valuestart = s; + valueend = &s[strcspn(s, "\r\n")]; + v = ecalloc(1, sizeof(*v)); v->key = estrndup(keystart, keyend - keystart); v->value = estrndup(valuestart, valueend - valuestart);