blob: 1fd4244ff76af3ac507b59d82376bfffc27e1718 [file] [log] [blame]
Jeff Thompson2d28d492013-07-01 17:57:53 -07001/*
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 Thompson58d798f2013-07-02 14:16:25 -070012#include <vector>
Jeff Thompson2d28d492013-07-01 17:57:53 -070013#include "../c/encoding/BinaryXMLEncoder.h"
14
15namespace ndn {
16
17/**
18 * A BinaryXMLEncoder wraps a C ndn_BinaryXMLEncoder struct and related functions.
19 */
20class BinaryXMLEncoder {
21public:
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 Thompson58d798f2013-07-02 14:16:25 -070048 /**
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 Thompson2d28d492013-07-01 17:57:53 -070057private:
58 struct ndn_BinaryXMLEncoder base_;
59};
60
61}
62
63#endif