2013-07-28 00:10:41 +08:00
|
|
|
/* misc_tools.h
|
2013-08-17 01:11:09 +08:00
|
|
|
*
|
2013-07-28 00:10:41 +08:00
|
|
|
* Miscellaneous functions and data structures for doing random things.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-17 01:11:09 +08:00
|
|
|
*
|
2013-07-28 00:10:41 +08:00
|
|
|
*/
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-28 00:10:41 +08:00
|
|
|
#ifndef MISC_TOOLS_H
|
|
|
|
#define MISC_TOOLS_H
|
|
|
|
|
2013-08-09 23:54:14 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2013-08-25 04:34:38 +08:00
|
|
|
#include <string.h> /* for memcpy() */
|
2013-08-09 23:54:14 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
unsigned char *hex_string_to_bin(char hex_string[]);
|
2013-08-07 01:52:05 +08:00
|
|
|
|
2013-08-09 23:44:22 +08:00
|
|
|
/*********************Debugging Macros********************
|
|
|
|
* wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
|
|
|
|
*********************************************************/
|
2013-08-07 04:47:15 +08:00
|
|
|
#ifdef DEBUG
|
2013-08-25 04:54:03 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DEBUG_PRINT(str, ...) do { \
|
|
|
|
char msg[1000]; \
|
|
|
|
sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \
|
|
|
|
fprintf(stderr, msg, __VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define WARNING(...) do { \
|
|
|
|
fprintf(stderr, "warning in "); \
|
|
|
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define INFO(...) do { \
|
|
|
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#undef ERROR
|
|
|
|
#define ERROR(exit_status, ...) do { \
|
|
|
|
fprintf(stderr, "error in "); \
|
|
|
|
DEBUG_PRINT(__VA_ARGS__, ' '); \
|
|
|
|
exit(exit_status); \
|
|
|
|
} while (0)
|
2013-08-07 04:47:15 +08:00
|
|
|
#else
|
2013-08-25 04:54:03 +08:00
|
|
|
#define WARNING(...)
|
|
|
|
#define INFO(...)
|
|
|
|
#undef ERROR
|
|
|
|
#define ERROR(...)
|
2013-08-07 04:47:15 +08:00
|
|
|
#endif // DEBUG
|
2013-08-07 01:52:05 +08:00
|
|
|
|
|
|
|
/************************Linked List***********************
|
2013-08-09 23:44:22 +08:00
|
|
|
* http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
|
2013-08-25 04:34:38 +08:00
|
|
|
* TODO: Update wiki.
|
2013-08-07 01:52:05 +08:00
|
|
|
**********************************************************/
|
|
|
|
|
|
|
|
#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
|
|
|
|
(&(((parent_type*)0)->var_name_in_parent))
|
|
|
|
|
|
|
|
#define GET_PARENT(var, var_name_in_parent, parent_type) \
|
2013-08-09 23:44:22 +08:00
|
|
|
((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
|
2013-08-07 01:52:05 +08:00
|
|
|
|
|
|
|
#define TOX_LIST_FOR_EACH(lst, tmp_name) \
|
2013-08-25 04:34:38 +08:00
|
|
|
for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
|
2013-08-07 01:52:05 +08:00
|
|
|
|
|
|
|
#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type)
|
|
|
|
|
|
|
|
typedef struct tox_list {
|
2013-08-17 01:11:09 +08:00
|
|
|
struct tox_list *prev, *next;
|
2013-08-25 04:34:38 +08:00
|
|
|
} tox_list;
|
2013-08-07 01:52:05 +08:00
|
|
|
|
|
|
|
/* Returns a new tox_list_t. */
|
2013-08-25 04:34:38 +08:00
|
|
|
static inline void tox_list_new(tox_list *lst)
|
2013-08-09 23:45:34 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
lst->prev = lst->next = lst;
|
2013-08-07 01:52:05 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-07 01:52:05 +08:00
|
|
|
/* Inserts a new tox_lst after lst and returns it. */
|
2013-08-25 04:34:38 +08:00
|
|
|
static inline void tox_list_add(tox_list *lst, tox_list *new_lst)
|
2013-08-09 23:45:34 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
tox_list_new(new_lst);
|
2013-08-07 01:52:05 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
new_lst->next = lst->next;
|
|
|
|
new_lst->next->prev = new_lst;
|
2013-08-07 01:52:05 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
lst->next = new_lst;
|
|
|
|
new_lst->prev = lst;
|
2013-08-07 01:52:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-25 04:54:03 +08:00
|
|
|
static inline void tox_list_remove(tox_list *lst)
|
2013-08-09 23:45:34 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
lst->prev->next = lst->next;
|
|
|
|
lst->next->prev = lst->prev;
|
2013-08-07 01:52:05 +08:00
|
|
|
}
|
|
|
|
|
2013-08-09 23:38:21 +08:00
|
|
|
/****************************Array***************************
|
2013-08-25 04:34:38 +08:00
|
|
|
* Array which manages its own memory allocation.
|
|
|
|
* It stores copy of data (not pointers).
|
2013-08-09 23:44:22 +08:00
|
|
|
* TODO: Add wiki info usage.
|
2013-08-09 23:38:21 +08:00
|
|
|
************************************************************/
|
|
|
|
|
2013-08-25 04:34:38 +08:00
|
|
|
typedef struct tox_array {
|
|
|
|
void *data;
|
|
|
|
uint32_t len;
|
|
|
|
size_t elem_size; /* in bytes */
|
|
|
|
} tox_array;
|
2013-08-09 23:38:21 +08:00
|
|
|
|
2013-08-25 04:54:03 +08:00
|
|
|
static inline void tox_array_init(tox_array *arr, size_t elem_size)
|
2013-08-09 23:38:21 +08:00
|
|
|
{
|
2013-08-25 04:34:38 +08:00
|
|
|
arr->len = 0;
|
|
|
|
arr->elem_size = elem_size;
|
|
|
|
arr->data = NULL;
|
2013-08-09 23:38:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-25 04:54:03 +08:00
|
|
|
static inline void tox_array_delete(tox_array *arr)
|
2013-08-09 23:38:21 +08:00
|
|
|
{
|
|
|
|
free(arr->data);
|
2013-08-25 04:34:38 +08:00
|
|
|
arr->len = arr->elem_size = 0;
|
2013-08-09 23:38:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-25 04:54:03 +08:00
|
|
|
static inline void _tox_array_push(tox_array *arr, uint8_t *item)
|
2013-08-09 23:38:21 +08:00
|
|
|
{
|
2013-08-25 04:34:38 +08:00
|
|
|
arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-25 04:34:38 +08:00
|
|
|
memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
|
|
|
|
arr->len++;
|
2013-08-09 23:38:21 +08:00
|
|
|
}
|
2013-08-25 04:34:38 +08:00
|
|
|
#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
|
2013-08-09 23:38:21 +08:00
|
|
|
|
2013-08-25 04:34:38 +08:00
|
|
|
/* Deletes num items from array.
|
|
|
|
* Not same as pop in stacks, because to access elements you use data.
|
|
|
|
*/
|
2013-08-25 04:54:03 +08:00
|
|
|
static inline void tox_array_pop(tox_array *arr, uint32_t num)
|
2013-08-09 23:38:21 +08:00
|
|
|
{
|
2013-08-25 04:34:38 +08:00
|
|
|
arr->len--;
|
|
|
|
arr->data = realloc(arr->data, arr->elem_size*arr->len);
|
2013-08-09 23:38:21 +08:00
|
|
|
}
|
|
|
|
|
2013-08-25 04:34:38 +08:00
|
|
|
#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
|
|
|
|
|
2013-07-31 02:39:04 +08:00
|
|
|
#endif // MISC_TOOLS_H
|