1
1
/*
2
2
* 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.
4
4
*
5
5
* Modified by Pavel Levshin ([email protected] ) in 2013
6
6
*
@@ -1644,6 +1644,12 @@ PARSER_Parse(OpQuotedString)
1644
1644
}
1645
1645
1646
1646
1647
+
1648
+ struct data_QuotedString {
1649
+ int dashIsEmpty ;
1650
+ int quotesOptional ;
1651
+ int supportEscape ;
1652
+ };
1647
1653
/**
1648
1654
* Parse a quoted string. In this initial implementation, escaping of the quote
1649
1655
* char is not supported. A quoted string is one start starts with a double quote,
@@ -1653,38 +1659,94 @@ PARSER_Parse(OpQuotedString)
1653
1659
*/
1654
1660
PARSER_Parse (QuotedString )
1655
1661
const char * c ;
1662
+ struct data_QuotedString * const data = (struct data_QuotedString * ) pdata ;
1656
1663
size_t i ;
1664
+ int hadQuote = 0 ;
1657
1665
1658
1666
assert (npb -> str != NULL );
1659
1667
assert (offs != NULL );
1660
1668
assert (parsed != NULL );
1661
1669
c = npb -> str ;
1662
1670
i = * offs ;
1663
- if (i + 2 > npb -> strLen )
1664
- goto done ; /* needs at least 2 characters */
1671
+ if (i + 1 > npb -> strLen )
1672
+ goto done ; /* needs at least 1 characters (with quotesQptional...) */
1665
1673
1666
- if (c [i ] != '"' )
1667
- goto done ;
1668
- ++ i ;
1674
+ if (c [i ] == '"' ) {
1675
+ hadQuote = 1 ;
1676
+ ++ i ;
1677
+ } else {
1678
+ if (!data -> quotesOptional ) {
1679
+ goto done ;
1680
+ }
1681
+ }
1669
1682
1670
1683
/* search end of string */
1671
- while (i < npb -> strLen && c [i ] != '"' )
1684
+ while (i < npb -> strLen &&
1685
+ ( (hadQuote && c [i ] != '"' ) || (!hadQuote && c [i ] != ' ' ) )
1686
+ ) {
1687
+ if (data -> supportEscape && c [i ] == '\\' && (i < npb -> strLen )) {
1688
+ i ++ ; /* next char is escaped */
1689
+ }
1672
1690
i ++ ;
1691
+ }
1673
1692
1674
- if (i == npb -> strLen || c [i ] != '"' )
1693
+ if (hadQuote && ( i == npb -> strLen || c [i ] != '"' ) )
1675
1694
goto done ;
1676
1695
1677
1696
/* success, persist */
1678
- * parsed = i + 1 - * offs ; /* "eat" terminal double quote */
1697
+ const size_t charsFound = i - * offs + (hadQuote ? 1 : 0 );
1698
+ * parsed = charsFound ; /* "eat" terminal double quote */
1679
1699
/* create JSON value to save quoted string contents */
1680
1700
if (value != NULL ) {
1681
- * value = json_object_new_string_len (npb -> str + (* offs ), * parsed );
1701
+ if (charsFound == 3 && data -> dashIsEmpty && !strncmp (npb -> str + (* offs ), "\"-\"" , 3 )) {
1702
+ * value = json_object_new_string_len ("" , 0 );
1703
+ } else {
1704
+ * value = json_object_new_string_len (npb -> str + (* offs ), * parsed );
1705
+ }
1682
1706
}
1683
1707
r = 0 ; /* success */
1684
1708
done :
1685
1709
return r ;
1686
1710
}
1687
1711
1712
+ PARSER_Construct (QuotedString )
1713
+ {
1714
+ int r = 0 ;
1715
+ struct data_QuotedString * data = (struct data_QuotedString * ) calloc (1 , sizeof (struct data_QuotedString ));
1716
+
1717
+ if (json == NULL )
1718
+ goto done ;
1719
+
1720
+ struct json_object_iterator it = json_object_iter_begin (json );
1721
+ struct json_object_iterator itEnd = json_object_iter_end (json );
1722
+ while (!json_object_iter_equal (& it , & itEnd )) {
1723
+ const char * key = json_object_iter_peek_name (& it );
1724
+ struct json_object * const val = json_object_iter_peek_value (& it );
1725
+ if (!strcasecmp (key , "option.quotesOptional" )) {
1726
+ data -> quotesOptional = json_object_get_boolean (val );
1727
+ } else if (!strcasecmp (key , "option.dashIsEmpty" )) {
1728
+ data -> dashIsEmpty = json_object_get_boolean (val );
1729
+ } else if (!strcasecmp (key , "option.supportEscape" )) {
1730
+ data -> supportEscape = json_object_get_boolean (val );
1731
+ } else {
1732
+ ln_errprintf (ctx , 0 , "invalid param for QuotedString: %s" ,
1733
+ json_object_to_json_string (val ));
1734
+ }
1735
+ json_object_iter_next (& it );
1736
+ }
1737
+
1738
+ done :
1739
+ * pdata = data ;
1740
+ if (r != 0 )
1741
+ free (data );
1742
+ return r ;
1743
+ }
1744
+ PARSER_Destruct (QuotedString )
1745
+ {
1746
+ free (pdata );
1747
+ }
1748
+
1749
+
1688
1750
1689
1751
/**
1690
1752
* Parse an ISO date, that is YYYY-MM-DD (exactly this format).
0 commit comments