diff options
author | Anony Moose <noMail@nomail.su> | 2013-08-29 22:54:54 +0000 |
---|---|---|
committer | Anony Moose <noMail@nomail.su> | 2013-08-29 22:54:54 +0000 |
commit | eb33796e58cfca4fa26b459ad5a9d013f1df4c42 (patch) | |
tree | 6c7fd1cb36376580d667719b12e85634590cd54b /testing/misc_tools.h | |
parent | 31354d80d40270ee415b84e312f91b47bf104afe (diff) |
Moved misc_tools from testing to toxcore.
Diffstat (limited to 'testing/misc_tools.h')
-rw-r--r-- | testing/misc_tools.h | 168 |
1 files changed, 0 insertions, 168 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h deleted file mode 100644 index f71d0064..00000000 --- a/testing/misc_tools.h +++ /dev/null | |||
@@ -1,168 +0,0 @@ | |||
1 | /* misc_tools.h | ||
2 | * | ||
3 | * Miscellaneous functions and data structures for doing random things. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
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/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef MISC_TOOLS_H | ||
25 | #define MISC_TOOLS_H | ||
26 | |||
27 | #include <stdlib.h> | ||
28 | #include <stdint.h> | ||
29 | #include <string.h> /* for memcpy() */ | ||
30 | |||
31 | unsigned char *hex_string_to_bin(char hex_string[]); | ||
32 | |||
33 | /*********************Debugging Macros******************** | ||
34 | * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging | ||
35 | *********************************************************/ | ||
36 | #ifdef DEBUG | ||
37 | #include <assert.h> | ||
38 | #include <stdio.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | #define DEBUG_PRINT(str, ...) do { \ | ||
42 | char msg[1000]; \ | ||
43 | sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ | ||
44 | fprintf(stderr, msg, __VA_ARGS__); \ | ||
45 | } while (0) | ||
46 | |||
47 | #define WARNING(...) do { \ | ||
48 | fprintf(stderr, "warning in "); \ | ||
49 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | ||
50 | } while (0) | ||
51 | |||
52 | #define INFO(...) do { \ | ||
53 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | ||
54 | } while (0) | ||
55 | |||
56 | #undef ERROR | ||
57 | #define ERROR(exit_status, ...) do { \ | ||
58 | fprintf(stderr, "error in "); \ | ||
59 | DEBUG_PRINT(__VA_ARGS__, ' '); \ | ||
60 | exit(exit_status); \ | ||
61 | } while (0) | ||
62 | #else | ||
63 | #define WARNING(...) | ||
64 | #define INFO(...) | ||
65 | #undef ERROR | ||
66 | #define ERROR(...) | ||
67 | #endif // DEBUG | ||
68 | |||
69 | /************************Linked List*********************** | ||
70 | * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List | ||
71 | * TODO: Update wiki. | ||
72 | **********************************************************/ | ||
73 | |||
74 | #define MEMBER_OFFSET(var_name_in_parent, parent_type) \ | ||
75 | (&(((parent_type*)0)->var_name_in_parent)) | ||
76 | |||
77 | #define GET_PARENT(var, var_name_in_parent, parent_type) \ | ||
78 | ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type)))) | ||
79 | |||
80 | #define TOX_LIST_FOR_EACH(lst, tmp_name) \ | ||
81 | for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) | ||
82 | |||
83 | #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) | ||
84 | |||
85 | typedef struct tox_list { | ||
86 | struct tox_list *prev, *next; | ||
87 | } tox_list; | ||
88 | |||
89 | /* Returns a new tox_list_t. */ | ||
90 | static inline void tox_list_new(tox_list *lst) | ||
91 | { | ||
92 | lst->prev = lst->next = lst; | ||
93 | } | ||
94 | |||
95 | /* Inserts a new tox_lst after lst and returns it. */ | ||
96 | static inline void tox_list_add(tox_list *lst, tox_list *new_lst) | ||
97 | { | ||
98 | tox_list_new(new_lst); | ||
99 | |||
100 | new_lst->next = lst->next; | ||
101 | new_lst->next->prev = new_lst; | ||
102 | |||
103 | lst->next = new_lst; | ||
104 | new_lst->prev = lst; | ||
105 | } | ||
106 | |||
107 | static inline void tox_list_remove(tox_list *lst) | ||
108 | { | ||
109 | lst->prev->next = lst->next; | ||
110 | lst->next->prev = lst->prev; | ||
111 | } | ||
112 | |||
113 | /****************************Array*************************** | ||
114 | * Array which manages its own memory allocation. | ||
115 | * It stores copy of data (not pointers). | ||
116 | * TODO: Add wiki info usage. | ||
117 | ************************************************************/ | ||
118 | |||
119 | typedef struct tox_array { | ||
120 | uint8_t *data; /* last elem is data[len-1] */ | ||
121 | uint32_t len; | ||
122 | size_t elem_size; /* in bytes */ | ||
123 | } tox_array; | ||
124 | |||
125 | static inline void tox_array_init(tox_array *arr, size_t elem_size) | ||
126 | { | ||
127 | arr->len = 0; | ||
128 | arr->elem_size = elem_size; | ||
129 | arr->data = NULL; | ||
130 | } | ||
131 | |||
132 | static inline void tox_array_delete(tox_array *arr) | ||
133 | { | ||
134 | free(arr->data); | ||
135 | arr->len = arr->elem_size = 0; | ||
136 | } | ||
137 | |||
138 | static inline uint8_t tox_array_push_ptr(tox_array *arr, uint8_t *item) | ||
139 | { | ||
140 | arr->data = realloc(arr->data, arr->elem_size * (arr->len+1)); | ||
141 | if (item != NULL) | ||
142 | memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size); | ||
143 | arr->len++; | ||
144 | |||
145 | return 1; | ||
146 | } | ||
147 | #define tox_array_push(arr, item) tox_array_push_ptr(arr, (uint8_t*)(&(item))) | ||
148 | |||
149 | /* Deletes num items from array. | ||
150 | * Not same as pop in stacks, because to access elements you use data. | ||
151 | */ | ||
152 | static inline void tox_array_pop(tox_array *arr, uint32_t num) | ||
153 | { | ||
154 | if (num == 0) | ||
155 | num = 1; | ||
156 | arr->len -= num; | ||
157 | arr->data = realloc(arr->data, arr->elem_size*arr->len); | ||
158 | } | ||
159 | |||
160 | /* TODO: return ptr and do not take type */ | ||
161 | #define tox_array_get(arr, i, type) (((type*)(arr)->data)[i]) | ||
162 | |||
163 | |||
164 | #define tox_array_for_each(arr, type, tmp_name) \ | ||
165 | type *tmp_name = &tox_array_get(arr, 0, type); uint32_t tmp_name ## _i = 0; \ | ||
166 | for (; tmp_name ## _i < (arr)->len; tmp_name = &tox_array_get(arr, ++ tmp_name ## _i, type)) | ||
167 | |||
168 | #endif // MISC_TOOLS_H | ||