blob: 2b09e25f8b9a4f85c077250877d8de142b235857 [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"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "binary-xml-structure-decoder.h"
11#include "../util/dynamic-uchar-array.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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070028 * self is the pointer to this ndn_ElementListener struct. See ndn_BinaryXmlElementReader_onReceivedData.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070029 */
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/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070037 * 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
Jeff Thompsonb42e3632013-07-15 16:51:42 -070039 * (*elementListener->onReceivedElement)(element, elementLength) with the element.
40 * This handles the case where a single call to onReceivedData may contain multiple elements.
41 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042struct ndn_BinaryXmlElementReader {
Jeff Thompsonb42e3632013-07-15 16:51:42 -070043 struct ndn_ElementListener *elementListener;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070044 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 Thompsonf0fea002013-07-30 17:22:42 -070051 * Initialize an ndn_BinaryXmlElementReader struct with the elementListener and a buffer for saving partial data.
52 * @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 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070058static inline void ndn_BinaryXmlElementReader_init
59 (struct ndn_BinaryXmlElementReader *self, struct ndn_ElementListener *elementListener,
Jeff Thompson5e275b42013-07-16 19:10:11 -070060 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;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070063 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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070071 * @param self pointer to the ndn_BinaryXmlElementReader struct
Jeff Thompsonb42e3632013-07-15 16:51:42 -070072 * @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 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070076ndn_Error ndn_BinaryXmlElementReader_onReceivedData
77 (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070078
79#ifdef __cplusplus
80}
81#endif
82
83#endif