blob: 0ca54fdf64ecb7edf4afe33bb3a514db9c56ff36 [file] [log] [blame]
Ethan Yonker6bb26b52016-01-10 22:26:51 -06001#ifndef DYNARRAY_H
2#define DYNARRAY_H
3
Ethan Yonker916cae82016-01-22 11:32:57 -06004// These functions are now found in system/core/toolbox/ls.c
5
Ethan Yonker6bb26b52016-01-10 22:26:51 -06006#include <stddef.h>
7
8/* simple dynamic array of pointers */
9typedef struct {
10 int count;
11 int capacity;
12 void** items;
13} dynarray_t;
14
15#define DYNARRAY_INITIALIZER { 0, 0, NULL }
16
17void dynarray_init( dynarray_t *a );
18void dynarray_done( dynarray_t *a );
19
20void 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 */
50typedef 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
66void strlist_init( strlist_t *list );
67
68/* note: strlist_done will free all the strings owned by the list */
69void 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 */
74void 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 */
77void strlist_append_dup( strlist_t *list, const char *str);
78
79/* sort the strings in a given list (using strcmp) */
80void strlist_sort( strlist_t *list );
81
Ethan Yonker916cae82016-01-22 11:32:57 -060082#endif /* DYNARRAY_H */