blob: 1885cb3c69749580510c9b683e78a24741566386 [file] [log] [blame]
Jeff Thompson76317aa2013-06-25 19:11:48 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
7#ifndef BINARYXMLSTRUCTUREDECODER_HPP
8#define BINARYXMLSTRUCTUREDECODER_HPP
9
Jeff Thompson71d8f772013-06-27 14:34:11 -070010#include <stdexcept>
Jeff Thompson58b732c2013-07-01 16:51:36 -070011#include "../c/encoding/BinaryXMLStructureDecoder.h"
Jeff Thompson76317aa2013-06-25 19:11:48 -070012
13namespace ndn {
Jeff Thompson4e278992013-06-26 18:59:17 -070014
15/**
Jeff Thompson6bd3a2f2013-07-01 17:17:17 -070016 * A BinaryXMLStructureDecoder wraps a C ndn_BinaryXMLStructureDecoder struct and related functions.
Jeff Thompson4e278992013-06-26 18:59:17 -070017 */
Jeff Thompson76317aa2013-06-25 19:11:48 -070018class BinaryXMLStructureDecoder {
19public:
Jeff Thompsonffbe8ac2013-07-02 11:43:09 -070020 BinaryXMLStructureDecoder()
21 {
Jeff Thompson71d8f772013-06-27 14:34:11 -070022 ndn_BinaryXMLStructureDecoder_init(&base_);
Jeff Thompson76317aa2013-06-25 19:11:48 -070023 }
24
Jeff Thompson71d8f772013-06-27 14:34:11 -070025 /**
26 * Continue scanning input starting from getOffset() to find the element end.
27 * If the end of the element which started at offset 0 is found, then return true and getOffset() is the length of
28 * the element. Otherwise return false, which means you should read more into input and call again.
29 * @param input the input buffer. You have to pass in input each time because the buffer could be reallocated.
30 * @param inputLength the number of bytes in input.
31 * @return true if found the element end, false if need to read more. (This is the same as returning gotElementEnd().)
32 */
Jeff Thompsonffbe8ac2013-07-02 11:43:09 -070033 bool findElementEnd(unsigned char *input, unsigned int inputLength)
34 {
Jeff Thompsond6f13282013-06-27 17:31:50 -070035 char *error;
Jeff Thompson71d8f772013-06-27 14:34:11 -070036 if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&base_, input, inputLength))
37 throw std::runtime_error(error);
38 return gotElementEnd();
39 }
40
Jeff Thompsonca3afa32013-07-02 13:50:05 -070041 unsigned int getOffset() { return base_.offset; }
Jeff Thompson71d8f772013-06-27 14:34:11 -070042 bool gotElementEnd() { return base_.gotElementEnd != 0; }
43
Jeff Thompson76317aa2013-06-25 19:11:48 -070044private:
Jeff Thompson71d8f772013-06-27 14:34:11 -070045 struct ndn_BinaryXMLStructureDecoder base_;
Jeff Thompson76317aa2013-06-25 19:11:48 -070046};
Jeff Thompson4e278992013-06-26 18:59:17 -070047
Jeff Thompson76317aa2013-06-25 19:11:48 -070048}
49
50#endif /* BINARYXMLSTRUCTUREDECODER_HPP */
51