blob: 9a80debece08150e39413adb47ccfbbe764fef76 [file] [log] [blame]
Jeff Thompsonb42e3632013-07-15 16:51:42 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#ifndef NDN_BINARYXMLELEMENTREADER_H
7#define NDN_BINARYXMLELEMENTREADER_H
8
9#include "../errors.h"
10#include "BinaryXMLStructureDecoder.h"
Jeff Thompson5e275b42013-07-16 19:10:11 -070011#include "../util/DynamicUCharArray.h"
Jeff Thompsonb42e3632013-07-15 16:51:42 -070012
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17/** An ndn_ElementListener struct holds a function pointer onReceivedElement. You can extend this struct with data that
18 * will be passed to onReceivedElement.
19 */
20struct ndn_ElementListener {
21 void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength); /**< see ndn_ElementListener_init */
22};
23
24/**
25 * Initialize an ndn_ElementListener struct to use the onReceivedElement function pointer.
26 * @param self pointer to the ndn_ElementListener struct
27 * @param onReceivedElement pointer to a function which is called when an entire binary XML element is received.
28 * self is the pointer to this ndn_ElementListener struct. See ndn_BinaryXMLElementReader_onReceivedData.
29 */
30static inline void ndn_ElementListener_init
31 (struct ndn_ElementListener *self, void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength))
32{
33 self->onReceivedElement = onReceivedElement;
34}
35
36/**
37 * A BinaryXmlElementReader lets you call ndn_BinaryXMLElementReader_onReceivedData multiple times which uses an
38 * ndn_BinaryXMLStructureDecoder to detect the end of a binary XML element and calls
39 * (*elementListener->onReceivedElement)(element, elementLength) with the element.
40 * This handles the case where a single call to onReceivedData may contain multiple elements.
41 */
42struct ndn_BinaryXMLElementReader {
43 struct ndn_ElementListener *elementListener;
44 struct ndn_BinaryXMLStructureDecoder structureDecoder;
Jeff Thompson5e275b42013-07-16 19:10:11 -070045 int usePartialData;
46 struct ndn_DynamicUCharArray partialData;
47 unsigned int partialDataLength;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070048};
49
50/**
Jeff Thompson5e275b42013-07-16 19:10:11 -070051 * Initialize an ndn_BinaryXMLElementReader struct with the elementListener and a buffer for saving partial data.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070052 * @param self pointer to the ndn_BinaryXMLElementReader struct
53 * @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXMLElementReader_onReceivedData.
Jeff Thompson5e275b42013-07-16 19:10:11 -070054 * @param buffer the allocated buffer. If reallocFunction is null, this should be large enough to save a full element, perhaps 8000 bytes.
55 * @param bufferLength the length of the buffer
56 * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070057 */
58static inline void ndn_BinaryXMLElementReader_init
Jeff Thompson5e275b42013-07-16 19:10:11 -070059 (struct ndn_BinaryXMLElementReader *self, struct ndn_ElementListener *elementListener,
60 unsigned char *buffer, unsigned int bufferLength, unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070061{
62 self->elementListener = elementListener;
63 ndn_BinaryXMLStructureDecoder_init(&self->structureDecoder);
Jeff Thompson5e275b42013-07-16 19:10:11 -070064 self->usePartialData = 0;
65 ndn_DynamicUCharArray_init(&self->partialData, buffer, bufferLength, reallocFunction);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070066}
67
68/**
69 * Continue to read binary XML data until the end of an element, then call (*elementListener->onReceivedElement)(element, elementLength).
70 * The buffer passed to onReceivedElement is only valid during this call. If you need the data later, you must copy.
71 * @param self pointer to the ndn_BinaryXMLElementReader struct
72 * @param data pointer to the buffer with the binary XML bytes
73 * @param dataLength length of data
74 * @return 0 for success, else an error code
75 */
76ndn_Error ndn_BinaryXMLElementReader_onReceivedData
77 (struct ndn_BinaryXMLElementReader *self, unsigned char *data, unsigned int dataLength);
78
79#ifdef __cplusplus
80}
81#endif
82
83#endif