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" |
| 11 | |
| 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
| 16 | /** An ndn_ElementListener struct holds a function pointer onReceivedElement. You can extend this struct with data that |
| 17 | * will be passed to onReceivedElement. |
| 18 | */ |
| 19 | struct ndn_ElementListener { |
| 20 | void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength); /**< see ndn_ElementListener_init */ |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Initialize an ndn_ElementListener struct to use the onReceivedElement function pointer. |
| 25 | * @param self pointer to the ndn_ElementListener struct |
| 26 | * @param onReceivedElement pointer to a function which is called when an entire binary XML element is received. |
| 27 | * self is the pointer to this ndn_ElementListener struct. See ndn_BinaryXMLElementReader_onReceivedData. |
| 28 | */ |
| 29 | static inline void ndn_ElementListener_init |
| 30 | (struct ndn_ElementListener *self, void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength)) |
| 31 | { |
| 32 | self->onReceivedElement = onReceivedElement; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * A BinaryXmlElementReader lets you call ndn_BinaryXMLElementReader_onReceivedData multiple times which uses an |
| 37 | * ndn_BinaryXMLStructureDecoder to detect the end of a binary XML element and calls |
| 38 | * (*elementListener->onReceivedElement)(element, elementLength) with the element. |
| 39 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 40 | */ |
| 41 | struct ndn_BinaryXMLElementReader { |
| 42 | struct ndn_ElementListener *elementListener; |
| 43 | struct ndn_BinaryXMLStructureDecoder structureDecoder; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Initialize an ndn_BinaryXMLElementReader struct with the elementListener. |
| 48 | * @param self pointer to the ndn_BinaryXMLElementReader struct |
| 49 | * @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXMLElementReader_onReceivedData. |
| 50 | */ |
| 51 | static inline void ndn_BinaryXMLElementReader_init |
| 52 | (struct ndn_BinaryXMLElementReader *self, struct ndn_ElementListener *elementListener) |
| 53 | { |
| 54 | self->elementListener = elementListener; |
| 55 | ndn_BinaryXMLStructureDecoder_init(&self->structureDecoder); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Continue to read binary XML data until the end of an element, then call (*elementListener->onReceivedElement)(element, elementLength). |
| 60 | * The buffer passed to onReceivedElement is only valid during this call. If you need the data later, you must copy. |
| 61 | * @param self pointer to the ndn_BinaryXMLElementReader struct |
| 62 | * @param data pointer to the buffer with the binary XML bytes |
| 63 | * @param dataLength length of data |
| 64 | * @return 0 for success, else an error code |
| 65 | */ |
| 66 | ndn_Error ndn_BinaryXMLElementReader_onReceivedData |
| 67 | (struct ndn_BinaryXMLElementReader *self, unsigned char *data, unsigned int dataLength); |
| 68 | |
| 69 | #ifdef __cplusplus |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | #endif |