From 5a2a1e0447bc9dc3defe6dca75e6a54312bbb045 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 9 Nov 2014 19:26:48 +0100 Subject: src: consolidate XML/JSON exportation Add new buffer class to consolidate the existing code to export objects in XML/JSON and use it. We save ~700 LOC with this change. The rule and set objects are not yet consolidated. It seems this would require some specific glue code per representation type since lists are arranged differently. This also consolidates the tag names, so we make sure the same are used from XML and JSON by placing them in include/buffer.h. Signed-off-by: Pablo Neira Ayuso --- src/expr/limit.c | 50 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 40 deletions(-) (limited to 'src/expr/limit.c') diff --git a/src/expr/limit.c b/src/expr/limit.c index 68cfa37..375e6e0 100644 --- a/src/expr/limit.c +++ b/src/expr/limit.c @@ -22,6 +22,7 @@ #include #include #include "expr_ops.h" +#include struct nft_expr_limit { uint64_t rate; @@ -169,48 +170,18 @@ static const char *get_unit(uint64_t u) return "error"; } -static int nft_rule_expr_limit_snprintf_xml(char *buf, size_t len, - struct nft_rule_expr *e) +static int nft_rule_expr_limit_export(char *buf, size_t size, + struct nft_rule_expr *e, int type) { struct nft_expr_limit *limit = nft_expr_data(e); - int ret, size = len, offset = 0; + NFT_BUF_INIT(b, buf, size); - if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) { - ret = snprintf(buf + offset, len, "%"PRIu64"", - limit->rate); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) { - ret = snprintf(buf + offset, len, "%"PRIu64"", - limit->unit); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - - return offset; -} - -static int nft_rule_expr_limit_snprintf_json(char *buf, size_t len, - struct nft_rule_expr *e) -{ - struct nft_expr_limit *limit = nft_expr_data(e); - int ret, size = len, offset = 0; - - if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) { - ret = snprintf(buf + offset, len, "\"rate\":%"PRIu64",", - limit->rate); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) { - ret = snprintf(buf + offset, len, "\"unit\":%"PRIu64",", - limit->unit); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - - /* Remove the last comma characther */ - if (offset > 0) - offset--; + if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) + nft_buf_u64(&b, type, limit->rate, RATE); + if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) + nft_buf_u64(&b, type, limit->unit, UNIT); - return offset; + return nft_buf_done(&b); } static int nft_rule_expr_limit_snprintf_default(char *buf, size_t len, @@ -231,9 +202,8 @@ nft_rule_expr_limit_snprintf(char *buf, size_t len, uint32_t type, case NFT_OUTPUT_DEFAULT: return nft_rule_expr_limit_snprintf_default(buf, len, e); case NFT_OUTPUT_XML: - return nft_rule_expr_limit_snprintf_xml(buf, len, e); case NFT_OUTPUT_JSON: - return nft_rule_expr_limit_snprintf_json(buf, len, e); + return nft_rule_expr_limit_export(buf, len, e, type); default: break; } -- cgit v1.2.3