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