Jeff Thompson | 2d28d49 | 2013-07-01 17:57:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BINARYXMLENCODER_HPP |
| 8 | #define NDN_BINARYXMLENCODER_HPP |
| 9 | |
| 10 | #include <cstdlib> |
| 11 | #include <stdexcept> |
Jeff Thompson | 58d798f | 2013-07-02 14:16:25 -0700 | [diff] [blame^] | 12 | #include <vector> |
Jeff Thompson | 2d28d49 | 2013-07-01 17:57:53 -0700 | [diff] [blame] | 13 | #include "../c/encoding/BinaryXMLEncoder.h" |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | /** |
| 18 | * A BinaryXMLEncoder wraps a C ndn_BinaryXMLEncoder struct and related functions. |
| 19 | */ |
| 20 | class BinaryXMLEncoder { |
| 21 | public: |
| 22 | /** |
| 23 | * Initialize the base ndn_BinaryXMLEncoder struct with an initial array of 16 bytes. Use simpleRealloc. |
| 24 | */ |
| 25 | BinaryXMLEncoder() |
| 26 | { |
| 27 | const unsigned int initialLength = 16; |
| 28 | ndn_BinaryXMLEncoder_init(&base_, (unsigned char *)malloc(initialLength), initialLength, simpleRealloc); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Wrap the C stdlib realloc to convert to/from void * to unsigned char *. |
| 33 | * @param array the allocated array buffer to realloc |
| 34 | * @param length the length for the new array buffer |
| 35 | * @return the new allocated array buffer |
| 36 | */ |
| 37 | static unsigned char *simpleRealloc(unsigned char *array, unsigned int length) |
| 38 | { |
| 39 | return (unsigned char *)realloc(array, length); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Return a pointer to the base ndn_BinaryXMLEncoder struct. |
| 44 | * @return |
| 45 | */ |
| 46 | struct ndn_BinaryXMLEncoder *getEncoder() { return &base_; } |
| 47 | |
Jeff Thompson | 58d798f | 2013-07-02 14:16:25 -0700 | [diff] [blame^] | 48 | /** |
| 49 | * Copy the encoded bytes to the end of the buffer. |
| 50 | * @param buffer a vector to receive the copy |
| 51 | */ |
| 52 | void appendTo(std::vector<unsigned char> &buffer) |
| 53 | { |
| 54 | buffer.insert(buffer.end(), base_.output.array, base_.output.array + base_.offset); |
| 55 | } |
| 56 | |
Jeff Thompson | 2d28d49 | 2013-07-01 17:57:53 -0700 | [diff] [blame] | 57 | private: |
| 58 | struct ndn_BinaryXMLEncoder base_; |
| 59 | }; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | #endif |