blob: eac9a8db89a1def2dd48ae0511c582b5b76c25ac [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
6#ifndef BINARYXMLSTRUCTUREDECODER_HPP
7#define BINARYXMLSTRUCTUREDECODER_HPP
8
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 Thompsond6f13282013-06-27 17:31:50 -070034 char *error;
Jeff Thompson71d8f772013-06-27 14:34:11 -070035 if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&base_, input, inputLength))
36 throw std::runtime_error(error);
37 return gotElementEnd();
38 }
39
Jeff Thompsonca3afa32013-07-02 13:50:05 -070040 unsigned int getOffset() { return base_.offset; }
Jeff Thompson71d8f772013-06-27 14:34:11 -070041 bool gotElementEnd() { return base_.gotElementEnd != 0; }
42
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
49#endif /* BINARYXMLSTRUCTUREDECODER_HPP */
50