blob: 0e9e62b2fcd930a2c972a6d5bf8552d7b279f415 [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/**
17 * A BinaryXMLEncoder wraps a C ndn_BinaryXMLEncoder struct and related functions.
18 */
19class BinaryXMLEncoder {
20public:
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;
27 ndn_BinaryXMLEncoder_init(&base_, (unsigned char *)malloc(initialLength), initialLength, simpleRealloc);
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 /**
42 * Return a pointer to the base ndn_BinaryXMLEncoder struct.
43 * @return
44 */
45 struct ndn_BinaryXMLEncoder *getEncoder() { return &base_; }
46
Jeff Thompson58d798f2013-07-02 14:16:25 -070047 /**
48 * Copy the encoded bytes to the end of the buffer.
49 * @param buffer a vector to receive the copy
50 */
51 void appendTo(std::vector<unsigned char> &buffer)
52 {
53 buffer.insert(buffer.end(), base_.output.array, base_.output.array + base_.offset);
54 }
55
Jeff Thompson2d28d492013-07-01 17:57:53 -070056private:
57 struct ndn_BinaryXMLEncoder base_;
58};
59
60}
61
62#endif