summaryrefslogtreecommitdiff
path: root/testing/misc_tools.h
diff options
context:
space:
mode:
authorAnony Moose <noMail@nomail.su>2013-08-25 17:52:22 +0000
committerAnony Moose <noMail@nomail.su>2013-08-25 17:55:54 +0000
commit76761a605b153ed078fd3d42a81e06bb53366b17 (patch)
treed66a0d90655e731371c5ea9144bfc4e1878ac8f7 /testing/misc_tools.h
parent9a7f7b5ec4033b83bbde8a7347895994e7aad6c4 (diff)
Modified Lossless_UDP connections to work with tox_array.
Diffstat (limited to 'testing/misc_tools.h')
-rw-r--r--testing/misc_tools.h38
1 files changed, 26 insertions, 12 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h
index 7c37ba38..3b71d8c4 100644
--- a/testing/misc_tools.h
+++ b/testing/misc_tools.h
@@ -117,7 +117,7 @@ static inline void tox_list_remove(tox_list *lst)
117 ************************************************************/ 117 ************************************************************/
118 118
119typedef struct tox_array { 119typedef struct tox_array {
120 void *data; 120 void *data; /* last elem is data[len-1] */
121 uint32_t len; 121 uint32_t len;
122 size_t elem_size; /* in bytes */ 122 size_t elem_size; /* in bytes */
123} tox_array; 123} tox_array;
@@ -135,34 +135,48 @@ static inline void tox_array_delete(tox_array *arr)
135 arr->len = arr->elem_size = 0; 135 arr->len = arr->elem_size = 0;
136} 136}
137 137
138static inline void _tox_array_push(tox_array *arr, uint8_t *item) 138static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item)
139{ 139{
140 arr->data = realloc(arr->data, arr->elem_size * (arr->len+1)); 140 arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
141 141
142 memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size); 142 if (arr->data == NULL)
143 return 0;
144
145 if (item != NULL)
146 memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
143 arr->len++; 147 arr->len++;
148
149 return 1;
144} 150}
145#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item))) 151#define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item)))
146 152
147/* Deletes num items from array. 153/* Deletes num items from array.
148 * Not same as pop in stacks, because to access elements you use data. 154 * Not same as pop in stacks, because to access elements you use data.
149 */ 155 */
150static inline void tox_array_pop(tox_array *arr, uint32_t num) 156static inline void tox_array_pop(tox_array *arr, uint32_t num)
151{ 157{
152 arr->len--; 158 if (num == 0)
159 num = 1;
160 arr->len -= num;
153 arr->data = realloc(arr->data, arr->elem_size*arr->len); 161 arr->data = realloc(arr->data, arr->elem_size*arr->len);
154} 162}
155 163
164/* TODO: do not use type, since we track the needed info in
165 * elem_size (but array user will have to cast)
166 */
156#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i]) 167#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i])
157 168
158/* TODO: what about nested for loops? */ 169/* This version requires C99 (declaring variables inside for loop)
159#define tox_array_for_each(arr, type) \ 170#define tox_array_for_each(arr, type, tmp_name) \
160 for ( \ 171 for ( \
161 struct { type tmp; uint32_t i; } tox_array_tmp = { tox_array_get(arr, 0, type), 0 }; \ 172 struct { type val; uint32_t i; } tmp_name = { tox_array_get(arr, 0, type), 0 }; \
162 tox_array_tmp.i != (arr)->len; \ 173 tmp_name.i != (arr)->len; \
163 tox_array_tmp.tmp = tox_array_get(arr, ++tox_array_tmp.i, type) \ 174 tmp_name.val = tox_array_get(arr, ++tmp_name.i, type) \
164 ) 175 )
176*/
165 177
166#define TOX_ARRAY_TMP (tox_array_tmp.tmp) 178#define tox_array_for_each(arr, type, tmp_name) \
179 type tmp_name; uint32_t tmp_name ## _i = 0; \
180 for (; tmp_name ## _i != (arr)->len; tmp_name = tox_array_get(arr, ++ tmp_name ## _i, type))
167 181
168#endif // MISC_TOOLS_H \ No newline at end of file 182#endif // MISC_TOOLS_H