blob: bb703b638e32f9e1401cf8b5f6a202b5401cdb11 [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;
Jeff Thompson167c4c32013-07-15 16:14:52 -070027 ndn_BinaryXMLEncoder_init(&encoder_, (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 /**
42 * Return a pointer to the base ndn_BinaryXMLEncoder struct.
43 * @return
44 */
Jeff Thompson167c4c32013-07-15 16:14:52 -070045 struct ndn_BinaryXMLEncoder *getEncoder() { return &encoder_; }
Jeff Thompson2d28d492013-07-01 17:57:53 -070046
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 {
Jeff Thompson167c4c32013-07-15 16:14:52 -070053 buffer.insert(buffer.end(), encoder_.output.array, encoder_.output.array + encoder_.offset);
Jeff Thompson58d798f2013-07-02 14:16:25 -070054 }
55
Jeff Thompson2d28d492013-07-01 17:57:53 -070056private:
Jeff Thompson167c4c32013-07-15 16:14:52 -070057 struct ndn_BinaryXMLEncoder encoder_;
Jeff Thompson2d28d492013-07-01 17:57:53 -070058};
59
60}
61
62#endif