netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file
Date: Thu, 26 Jun 2014 19:08:40 +0200	[thread overview]
Message-ID: <1403802521-12571-5-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 | 47 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/table.c b/src/table.c
index 5a0135f..7874245 100644
--- a/src/table.c
+++ b/src/table.c
@@ -266,20 +266,17 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
-	if (name == NULL)
-		return -1;
-	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);
+	if (name != NULL)
+		nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);
 
 	family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
 				       NFT_XML_MAND, err);
-	if (family < 0)
-		return -1;
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
+	if (family >= 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
 	if (nft_mxml_num_parse(tree, "flags", MXML_DESCEND, BASE_DEC,
-			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) != 0)
-		return -1;
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
+			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
 	if (nft_mxml_num_parse(tree, "use", MXML_DESCEND, BASE_DEC,
 			       &use, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
@@ -424,10 +421,34 @@ static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t)
 
 static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t)
 {
-	return snprintf(buf, size, "<table><name>%s</name><family>%s</family>"
-			"<flags>%d</flags><use>%d</use></table>",
-			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);
+
+	if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) {
+		ret = snprintf(buf + offset, size, "<name>%s</name>", t->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) {
+		ret = snprintf(buf + offset, size, "<family>%s</family>",
+			       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</flags>",
+			       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</use>", t->use);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	ret = snprintf(buf + offset, size, "</table>");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	return offset;
 }
 
 static int nft_table_snprintf_default(char *buf, size_t size, struct nft_table *t)
-- 
2.0.0


  parent reply	other threads:[~2014-06-26 17:08 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 ` Ana Rey [this message]
2014-06-26 17:08 ` [libnftnl PATCH 5/5] src: table: Do not print unset values in json file Ana Rey
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-5-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).