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