David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 1 | #ifndef DYNARRAY_H |
| 2 | #define DYNARRAY_H |
| 3 | |
| 4 | #include <stddef.h> |
| 5 | |
| 6 | /* simple dynamic array of pointers */ |
| 7 | typedef struct { |
| 8 | int count; |
| 9 | int capacity; |
| 10 | void** items; |
| 11 | } dynarray_t; |
| 12 | |
| 13 | #define DYNARRAY_INITIALIZER { 0, 0, NULL } |
| 14 | |
| 15 | void dynarray_init( dynarray_t *a ); |
| 16 | void dynarray_done( dynarray_t *a ); |
| 17 | |
| 18 | void dynarray_append( dynarray_t *a, void* item ); |
| 19 | |
| 20 | /* Used to iterate over a dynarray_t |
| 21 | * _array :: pointer to the array |
| 22 | * _item_type :: type of objects pointed to by the array |
| 23 | * _item :: name of a local variable defined within the loop |
| 24 | * with type '_item_type' |
| 25 | * _stmnt :: C statement that will be executed in each iteration. |
| 26 | * |
| 27 | * You case use 'break' and 'continue' within _stmnt |
| 28 | * |
| 29 | * This macro is only intended for simple uses. I.e. do not add or |
| 30 | * remove items from the array during iteration. |
| 31 | */ |
| 32 | #define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ |
| 33 | do { \ |
| 34 | int _nn_##__LINE__ = 0; \ |
| 35 | for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ |
| 36 | _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ |
| 37 | _stmnt; \ |
| 38 | } \ |
| 39 | } while (0) |
| 40 | |
| 41 | #define DYNARRAY_FOREACH(_array,_item,_stmnt) \ |
| 42 | DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) |
| 43 | |
| 44 | /* Simple dynamic string arrays |
| 45 | * |
| 46 | * NOTE: A strlist_t owns the strings it references. |
| 47 | */ |
| 48 | typedef dynarray_t strlist_t; |
| 49 | |
| 50 | #define STRLIST_INITIALIZER DYNARRAY_INITIALIZER |
| 51 | |
| 52 | /* Used to iterate over a strlist_t |
| 53 | * _list :: pointer to strlist_t object |
| 54 | * _string :: name of local variable name defined within the loop with |
| 55 | * type 'char*' |
| 56 | * _stmnt :: C statement executed in each iteration |
| 57 | * |
| 58 | * This macro is only intended for simple uses. Do not add or remove items |
| 59 | * to/from the list during iteration. |
| 60 | */ |
| 61 | #define STRLIST_FOREACH(_list,_string,_stmnt) \ |
| 62 | DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) |
| 63 | |
| 64 | void strlist_init( strlist_t *list ); |
| 65 | |
| 66 | /* note: strlist_done will free all the strings owned by the list */ |
| 67 | void strlist_done( strlist_t *list ); |
| 68 | |
| 69 | /* append a new string made of the first 'slen' characters from 'str' |
| 70 | * followed by a trailing zero. |
| 71 | */ |
| 72 | void strlist_append_b( strlist_t *list, const void* str, size_t slen ); |
| 73 | |
| 74 | /* append the copy of a given input string to a strlist_t */ |
| 75 | void strlist_append_dup( strlist_t *list, const char *str); |
| 76 | |
| 77 | /* sort the strings in a given list (using strcmp) */ |
| 78 | void strlist_sort( strlist_t *list ); |
| 79 | |
| 80 | #endif /* DYNARRAY_H */ |