blob: adc4e773c2c7c94d270be2630bd2c7c111aa20a2 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson76317aa2013-06-25 19:11:48 -07004 */
5
Jeff Thompson19f01182013-07-09 19:29:36 -07006#ifndef NDN_BINARYXMLSTRUCTUREDECODER_HPP
7#define NDN_BINARYXMLSTRUCTUREDECODER_HPP
Jeff Thompson76317aa2013-06-25 19:11:48 -07008
Jeff Thompson71d8f772013-06-27 14:34:11 -07009#include <stdexcept>
Jeff Thompson58b732c2013-07-01 16:51:36 -070010#include "../c/encoding/BinaryXMLStructureDecoder.h"
Jeff Thompson76317aa2013-06-25 19:11:48 -070011
12namespace ndn {
Jeff Thompson4e278992013-06-26 18:59:17 -070013
14/**
Jeff Thompson6bd3a2f2013-07-01 17:17:17 -070015 * A BinaryXMLStructureDecoder wraps a C ndn_BinaryXMLStructureDecoder struct and related functions.
Jeff Thompson4e278992013-06-26 18:59:17 -070016 */
Jeff Thompson76317aa2013-06-25 19:11:48 -070017class BinaryXMLStructureDecoder {
18public:
Jeff Thompsonffbe8ac2013-07-02 11:43:09 -070019 BinaryXMLStructureDecoder()
20 {
Jeff Thompson71d8f772013-06-27 14:34:11 -070021 ndn_BinaryXMLStructureDecoder_init(&base_);
Jeff Thompson76317aa2013-06-25 19:11:48 -070022 }
23
Jeff Thompson71d8f772013-06-27 14:34:11 -070024 /**
25 * Continue scanning input starting from getOffset() to find the element end.
26 * If the end of the element which started at offset 0 is found, then return true and getOffset() is the length of
27 * the element. Otherwise return false, which means you should read more into input and call again.
28 * @param input the input buffer. You have to pass in input each time because the buffer could be reallocated.
29 * @param inputLength the number of bytes in input.
30 * @return true if found the element end, false if need to read more. (This is the same as returning gotElementEnd().)
31 */
Jeff Thompsonffbe8ac2013-07-02 11:43:09 -070032 bool findElementEnd(unsigned char *input, unsigned int inputLength)
33 {
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070034 ndn_Error error;
Jeff Thompson71d8f772013-06-27 14:34:11 -070035 if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&base_, input, inputLength))
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -070036 throw std::runtime_error(ndn_getErrorString(error));
Jeff Thompson71d8f772013-06-27 14:34:11 -070037 return gotElementEnd();
38 }
39
Jeff Thompsond345a5b2013-07-08 16:18:23 -070040 unsigned int getOffset() const { return base_.offset; }
41 bool gotElementEnd() const { return base_.gotElementEnd != 0; }
Jeff Thompson71d8f772013-06-27 14:34:11 -070042
Jeff Thompson76317aa2013-06-25 19:11:48 -070043private:
Jeff Thompson71d8f772013-06-27 14:34:11 -070044 struct ndn_BinaryXMLStructureDecoder base_;
Jeff Thompson76317aa2013-06-25 19:11:48 -070045};
Jeff Thompson4e278992013-06-26 18:59:17 -070046
Jeff Thompson76317aa2013-06-25 19:11:48 -070047}
48
Jeff Thompson19f01182013-07-09 19:29:36 -070049#endif