Skip to content

Commit

Permalink
add new parameters for quoted-string parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerhards committed May 18, 2021
1 parent 530b7f2 commit 4f14886
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
53 changes: 51 additions & 2 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* liblognorm - a fast samples-based log normalization library
* Copyright 2010-2018 by Rainer Gerhards and Adiscon GmbH.
* Copyright 2010-2021 by Rainer Gerhards and Adiscon GmbH.
*
* Modified by Pavel Levshin ([email protected]) in 2013
*
Expand Down Expand Up @@ -1644,6 +1644,12 @@ PARSER_Parse(OpQuotedString)
}



struct data_QuotedString {
int dashIsNull;
int quotesOptional;
int supportEscape;
};
/**
* Parse a quoted string. In this initial implementation, escaping of the quote
* char is not supported. A quoted string is one start starts with a double quote,
Expand All @@ -1653,6 +1659,7 @@ PARSER_Parse(OpQuotedString)
*/
PARSER_Parse(QuotedString)
const char *c;
struct data_QuotedString *const data = (struct data_QuotedString*) pdata;
size_t i;

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

/* search end of string */
while(i < npb->strLen && c[i] != '"')
while(i < npb->strLen && c[i] != '"') {
if(data->supportEscape && c[i] == '\\' && (i < npb->strLen)) {
i++; /* next char is escaped */
}
i++;
}

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

PARSER_Construct(QuotedString)
{
int r = 0;
struct data_QuotedString *data = (struct data_QuotedString*) calloc(1, sizeof(struct data_QuotedString));

if(json == NULL)
goto done;

struct json_object_iterator it = json_object_iter_begin(json);
struct json_object_iterator itEnd = json_object_iter_end(json);
while (!json_object_iter_equal(&it, &itEnd)) {
const char *key = json_object_iter_peek_name(&it);
struct json_object *const val = json_object_iter_peek_value(&it);
if(!strcasecmp(key, "option.quotesOptional")) {
data->quotesOptional = json_object_get_boolean(val);
} else if(!strcasecmp(key, "option.dashIsNull")) {
data->dashIsNull = json_object_get_boolean(val);
} else if(!strcasecmp(key, "option.supportEscape")) {
data->supportEscape = json_object_get_boolean(val);
} else {
ln_errprintf(ctx, 0, "invalid param for QuotedString: %s",
json_object_to_json_string(val));
}
json_object_iter_next(&it);
}

done:
*pdata = data;
if(r != 0)
free(data);
return r;
}
PARSER_Destruct(QuotedString)
{
free(pdata);
}



/**
* Parse an ISO date, that is YYYY-MM-DD (exactly this format).
Expand Down
4 changes: 2 additions & 2 deletions src/parser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* liblognorm - a fast samples-based log normalization library
* Copyright 2010-2015 by Rainer Gerhards and Adiscon GmbH.
* Copyright 2010-2021 by Rainer Gerhards and Adiscon GmbH.
*
* Modified by Pavel Levshin ([email protected]) in 2013
*
Expand Down Expand Up @@ -63,7 +63,7 @@ PARSERDEF(Repeat);
PARSERDEF(String);
PARSERDEF_NO_DATA(Rest);
PARSERDEF_NO_DATA(OpQuotedString);
PARSERDEF_NO_DATA(QuotedString);
PARSERDEF(QuotedString);
PARSERDEF_NO_DATA(ISODate);
PARSERDEF_NO_DATA(Time12hr);
PARSERDEF_NO_DATA(Time24hr);
Expand Down
4 changes: 2 additions & 2 deletions src/pdag.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @brief Implementation of the parse dag object.
* @class ln_pdag pdag.h
*//*
* Copyright 2015 by Rainer Gerhards and Adiscon GmbH.
* Copyright 2015-2021 by Rainer Gerhards and Adiscon GmbH.
*
* Released under ASL 2.0.
*/
Expand Down Expand Up @@ -83,7 +83,7 @@ static struct ln_parser_info parser_lookup_table[] = {
PARSER_ENTRY_NO_DATA("alpha", Alpha, 32),
PARSER_ENTRY_NO_DATA("rest", Rest, 255),
PARSER_ENTRY_NO_DATA("op-quoted-string", OpQuotedString, 64),
PARSER_ENTRY_NO_DATA("quoted-string", QuotedString, 64),
PARSER_ENTRY("quoted-string", QuotedString, 64),
PARSER_ENTRY_NO_DATA("date-iso", ISODate, 8),
PARSER_ENTRY_NO_DATA("time-24hr", Time24hr, 8),
PARSER_ENTRY_NO_DATA("time-12hr", Time12hr, 8),
Expand Down

0 comments on commit 4f14886

Please sign in to comment.