summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/misc_tools.h81
1 files changed, 7 insertions, 74 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h
index cc5abeda..550465f4 100644
--- a/testing/misc_tools.h
+++ b/testing/misc_tools.h
@@ -26,15 +26,9 @@
26 26
27unsigned char * hex_string_to_bin(char hex_string[]); 27unsigned char * hex_string_to_bin(char hex_string[]);
28 28
29/* WARNING(msg) takes a printf()-styled string and prints it 29/*********************Debugging Macros********************
30 * with some additional details. 30 * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
31 * ERROR(exit_status, msg) does the same thing as WARNING(), but 31 *********************************************************/
32 * also exits the program with the given exit status.
33 * Examples:
34 * WARNING("<insert warning message here>");
35 * int exit_status = 2;
36 * ERROR(exit_status, "exiting with status %i", exit_status);
37 */
38#ifdef DEBUG 32#ifdef DEBUG
39 #include <assert.h> 33 #include <assert.h>
40 #include <stdio.h> 34 #include <stdio.h>
@@ -69,76 +63,14 @@ unsigned char * hex_string_to_bin(char hex_string[]);
69#endif // DEBUG 63#endif // DEBUG
70 64
71/************************Linked List*********************** 65/************************Linked List***********************
72 * This is a simple linked list implementation, very similar 66 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
73 * to Linux kernel's /include/linux/list.h (which we can't
74 * use because Tox is GPLv3 and Linux is GPLv2.)
75 *
76 * TODO: Make the lists easier to use with some sweat pre-
77 * processor syntactic sugar.
78 **********************************************************/ 67 **********************************************************/
79 68
80/* Example usage
81
82This sample program makes a new struct which contains a
83character and a tox_list_t. It then prompts a user for
84input until he enters q or e. It then adds each character
85to the list, and uses a special for loop to print them.
86It then removes all the 'z' characters, and prints the list
87again.
88
89//Notice that the data to be put in the list *contains* tox_list_t;
90//usually, this is the other way around!
91typedef struct tox_string {
92 char c;
93 tox_list_t tox_lst; //Notice that tox_lst is *NOT* a pointer.
94} tox_string_t;
95
96int main()
97{
98 tox_list_t head;
99 tox_list_new(&head); //initialize head
100
101 //input a new character, until user enters q or e
102 char c = '\0';
103 while (c != 'q' && c != 'e') {
104 scanf("%c", &c);
105 tox_string_t* tmp = malloc(sizeof(tox_string_t));
106 tmp->c = c;
107 tox_list_add(&head, &tmp->tox_lst); //add it to the list
108 }
109
110TOX_LIST_FOR_EACH() takes a struct tox_list and a name for a temporary pointer to use in the loop.
111
112TOX_LIST_GET_VALUE() uses magic to return an instance of a structure that contains tox_list_t.
113You have to give it a temporary tox_string_t, name of tox_list_t member inside our structure (tox_lst),
114and the type of structure to return.
115
116 TOX_LIST_FOR_EACH(head, tmp)
117 printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c);
118
119 TOX_LIST_FOR_EACH(head, tmp) {
120 if (TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c == 'z') {
121 //If you delete tmp, you have to quit the loop, or it will go on infinitly.
122 //This will be fixed later on.
123 tox_list_remove(tmp);
124 break;
125 }
126 }
127
128 printf("\n");
129 TOX_LIST_FOR_EACH(head, tmp)
130 printf("%c", TOX_LIST_GET_VALUE(*tmp, tox_lst, tox_string_t).c);
131
132
133 return 0;
134}
135*/
136
137#define MEMBER_OFFSET(var_name_in_parent, parent_type) \ 69#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
138 (&(((parent_type*)0)->var_name_in_parent)) 70 (&(((parent_type*)0)->var_name_in_parent))
139 71
140#define GET_PARENT(var, var_name_in_parent, parent_type) \ 72#define GET_PARENT(var, var_name_in_parent, parent_type) \
141 (*((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))) 73 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
142 74
143#define TOX_LIST_FOR_EACH(lst, tmp_name) \ 75#define TOX_LIST_FOR_EACH(lst, tmp_name) \
144 for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) 76 for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
@@ -177,6 +109,7 @@ static inline void tox_list_remove(tox_list_t* lst) {
177 * Array to store pointers which tracks it's own size. 109 * Array to store pointers which tracks it's own size.
178 * TODO: Figure out if it shold store values instead of 110 * TODO: Figure out if it shold store values instead of
179 * pointers? 111 * pointers?
112 * TODO: Add wiki info usage.
180 ************************************************************/ 113 ************************************************************/
181 114
182struct tox_array { 115struct tox_array {
@@ -184,7 +117,7 @@ struct tox_array {
184 uint32_t size, length; 117 uint32_t size, length;
185}; 118};
186 119
187 static inline void tox_array_init(struct tox_array *arr) 120static inline void tox_array_init(struct tox_array *arr)
188{ 121{
189 arr->size = 1; 122 arr->size = 1;
190 arr->length = 0; 123 arr->length = 0;