Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 1 | /** |
| 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 Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 11 | #include "../util/DynamicUCharArray.h" |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 12 | |
| 13 | #ifdef __cplusplus |
| 14 | extern "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 | */ |
| 20 | struct 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 | */ |
| 30 | static 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 | */ |
| 42 | struct ndn_BinaryXMLElementReader { |
| 43 | struct ndn_ElementListener *elementListener; |
| 44 | struct ndn_BinaryXMLStructureDecoder structureDecoder; |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 45 | int usePartialData; |
| 46 | struct ndn_DynamicUCharArray partialData; |
| 47 | unsigned int partialDataLength; |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /** |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 51 | * Initialize an ndn_BinaryXMLElementReader struct with the elementListener and a buffer for saving partial data. |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 52 | * @param self pointer to the ndn_BinaryXMLElementReader struct |
| 53 | * @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXMLElementReader_onReceivedData. |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 54 | * @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 Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 57 | */ |
| 58 | static inline void ndn_BinaryXMLElementReader_init |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 59 | (struct ndn_BinaryXMLElementReader *self, struct ndn_ElementListener *elementListener, |
| 60 | unsigned char *buffer, unsigned int bufferLength, unsigned char * (*reallocFunction)(unsigned char *, unsigned int)) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 61 | { |
| 62 | self->elementListener = elementListener; |
| 63 | ndn_BinaryXMLStructureDecoder_init(&self->structureDecoder); |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 64 | self->usePartialData = 0; |
| 65 | ndn_DynamicUCharArray_init(&self->partialData, buffer, bufferLength, reallocFunction); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 66 | } |
| 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 | */ |
| 76 | ndn_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 |