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