diff options
Diffstat (limited to 'testing/misc_tools.h')
-rw-r--r-- | testing/misc_tools.h | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index b8c90fa6..1379b31a 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* misc_tools.h | 1 | /* misc_tools.h |
2 | * | 2 | * |
3 | * Miscellaneous functions and data structures for doing random things. | 3 | * Miscellaneous functions and data structures for doing random things. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -18,51 +18,51 @@ | |||
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
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> | 27 | #include <stdlib.h> |
28 | #include <stdint.h> | 28 | #include <stdint.h> |
29 | 29 | ||
30 | unsigned char* hex_string_to_bin(char hex_string[]); | 30 | unsigned char *hex_string_to_bin(char hex_string[]); |
31 | 31 | ||
32 | /*********************Debugging Macros******************** | 32 | /*********************Debugging Macros******************** |
33 | * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging | 33 | * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging |
34 | *********************************************************/ | 34 | *********************************************************/ |
35 | #ifdef DEBUG | 35 | #ifdef DEBUG |
36 | #include <assert.h> | 36 | #include <assert.h> |
37 | #include <stdio.h> | 37 | #include <stdio.h> |
38 | #include <string.h> | 38 | #include <string.h> |
39 | 39 | ||
40 | #define DEBUG_PRINT(str, ...) do { \ | 40 | #define DEBUG_PRINT(str, ...) do { \ |
41 | char msg[1000]; \ | 41 | char msg[1000]; \ |
42 | sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ | 42 | sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ |
43 | fprintf(stderr, msg, __VA_ARGS__); \ | 43 | fprintf(stderr, msg, __VA_ARGS__); \ |
44 | } while (0) | 44 | } while (0) |
45 | 45 | ||
46 | #define WARNING(...) do { \ | 46 | #define WARNING(...) do { \ |
47 | fprintf(stderr, "warning in "); \ | 47 | fprintf(stderr, "warning in "); \ |
48 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 48 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
49 | } while (0) | 49 | } while (0) |
50 | 50 | ||
51 | #define INFO(...) do { \ | 51 | #define INFO(...) do { \ |
52 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 52 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
53 | } while (0) | 53 | } while (0) |
54 | 54 | ||
55 | #undef ERROR | 55 | #undef ERROR |
56 | #define ERROR(exit_status, ...) do { \ | 56 | #define ERROR(exit_status, ...) do { \ |
57 | fprintf(stderr, "error in "); \ | 57 | fprintf(stderr, "error in "); \ |
58 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | 58 | DEBUG_PRINT(__VA_ARGS__, ' '); \ |
59 | exit(exit_status); \ | 59 | exit(exit_status); \ |
60 | } while (0) | 60 | } while (0) |
61 | #else | 61 | #else |
62 | #define WARNING(...) | 62 | #define WARNING(...) |
63 | #define INFO(...) | 63 | #define INFO(...) |
64 | #undef ERROR | 64 | #undef ERROR |
65 | #define ERROR(...) | 65 | #define ERROR(...) |
66 | #endif // DEBUG | 66 | #endif // DEBUG |
67 | 67 | ||
68 | /************************Linked List*********************** | 68 | /************************Linked List*********************** |
@@ -81,31 +81,31 @@ unsigned char* hex_string_to_bin(char hex_string[]); | |||
81 | #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) | 81 | #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) |
82 | 82 | ||
83 | typedef struct tox_list { | 83 | typedef struct tox_list { |
84 | struct tox_list *prev, *next; | 84 | struct tox_list *prev, *next; |
85 | } tox_list_t; | 85 | } tox_list_t; |
86 | 86 | ||
87 | /* Returns a new tox_list_t. */ | 87 | /* Returns a new tox_list_t. */ |
88 | static inline void tox_list_new(tox_list_t* lst) | 88 | static inline void tox_list_new(tox_list_t *lst) |
89 | { | 89 | { |
90 | lst->prev = lst->next = lst; | 90 | lst->prev = lst->next = lst; |
91 | } | 91 | } |
92 | 92 | ||
93 | /* Inserts a new tox_lst after lst and returns it. */ | 93 | /* Inserts a new tox_lst after lst and returns it. */ |
94 | static inline void tox_list_add(tox_list_t* lst, tox_list_t* new_lst) | 94 | static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) |
95 | { | 95 | { |
96 | tox_list_new(new_lst); | 96 | tox_list_new(new_lst); |
97 | 97 | ||
98 | new_lst->next = lst->next; | 98 | new_lst->next = lst->next; |
99 | new_lst->next->prev = new_lst; | 99 | new_lst->next->prev = new_lst; |
100 | 100 | ||
101 | lst->next = new_lst; | 101 | lst->next = new_lst; |
102 | new_lst->prev = lst; | 102 | new_lst->prev = lst; |
103 | } | 103 | } |
104 | 104 | ||
105 | static inline void tox_list_remove(tox_list_t* lst) | 105 | static inline void tox_list_remove(tox_list_t *lst) |
106 | { | 106 | { |
107 | lst->prev->next = lst->next; | 107 | lst->prev->next = lst->next; |
108 | lst->next->prev = lst->prev; | 108 | lst->next->prev = lst->prev; |
109 | } | 109 | } |
110 | 110 | ||
111 | /****************************Array*************************** | 111 | /****************************Array*************************** |
@@ -124,7 +124,7 @@ static inline void tox_array_init(struct tox_array *arr) | |||
124 | { | 124 | { |
125 | arr->size = 1; | 125 | arr->size = 1; |
126 | arr->length = 0; | 126 | arr->length = 0; |
127 | arr->data = malloc(sizeof(void*)); | 127 | arr->data = malloc(sizeof(void *)); |
128 | } | 128 | } |
129 | 129 | ||
130 | static inline void tox_array_delete(struct tox_array *arr) | 130 | static inline void tox_array_delete(struct tox_array *arr) |
@@ -139,22 +139,24 @@ static inline void tox_array_delete(struct tox_array *arr) | |||
139 | static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) | 139 | static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) |
140 | { | 140 | { |
141 | arr->size = arr->length + extra; | 141 | arr->size = arr->length + extra; |
142 | arr->data = realloc(arr->data, arr->size * sizeof(void*)); | 142 | arr->data = realloc(arr->data, arr->size * sizeof(void *)); |
143 | } | 143 | } |
144 | 144 | ||
145 | static inline void _tox_array_push(struct tox_array *arr, void *new) | 145 | static inline void _tox_array_push(struct tox_array *arr, void *new) |
146 | { | 146 | { |
147 | if (arr->length+1 >= arr->size) | 147 | if (arr->length + 1 >= arr->size) |
148 | tox_array_shrink_to_fit(arr, arr->size); | 148 | tox_array_shrink_to_fit(arr, arr->size); |
149 | |||
149 | arr->data[arr->length++] = new; | 150 | arr->data[arr->length++] = new; |
150 | } | 151 | } |
151 | #define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) | 152 | #define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) |
152 | 153 | ||
153 | static inline void* tox_array_pop(struct tox_array *arr) | 154 | static inline void *tox_array_pop(struct tox_array *arr) |
154 | { | 155 | { |
155 | if (arr->length-1 < arr->size/4) | 156 | if (arr->length - 1 < arr->size / 4) |
156 | tox_array_shrink_to_fit(arr, arr->length*2); | 157 | tox_array_shrink_to_fit(arr, arr->length * 2); |
157 | return arr->data[arr->length--]; | 158 | |
159 | return arr->data[arr->length--]; | ||
158 | } | 160 | } |
159 | 161 | ||
160 | #endif // MISC_TOOLS_H | 162 | #endif // MISC_TOOLS_H |