Skip to content

Commit 4f14886

Browse files
committed
add new parameters for quoted-string parser
1 parent 530b7f2 commit 4f14886

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/parser.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* liblognorm - a fast samples-based log normalization library
3-
* Copyright 2010-2018 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2010-2021 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* Modified by Pavel Levshin ([email protected]) in 2013
66
*
@@ -1644,6 +1644,12 @@ PARSER_Parse(OpQuotedString)
16441644
}
16451645

16461646

1647+
1648+
struct data_QuotedString {
1649+
int dashIsNull;
1650+
int quotesOptional;
1651+
int supportEscape;
1652+
};
16471653
/**
16481654
* Parse a quoted string. In this initial implementation, escaping of the quote
16491655
* char is not supported. A quoted string is one start starts with a double quote,
@@ -1653,6 +1659,7 @@ PARSER_Parse(OpQuotedString)
16531659
*/
16541660
PARSER_Parse(QuotedString)
16551661
const char *c;
1662+
struct data_QuotedString *const data = (struct data_QuotedString*) pdata;
16561663
size_t i;
16571664

16581665
assert(npb->str != NULL);
@@ -1668,8 +1675,12 @@ PARSER_Parse(QuotedString)
16681675
++i;
16691676

16701677
/* search end of string */
1671-
while(i < npb->strLen && c[i] != '"')
1678+
while(i < npb->strLen && c[i] != '"') {
1679+
if(data->supportEscape && c[i] == '\\' && (i < npb->strLen)) {
1680+
i++; /* next char is escaped */
1681+
}
16721682
i++;
1683+
}
16731684

16741685
if(i == npb->strLen || c[i] != '"')
16751686
goto done;
@@ -1685,6 +1696,44 @@ PARSER_Parse(QuotedString)
16851696
return r;
16861697
}
16871698

1699+
PARSER_Construct(QuotedString)
1700+
{
1701+
int r = 0;
1702+
struct data_QuotedString *data = (struct data_QuotedString*) calloc(1, sizeof(struct data_QuotedString));
1703+
1704+
if(json == NULL)
1705+
goto done;
1706+
1707+
struct json_object_iterator it = json_object_iter_begin(json);
1708+
struct json_object_iterator itEnd = json_object_iter_end(json);
1709+
while (!json_object_iter_equal(&it, &itEnd)) {
1710+
const char *key = json_object_iter_peek_name(&it);
1711+
struct json_object *const val = json_object_iter_peek_value(&it);
1712+
if(!strcasecmp(key, "option.quotesOptional")) {
1713+
data->quotesOptional = json_object_get_boolean(val);
1714+
} else if(!strcasecmp(key, "option.dashIsNull")) {
1715+
data->dashIsNull = json_object_get_boolean(val);
1716+
} else if(!strcasecmp(key, "option.supportEscape")) {
1717+
data->supportEscape = json_object_get_boolean(val);
1718+
} else {
1719+
ln_errprintf(ctx, 0, "invalid param for QuotedString: %s",
1720+
json_object_to_json_string(val));
1721+
}
1722+
json_object_iter_next(&it);
1723+
}
1724+
1725+
done:
1726+
*pdata = data;
1727+
if(r != 0)
1728+
free(data);
1729+
return r;
1730+
}
1731+
PARSER_Destruct(QuotedString)
1732+
{
1733+
free(pdata);
1734+
}
1735+
1736+
16881737

16891738
/**
16901739
* Parse an ISO date, that is YYYY-MM-DD (exactly this format).

src/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* liblognorm - a fast samples-based log normalization library
3-
* Copyright 2010-2015 by Rainer Gerhards and Adiscon GmbH.
3+
* Copyright 2010-2021 by Rainer Gerhards and Adiscon GmbH.
44
*
55
* Modified by Pavel Levshin ([email protected]) in 2013
66
*
@@ -63,7 +63,7 @@ PARSERDEF(Repeat);
6363
PARSERDEF(String);
6464
PARSERDEF_NO_DATA(Rest);
6565
PARSERDEF_NO_DATA(OpQuotedString);
66-
PARSERDEF_NO_DATA(QuotedString);
66+
PARSERDEF(QuotedString);
6767
PARSERDEF_NO_DATA(ISODate);
6868
PARSERDEF_NO_DATA(Time12hr);
6969
PARSERDEF_NO_DATA(Time24hr);

src/pdag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @brief Implementation of the parse dag object.
44
* @class ln_pdag pdag.h
55
*//*
6-
* Copyright 2015 by Rainer Gerhards and Adiscon GmbH.
6+
* Copyright 2015-2021 by Rainer Gerhards and Adiscon GmbH.
77
*
88
* Released under ASL 2.0.
99
*/
@@ -83,7 +83,7 @@ static struct ln_parser_info parser_lookup_table[] = {
8383
PARSER_ENTRY_NO_DATA("alpha", Alpha, 32),
8484
PARSER_ENTRY_NO_DATA("rest", Rest, 255),
8585
PARSER_ENTRY_NO_DATA("op-quoted-string", OpQuotedString, 64),
86-
PARSER_ENTRY_NO_DATA("quoted-string", QuotedString, 64),
86+
PARSER_ENTRY("quoted-string", QuotedString, 64),
8787
PARSER_ENTRY_NO_DATA("date-iso", ISODate, 8),
8888
PARSER_ENTRY_NO_DATA("time-24hr", Time24hr, 8),
8989
PARSER_ENTRY_NO_DATA("time-12hr", Time12hr, 8),

0 commit comments

Comments
 (0)