aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-05-22 00:37:58 +0200
committerFederico Angelilli <code@fedang.net>2024-05-22 00:37:58 +0200
commitf83f21f98069c3267725c640f090feb6a2628894 (patch)
tree6a7a8cc6d8e67f1fcbf677de764c80de9c0d2180
parent2881783a6d7bc37e03c8639caf8ac0b0876a01c6 (diff)
Add multiline ini parsing
-rw-r--r--any_ini.h21
-rw-r--r--test/ini.c5
-rw-r--r--test/test.ini6
3 files changed, 27 insertions, 5 deletions
diff --git a/any_ini.h b/any_ini.h
index 8efce03..dff8a7c 100644
--- a/any_ini.h
+++ b/any_ini.h
@@ -113,7 +113,21 @@ static char *any_ini_copy(const char *start, size_t length)
{
char *string = ANY_INI_MALLOC(length + 1);
if (string) {
+#ifdef ANY_INI_NO_MULTILINE
memcpy(string, start, length);
+#else
+ size_t i, size = length;
+ for (i = 0, length = 0; i < size; i++) {
+ if (start[i] == '\\') {
+ if (i + 1 < size && start[i + 1] == '\n')
+ i++;
+ else if (i + 2 < size && start[i + 1] == '\r' && start[i + 2] == '\\')
+ i += 2;
+ continue;
+ }
+ string[length++] = start[i];
+ }
+#endif
string[length] = '\0';
}
return string;
@@ -152,6 +166,11 @@ static bool any_ini_skip_pair(const char *source, size_t cursor, bool key)
{
switch (source[cursor]) {
case '\n':
+#ifndef ANY_INI_NO_MULTILINE
+ if ((cursor > 2 && source[cursor - 1] == '\r' && source[cursor - 2] == '\\') ||
+ (cursor > 1 && source[cursor - 1] == '\\'))
+ return true;
+#endif
return false;
#ifndef ANY_INI_NO_INLINE_COMMENT
@@ -159,7 +178,7 @@ static bool any_ini_skip_pair(const char *source, size_t cursor, bool key)
#ifdef ANY_INI_DELIM_COMMENT2
case ANY_INI_DELIM_COMMENT2:
#endif
- if (isspace(source[cursor - 1]))
+ if (cursor != 0 && isspace(source[cursor - 1]))
return false;
// fallthrough
#endif
diff --git a/test/ini.c b/test/ini.c
index ebd41a6..06f841a 100644
--- a/test/ini.c
+++ b/test/ini.c
@@ -20,7 +20,10 @@ void test_ini()
/*13*/ "try = catch 123 bool\n"
/*14*/ " k e y = value pair! ; comment\n"
/*15*/ " su;s = [x] \n"
- /*16*/ " sus \n";
+ /*16*/ " sus \n"
+ /*17*/ "test = multi \\\n"
+ /*18*/ " line \\\n"
+ /*19*/ " works ; boh \n";
any_ini_t ini;
any_ini_init(&ini, src, strlen(src));
diff --git a/test/test.ini b/test/test.ini
index e575371..6d5f939 100644
--- a/test/test.ini
+++ b/test/test.ini
@@ -20,7 +20,7 @@ sus ; sus
123 = 456
; comment
- ; another
+ ; another
crazy = stuff ; here
crumped=stuff;nocomment
@@ -30,11 +30,11 @@ crumped=stuff;nocomment
[another]
- ciao = s
+ ciao = s
]err = sus[
- [empty]
+ [empty]
; yes
;