Added readOptionalTimeMillisecondsDTagElement
diff --git a/ndn-cpp/c/encoding/BinaryXMLDecoder.c b/ndn-cpp/c/encoding/BinaryXMLDecoder.c
index b687357..b27e8bc 100644
--- a/ndn-cpp/c/encoding/BinaryXMLDecoder.c
+++ b/ndn-cpp/c/encoding/BinaryXMLDecoder.c
@@ -247,6 +247,38 @@
return 0;
}
+ndn_Error ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value)
+{
+ ndn_Error error;
+ unsigned char *bytes;
+ unsigned int bytesLength;
+ if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(self, expectedTag, 0, &bytes, &bytesLength))
+ return error;
+
+ *value = 1000.0 * ndn_BinaryXMLDecoder_unsignedBigEndianToDouble(bytes, bytesLength) / 4096.0;
+ return 0;
+}
+
+ndn_Error ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value)
+{
+ int gotExpectedTag;
+ ndn_Error error;
+ if (error = ndn_BinaryXMLDecoder_peekDTag(self, expectedTag, &gotExpectedTag))
+ return error;
+
+ if (!gotExpectedTag) {
+ *value = -1.0;
+ return 0;
+ }
+
+ if (error = ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement(self, expectedTag, value))
+ return error;
+
+ return 0;
+}
+
double ndn_BinaryXMLDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength)
{
double result = 0.0;
diff --git a/ndn-cpp/c/encoding/BinaryXMLDecoder.h b/ndn-cpp/c/encoding/BinaryXMLDecoder.h
index d157ca7..fb8afe1 100644
--- a/ndn-cpp/c/encoding/BinaryXMLDecoder.h
+++ b/ndn-cpp/c/encoding/BinaryXMLDecoder.h
@@ -132,6 +132,29 @@
(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value);
/**
+ * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
+ * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds.
+ * Finally, read the element close. Update offset.
+ * @param self pointer to the ndn_BinaryXMLDecoder struct
+ * @param expectedTag the expected value for DTAG
+ * @param value output the number of milliseconds
+ * @return 0 for success, else an error code, including an error if not the expected tag
+ */
+ndn_Error ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value);
+
+/**
+ * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement.
+ * Otherwise, set value to -1.0 .
+ * @param self pointer to the ndn_BinaryXMLDecoder struct
+ * @param expectedTag the expected value for DTAG
+ * @param value output the number of milliseconds, or -1.0 if the next element doesn't have expectedTag.
+ * @return 0 for success, else an error code
+ */
+ndn_Error ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement
+ (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value);
+
+/**
* Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow.
* We use a double because it is large enough to represent NDN time (4096 ticks per second since 1970).
* @param bytes pointer to the array of bytes
diff --git a/ndn-cpp/c/encoding/BinaryXMLInterest.c b/ndn-cpp/c/encoding/BinaryXMLInterest.c
index 2ba6e66..337ca0f 100644
--- a/ndn-cpp/c/encoding/BinaryXMLInterest.c
+++ b/ndn-cpp/c/encoding/BinaryXMLInterest.c
@@ -216,20 +216,9 @@
(decoder, ndn_BinaryXML_DTag_Scope, &interest->scope))
return error;
- if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag))
+ if (error= ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement
+ (decoder, ndn_BinaryXML_DTag_InterestLifetime, &interest->interestLifetimeMilliseconds))
return error;
- if (gotExpectedTag) {
- unsigned char *interestLifetime;
- unsigned int interestLifetimeLength;
- if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
- (decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength))
- return error;
-
- interest->interestLifetimeMilliseconds = 1000.0 *
- ndn_BinaryXMLDecoder_unsignedBigEndianToDouble(interestLifetime, interestLifetimeLength) / 4096.0;
- }
- else
- interest->interestLifetimeMilliseconds = -1.0;
if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
(decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength))