Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 10 | #include <stdexcept> |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 11 | #include "BinaryXMLStructureDecoder.h" |
| 12 | |
| 13 | namespace ndn { |
Jeff Thompson | 4e27899 | 2013-06-26 18:59:17 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * A BinaryXMLStructureDecoder wraps a C struct ndn_BinaryXMLStructureDecoder and |
| 17 | * related functions. |
| 18 | */ |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 19 | class BinaryXMLStructureDecoder { |
| 20 | public: |
| 21 | BinaryXMLStructureDecoder() { |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 22 | ndn_BinaryXMLStructureDecoder_init(&base_); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 25 | /** |
| 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 Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 33 | bool findElementEnd(unsigned char *input, unsigned int inputLength) { |
| 34 | char *error; |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 35 | if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&base_, input, inputLength)) |
| 36 | throw std::runtime_error(error); |
| 37 | return gotElementEnd(); |
| 38 | } |
| 39 | |
| 40 | unsigned int getOffset() { return base_. |
| 41 | offset; } |
| 42 | bool gotElementEnd() { return base_.gotElementEnd != 0; } |
| 43 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 44 | private: |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 45 | struct ndn_BinaryXMLStructureDecoder base_; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 46 | }; |
Jeff Thompson | 4e27899 | 2013-06-26 18:59:17 -0700 | [diff] [blame] | 47 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | #endif /* BINARYXMLSTRUCTUREDECODER_HPP */ |
| 51 | |