Added writeOptionalUnsignedDecimalIntDTagElement
diff --git a/ndn-cpp/c/encoding/BinaryXMLEncoder.h b/ndn-cpp/c/encoding/BinaryXMLEncoder.h
index ee5d773..98dc13f 100644
--- a/ndn-cpp/c/encoding/BinaryXMLEncoder.h
+++ b/ndn-cpp/c/encoding/BinaryXMLEncoder.h
@@ -86,7 +86,7 @@
ndn_Error ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength);
/**
- * If value or valueLen is 0, then do nothing, otherwise call ndn_BinaryXMLEncoder_writeBlobDTagElement.
+ * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXMLEncoder_writeBlobDTagElement.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param tag the DTAG tag
* @param value an array of bytes for the blob value
@@ -122,6 +122,21 @@
ndn_Error ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned int value);
/**
+ * If value is negative then do nothing, otherwise call ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement.
+ * @param self pointer to the ndn_BinaryXMLEncoder struct
+ * @param tag the DTAG tag
+ * @param value negative for none, otherwise use (unsigned int)value
+ * @return 0 for success, else an error code
+ */
+static inline ndn_Error ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, int value)
+{
+ if (value >= 0)
+ return ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value);
+ else
+ return 0;
+}
+
+/**
* Write a BLOB header, then the value to self->output encoded as big endian.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param value the unsigned int to encode as big endian. If value is 0, the big endian encoding has zero bytes.
diff --git a/ndn-cpp/c/encoding/BinaryXMLInterest.c b/ndn-cpp/c/encoding/BinaryXMLInterest.c
index 9b06a76..5708761 100644
--- a/ndn-cpp/c/encoding/BinaryXMLInterest.c
+++ b/ndn-cpp/c/encoding/BinaryXMLInterest.c
@@ -128,16 +128,12 @@
if (error = ndn_encodeBinaryXMLName(&interest->name, encoder))
return error;
- if (interest->minSuffixComponents >= 0) {
- if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
- (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, (unsigned int)interest->minSuffixComponents))
- return error;
- }
- if (interest->maxSuffixComponents >= 0) {
- if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
- (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, (unsigned int)interest->maxSuffixComponents))
- return error;
- }
+ if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, interest->minSuffixComponents))
+ return error;
+ if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, interest->maxSuffixComponents))
+ return error;
// This will skip encoding if there is no publisherPublicKeyDigest.
if (error = ndn_encodeBinaryXMLPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, encoder))
@@ -147,21 +143,17 @@
if (error = encodeExclude(&interest->exclude, encoder))
return error;
- if (interest->childSelector >= 0) {
- if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
- (encoder, ndn_BinaryXML_DTag_ChildSelector, (unsigned int)interest->childSelector))
- return error;
- }
+ if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXML_DTag_ChildSelector, interest->childSelector))
+ return error;
if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
(encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind))
return error;
}
- if (interest->scope >= 0) {
- if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
- (encoder, ndn_BinaryXML_DTag_Scope, (unsigned int)interest->scope))
- return error;
- }
+ if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
+ (encoder, ndn_BinaryXML_DTag_Scope, interest->scope))
+ return error;
if (interest->interestLifetime >= 0) {
if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime))