blob: c1f8a1f16ae96e854687f577abe07e8a93141f15 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson2d28d492013-07-01 17:57:53 -07004 */
5
6#ifndef NDN_BINARYXMLENCODER_HPP
7#define NDN_BINARYXMLENCODER_HPP
8
9#include <cstdlib>
10#include <stdexcept>
Jeff Thompson58d798f2013-07-02 14:16:25 -070011#include <vector>
Jeff Thompson2d28d492013-07-01 17:57:53 -070012#include "../c/encoding/BinaryXMLEncoder.h"
13
14namespace ndn {
15
16/**
Jeff Thompson0adcbea2013-07-15 16:29:52 -070017 * A BinaryXMLEncoder extends a C ndn_BinaryXMLEncoder struct and wraps related functions.
Jeff Thompson2d28d492013-07-01 17:57:53 -070018 */
Jeff Thompson0adcbea2013-07-15 16:29:52 -070019class BinaryXMLEncoder : public ndn_BinaryXMLEncoder {
Jeff Thompson2d28d492013-07-01 17:57:53 -070020public:
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 Thompson0adcbea2013-07-15 16:29:52 -070027 ndn_BinaryXMLEncoder_init(this, (unsigned char *)malloc(initialLength), initialLength, simpleRealloc);
Jeff Thompson2d28d492013-07-01 17:57:53 -070028 }
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 Thompson58d798f2013-07-02 14:16:25 -070042 * 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 Thompson0adcbea2013-07-15 16:29:52 -070047 buffer.insert(buffer.end(), this->output.array, this->output.array + this->offset);
Jeff Thompson58d798f2013-07-02 14:16:25 -070048 }
Jeff Thompson2d28d492013-07-01 17:57:53 -070049};
50
51}
52
53#endif