diff options
author | Ethan Yonker <dees_troy@teamw.in> | 2016-01-11 05:26:51 +0100 |
---|---|---|
committer | Dees Troy <dees_troy@teamw.in> | 2016-01-15 01:27:45 +0100 |
commit | 6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b (patch) | |
tree | 6d971c658a3ca67fae74178fae46a250ad2ad604 /toolbox/dynarray.h | |
parent | Improve sdcard partitioning process (diff) | |
download | android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.gz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.bz2 android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.lz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.xz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.zst android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.zip |
Diffstat (limited to '')
-rw-r--r-- | toolbox/dynarray.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/toolbox/dynarray.h b/toolbox/dynarray.h new file mode 100644 index 000000000..f73fb3b9c --- /dev/null +++ b/toolbox/dynarray.h @@ -0,0 +1,80 @@ +#ifndef DYNARRAY_H +#define DYNARRAY_H + +#include <stddef.h> + +/* simple dynamic array of pointers */ +typedef struct { + int count; + int capacity; + void** items; +} dynarray_t; + +#define DYNARRAY_INITIALIZER { 0, 0, NULL } + +void dynarray_init( dynarray_t *a ); +void dynarray_done( dynarray_t *a ); + +void dynarray_append( dynarray_t *a, void* item ); + +/* Used to iterate over a dynarray_t + * _array :: pointer to the array + * _item_type :: type of objects pointed to by the array + * _item :: name of a local variable defined within the loop + * with type '_item_type' + * _stmnt :: C statement that will be executed in each iteration. + * + * You case use 'break' and 'continue' within _stmnt + * + * This macro is only intended for simple uses. I.e. do not add or + * remove items from the array during iteration. + */ +#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ + do { \ + int _nn_##__LINE__ = 0; \ + for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ + _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ + _stmnt; \ + } \ + } while (0) + +#define DYNARRAY_FOREACH(_array,_item,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) + +/* Simple dynamic string arrays + * + * NOTE: A strlist_t owns the strings it references. + */ +typedef dynarray_t strlist_t; + +#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER + +/* Used to iterate over a strlist_t + * _list :: pointer to strlist_t object + * _string :: name of local variable name defined within the loop with + * type 'char*' + * _stmnt :: C statement executed in each iteration + * + * This macro is only intended for simple uses. Do not add or remove items + * to/from the list during iteration. + */ +#define STRLIST_FOREACH(_list,_string,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) + +void strlist_init( strlist_t *list ); + +/* note: strlist_done will free all the strings owned by the list */ +void strlist_done( strlist_t *list ); + +/* append a new string made of the first 'slen' characters from 'str' + * followed by a trailing zero. + */ +void strlist_append_b( strlist_t *list, const void* str, size_t slen ); + +/* append the copy of a given input string to a strlist_t */ +void strlist_append_dup( strlist_t *list, const char *str); + +/* sort the strings in a given list (using strcmp) */ +void strlist_sort( strlist_t *list ); + +#endif /* DYNARRAY_H */
\ No newline at end of file |