From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 5/5] src: table: Do not print unset values in json file
Date: Thu, 26 Jun 2014 19:08:41 +0200 [thread overview]
Message-ID: <1403802521-12571-6-git-send-email-anarey@gmail.com> (raw)
In-Reply-To: <1403802521-12571-1-git-send-email-anarey@gmail.com>
It changes the parse and snprintf functions to omit unset values.
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/table.c | 64 ++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 42 insertions(+), 22 deletions(-)
diff --git a/src/table.c b/src/table.c
index 7874245..abbaa52 100644
--- a/src/table.c
+++ b/src/table.c
@@ -319,20 +319,15 @@ int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
return -1;
str = nft_jansson_parse_str(root, "name", err);
- if (str == NULL)
- return -1;
-
- nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);
-
- if (nft_jansson_parse_family(root, &family, err) != 0)
- return -1;
-
- nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
+ if (str != NULL)
+ nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);
- if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags, err) < 0)
- return -1;
+ if (nft_jansson_parse_family(root, &family, err) == 0)
+ nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
- nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
+ if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags,
+ err) == 0)
+ nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
if (nft_jansson_parse_val(root, "use", NFT_TYPE_U32, &use, err) == 0)
nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);
@@ -407,16 +402,41 @@ EXPORT_SYMBOL(nft_table_parse_file);
static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t)
{
- return snprintf(buf, size,
- "{\"table\":{"
- "\"name\":\"%s\","
- "\"family\":\"%s\","
- "\"flags\":%d,"
- "\"use\":%d"
- "}"
- "}" ,
- t->name, nft_family2str(t->family),
- t->table_flags, t->use);
+ int ret, len = size, offset = 0;
+
+ ret = snprintf(buf, size, "{\"table\":{");
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ ret = 0;
+
+ if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) {
+ ret = snprintf(buf + offset, size, "\"name\":\"%s\",", t->name);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) {
+ ret = snprintf(buf + offset, size, "\"family\":\"%s\",",
+ nft_family2str(t->family));
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) {
+ ret = snprintf(buf + offset, size, "\"flags\":%u,",
+ t->table_flags);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (t->flags & (1 << NFT_TABLE_ATTR_USE)) {
+ ret = snprintf(buf + offset, size, "\"use\":%u,", t->use);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ /* If some values is set, ret is not 0. So, It's needed to remove the
+ * last comma characther
+ */
+ if (ret > 0)
+ offset--;
+
+ ret = snprintf(buf + offset, size, "}}");
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+ return offset;
}
static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t)
--
2.0.0
next prev parent reply other threads:[~2014-06-26 17:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 2/5] src: table: Use nft_table_attr_set_* in the xml functions Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 3/5] src: table: Add set, unset and parse implementation for use value Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file Ana Rey
2014-06-26 17:08 ` Ana Rey [this message]
2014-06-30 10:49 ` [libnftnl PATCH 0/5] Do not print unset values in xml and json Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1403802521-12571-6-git-send-email-anarey@gmail.com \
--to=anarey@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).