summaryrefslogtreecommitdiff
path: root/toxcore/misc_tools.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/misc_tools.h')
-rw-r--r--toxcore/misc_tools.h185
1 files changed, 0 insertions, 185 deletions
diff --git a/toxcore/misc_tools.h b/toxcore/misc_tools.h
index 170dd2b1..543338b5 100644
--- a/toxcore/misc_tools.h
+++ b/toxcore/misc_tools.h
@@ -24,191 +24,6 @@
24#ifndef MISC_TOOLS_H 24#ifndef MISC_TOOLS_H
25#define MISC_TOOLS_H 25#define MISC_TOOLS_H
26 26
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h> /* for memcpy() */
30
31
32
33/************************Linked List***********************
34 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
35 * TODO: Update wiki.
36 **********************************************************/
37
38/* Example usage
39
40This sample program makes a new struct which contains a
41character and a tox_list_t. It then prompts a user for
42input until he enters q or e. It then adds each character
43to the list, and uses a special for loop to print them.
44It then removes all the 'z' characters, and prints the list
45again.
46
47//Notice that the data to be put in the list *contains* tox_list_t;
48//usually, this is the other way around!
49typedef struct tox_string {
50 char c;
51 tox_list_t tox_lst; //Notice that tox_lst is *NOT* a pointer.
52} tox_string_t;
53
54int main()
55{
56 tox_list_t head;
57 tox_list_new(&head); //initialize head
58
59 //input a new character, until user enters q or e
60 char c = '\0';
61 while (c != 'q' && c != 'e') {
62 scanf("%c", &c);
63 tox_string_t* tmp = malloc(sizeof(tox_string_t));
64 tmp->c = c;
65 tox_list_add(&head, &tmp->tox_lst); //add it to the list
66 }
67
68TOX_LIST_FOR_EACH() takes a struct tox_list and a name for a temporary pointer to use in the loop.
69
70TOX_LIST_GET_VALUE() uses magic to return an instance of a structure that contains tox_list_t.
71You have to give it a temporary tox_string_t, name of tox_list_t member inside our structure (tox_lst),
72and the type of structure to return.
73
74 TOX_LIST_FOR_EACH(head, tmp)
75 printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c);
76
77 TOX_LIST_FOR_EACH(head, tmp) {
78 if (TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c == 'z') {
79 //If you delete tmp, you have to quit the loop, or it will go on infinitly.
80 //This will be fixed later on.
81 tox_list_remove(tmp);
82 break;
83 }
84 }
85
86 printf("\n");
87 TOX_LIST_FOR_EACH(head, tmp)
88 printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c);
89
90
91 return 0;
92}
93*/
94
95#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
96 (&(((parent_type*)0)->var_name_in_parent))
97
98#define GET_PARENT(var, var_name_in_parent, parent_type) \
99 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
100
101#define TOX_LIST_FOR_EACH(lst, tmp_name) \
102 for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
103
104#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type)
105
106typedef struct tox_list {
107 struct tox_list *prev, *next;
108} tox_list;
109
110/* return new tox_list_t. */
111static inline void tox_list_new(tox_list *lst)
112{
113 lst->prev = lst->next = lst;
114}
115
116/* Inserts a new tox_lst after lst and returns it. */
117static inline void tox_list_add(tox_list *lst, tox_list *new_lst)
118{
119 tox_list_new(new_lst);
120
121 new_lst->next = lst->next;
122 new_lst->next->prev = new_lst;
123
124 lst->next = new_lst;
125 new_lst->prev = lst;
126}
127
128static inline void tox_list_remove(tox_list *lst)
129{
130 lst->prev->next = lst->next;
131 lst->next->prev = lst->prev;
132}
133
134/****************************Array***************************
135 * Array which manages its own memory allocation.
136 * It stores copy of data (not pointers).
137 * TODO: Add wiki info usage.
138 ************************************************************/
139
140typedef struct tox_array {
141 uint8_t *data; /* last elem is data[len-1] */
142 uint32_t len;
143 size_t elem_size; /* in bytes */
144} tox_array;
145
146static inline void tox_array_init(tox_array *arr, size_t elem_size)
147{
148 arr->len = 0;
149 arr->elem_size = elem_size;
150 arr->data = NULL;
151}
152
153static inline void tox_array_delete(tox_array *arr)
154{
155 free(arr->data);
156 arr->len = arr->elem_size = 0;
157}
158
159static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item)
160{
161 uint8_t *temp = realloc(arr->data, arr->elem_size * (arr->len + 1));
162
163 if (temp == NULL)
164 return 0;
165
166 arr->data = temp;
167
168 if (item != NULL)
169 memcpy(arr->data + arr->elem_size * arr->len, item, arr->elem_size);
170
171 arr->len++;
172
173 return 1;
174}
175#define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item)))
176
177/* Deletes num items from array.
178 * Not same as pop in stacks, because to access elements you use data.
179 */
180static inline void tox_array_pop(tox_array *arr, uint32_t num)
181{
182 if (num == 0)
183 return;
184
185 if (num > arr->len)
186 return;
187
188 if (arr->len == num) {
189 free(arr->data);
190 arr->data = NULL;
191 arr->len = 0;
192 return;
193 }
194
195 uint8_t *temp = realloc(arr->data, arr->elem_size * (arr->len - num));
196
197 if (temp == NULL)
198 return;
199
200 arr->len -= num;
201 arr->data = temp;
202}
203
204/* TODO: return ptr and do not take type */
205#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i])
206
207
208#define tox_array_for_each(arr, type, tmp_name) \
209 type *tmp_name = &tox_array_get(arr, 0, type); uint32_t tmp_name ## _i = 0; \
210 for (; tmp_name ## _i < (arr)->len; tmp_name = &tox_array_get(arr, ++ tmp_name ## _i, type))
211
212/****************************Algorithms*************************** 27/****************************Algorithms***************************
213 * Macro/generic definitions for useful algorithms 28 * Macro/generic definitions for useful algorithms
214 *****************************************************************/ 29 *****************************************************************/