Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jeff Thompson | 19f0118 | 2013-07-09 19:29:36 -0700 | [diff] [blame] | 6 | #ifndef NDN_BINARYXMLSTRUCTUREDECODER_HPP |
| 7 | #define NDN_BINARYXMLSTRUCTUREDECODER_HPP |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 9 | #include <stdexcept> |
Jeff Thompson | 58b732c | 2013-07-01 16:51:36 -0700 | [diff] [blame] | 10 | #include "../c/encoding/BinaryXMLStructureDecoder.h" |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 11 | |
| 12 | namespace ndn { |
Jeff Thompson | 4e27899 | 2013-06-26 18:59:17 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
Jeff Thompson | 6bd3a2f | 2013-07-01 17:17:17 -0700 | [diff] [blame] | 15 | * A BinaryXMLStructureDecoder wraps a C ndn_BinaryXMLStructureDecoder struct and related functions. |
Jeff Thompson | 4e27899 | 2013-06-26 18:59:17 -0700 | [diff] [blame] | 16 | */ |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 17 | class BinaryXMLStructureDecoder { |
| 18 | public: |
Jeff Thompson | ffbe8ac | 2013-07-02 11:43:09 -0700 | [diff] [blame] | 19 | BinaryXMLStructureDecoder() |
| 20 | { |
Jeff Thompson | 9cd4590 | 2013-07-15 16:15:04 -0700 | [diff] [blame] | 21 | ndn_BinaryXMLStructureDecoder_init(&structureDecoder_); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 24 | /** |
| 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 Thompson | ffbe8ac | 2013-07-02 11:43:09 -0700 | [diff] [blame] | 32 | bool findElementEnd(unsigned char *input, unsigned int inputLength) |
| 33 | { |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 34 | ndn_Error error; |
Jeff Thompson | 9cd4590 | 2013-07-15 16:15:04 -0700 | [diff] [blame] | 35 | if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&structureDecoder_, input, inputLength)) |
Jeff Thompson | b0e4fad | 2013-07-08 01:16:48 -0700 | [diff] [blame] | 36 | throw std::runtime_error(ndn_getErrorString(error)); |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 37 | return gotElementEnd(); |
| 38 | } |
| 39 | |
Jeff Thompson | 9cd4590 | 2013-07-15 16:15:04 -0700 | [diff] [blame] | 40 | unsigned int getOffset() const { return structureDecoder_.offset; } |
| 41 | bool gotElementEnd() const { return structureDecoder_.gotElementEnd != 0; } |
Jeff Thompson | 71d8f77 | 2013-06-27 14:34:11 -0700 | [diff] [blame] | 42 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 43 | private: |
Jeff Thompson | 9cd4590 | 2013-07-15 16:15:04 -0700 | [diff] [blame] | 44 | struct ndn_BinaryXMLStructureDecoder structureDecoder_; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 45 | }; |
Jeff Thompson | 4e27899 | 2013-06-26 18:59:17 -0700 | [diff] [blame] | 46 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Thompson | 19f0118 | 2013-07-09 19:29:36 -0700 | [diff] [blame] | 49 | #endif |