Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/include/nica/nc-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ typedef struct nc_string_t {
_nica_public_ nc_string *nc_string_dup_printf(const char *format, ...)
__attribute__((format(printf, 1, 2)));


_nica_public_ nc_string *nc_string_append_printf(nc_string *st, const char *ptn, ...)
__attribute__((format(printf, 2, 3)));

/**
* Duplicate a string into a new NUL-terminated nc_string
*
Expand Down Expand Up @@ -72,6 +76,9 @@ static inline void nc_string_free(nc_string *str)
*/
_nica_public_ bool nc_string_cat(nc_string *str, const char *append);

_nica_public_ bool nc_string_prepend(nc_string *s, const char *prepend);


/**
* Determine if string A is equal to string B
*
Expand Down
35 changes: 35 additions & 0 deletions src/inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ static char *string_strip(char *str, ssize_t len, ssize_t *out_len)
return c;
}

/* eat \s */
static char *string_unescape(char *str)
{
char *c, *c1, *c2, *c3;;

if (!str) {
return NULL;
}

c = calloc(strlen(str) + 1, 1);
c1 = c;

c2 = str;
while (c2 && *c2 != 0) {
c3 = c2 + 1;
if (*c2 == '\\') {
if (*c3 == 's') {
*c = ' ';
c++;
c2 += 2;
} else {
c2++;
}
} else {
*c = *c2;
c++;
c2++;
}
}

free(str);
return c1;
}

NcHashmap *nc_ini_file_parse(const char *path)
{
NcHashmap *ret = NULL;
Expand Down Expand Up @@ -268,6 +302,7 @@ int nc_ini_file_parse_full(const char *path, NcHashmap **out_map, int *error_lin
key = strdup(buf);
key = string_chew_terminated(key);
value = string_chew_terminated(value);
value = string_unescape(value);

if (streq(key, "")) {
err_ret = NC_INI_ERROR_EMPTY_KEY;
Expand Down
49 changes: 49 additions & 0 deletions src/nc-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ nc_string *nc_string_dup_printf(const char *ptn, ...)
return st;
}

nc_string *nc_string_append_printf(nc_string *st, const char *ptn, ...)
{
char *newstr;
char *newstr2;
int ret;

if (!ptn) {
return NULL;
}

va_list va;
va_start(va, ptn);

ret = vasprintf(&newstr, ptn, va);
if (ret <= 0) {
newstr = strdup("");
}

st->len = asprintf(&newstr2, "%s%s", st->str, newstr);
free(st->str);
st->str = newstr2;
free(newstr);
va_end(va);

return st;
}


bool nc_string_cat(nc_string *s, const char *append)
{
char *p = NULL;
Expand All @@ -82,6 +110,27 @@ bool nc_string_cat(nc_string *s, const char *append)
return true;
}

bool nc_string_prepend(nc_string *s, const char *prepend)
{
char *p = NULL;
int len = 0;

if (!s || !prepend) {
return false;
}
if (!s->str) {
return false;
}
len = asprintf(&p, "%s%s", prepend, s->str);
if (!p || len < s->len) {
return false;
}
free(s->str);
s->str = p;
s->len = len;
return true;
}

/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
Expand Down
11 changes: 11 additions & 0 deletions tests/check-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ START_TEST(nc_string_test)
nc_string st = { .len = 0 };
fail_if(nc_string_equal(&st, &st), "equal on NULL ->str");
fail_if(nc_string_const_equal(&st, "TEST"), "const_equal on NULL ->str");

str = nc_string_dup_printf("Test String #%d", 3);
fail_if(!str, "Failed to allocate string");
fail_if(ncstrlen(str) != 14, "Incorrect string length");
fail_if(!nc_string_append_printf(str, "append"), "Failed to append formatted string");
fail_if(ncstrlen(str) != 20, "Incorrect string length after formatted append");

fail_if(!nc_string_prepend(str, "prepend"), "Failed to prepend string");
fail_if(ncstrlen(str) != 27, "Incorrect string length after prepend");

nc_string_free(str);
}
END_TEST

Expand Down
7 changes: 6 additions & 1 deletion tests/check-inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ END_TEST
START_TEST(nc_inifile_good_test)
{
const char *wellformed[] = { TOP_DIR "/tests/ini/wellformed.ini",
TOP_DIR "/tests/ini/valid_padding.ini" };
TOP_DIR "/tests/ini/valid_padding.ini",
TOP_DIR "/tests/ini/escaped_chars.ini" };

for (size_t i = 0; i < sizeof(wellformed) / sizeof(wellformed[0]); i++) {
autofree(NcHashmap) *f = NULL;
Expand All @@ -56,6 +57,10 @@ START_TEST(nc_inifile_good_test)

ret = nc_hashmap_get(nc_hashmap_get(f, "Bob"), "Random");
fail_if(ret, "Got unexpected section");

ret = nc_hashmap_get(nc_hashmap_get(f, "Barry"), "slogan");
fail_if(!ret, "Failed to get known value from INI file");
fail_if(!streq(ret, "Hello World"), "Incorrect value in INI file #3");
}
}
END_TEST
Expand Down
10 changes: 10 additions & 0 deletions tests/ini/escaped_chars.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[John]
alive=true

[Alice]
# Various comment types
; Sorry, Alice!
alive = false

[Barry]
slogan=Hello\sWorld
3 changes: 3 additions & 0 deletions tests/ini/valid_padding.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
# Various comment types
; Sorry, Alice!
alive = false

[Barry]
slogan=Hello World
3 changes: 3 additions & 0 deletions tests/ini/wellformed.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ alive=true
# Various comment types
; Sorry, Alice!
alive = false

[Barry]
slogan=Hello World