Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_DYNAMICUCHARARRAY_H |
| 8 | #define NDN_DYNAMICUCHARARRAY_H |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | struct ndn_DynamicUCharArray { |
| 15 | unsigned char *array; |
| 16 | unsigned int length; |
| 17 | unsigned char (*realloc)(unsigned char *array, unsigned int length); |
| 18 | }; |
| 19 | |
| 20 | static inline void ndn_DynamicUCharArray_init |
| 21 | (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char (*reallocFunction)(unsigned char *, unsigned int)) |
| 22 | { |
| 23 | self->array = array; |
| 24 | self->length = length; |
| 25 | self->realloc = reallocFunction; |
| 26 | } |
| 27 | |
| 28 | char *ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length); |
| 29 | |
| 30 | static inline char *ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length) |
| 31 | { |
| 32 | if (self->length >= length) |
| 33 | return 0; |
| 34 | |
| 35 | return ndn_DynamicUCharArray_reallocArray(self, length); |
| 36 | } |
| 37 | |
| 38 | static inline char *ndn_DynamicUCharArray_set |
| 39 | (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset) |
| 40 | { |
| 41 | char *error; |
| 42 | if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset)) |
| 43 | return error; |
| 44 | ndn_memcpy(self->array + offset, value, valueLength); |
| 45 | }; |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | #endif |
| 52 | |