cMCP/inlcude/utils.h

155 lines
4.8 KiB
C

#ifndef __UTILS_H__
#define __UTILS_H__
#include <stdlib.h>
#include <string.h>
#define OUT_OF_BOUNDS(base, pos, size) do {\
if ((pos) - (base) + 1 > (size) )\
{\
fprintf(stderr, "%s:%d: Error: Expected more characters, but found the end of the request.\n", __FILE__, __LINE__);\
return 1;\
}\
} while(0)
#define len(x) (\
(x) == NULL \
? ( \
fprintf(stderr, "%s:%d: ERROR: Tried to use len("#x"), but it is NULL\n", __FILE__, __LINE__), \
0 \
) \
: sizeof(x)/sizeof(*x) \
)
#define foreach(i, arr) for (size_t i = 0 ; i < len(arr) ; i++)
#define DA_MIN_CAPACITY 32
#define da_error(s) fprintf(stderr, "%s:%d: ERROR: " #s "\n", __FILE__, __LINE__)
#define da_append(da, item) \
( (da) != NULL \
? ( (da)->data == NULL \
? ( (da)->data = malloc(DA_MIN_CAPACITY), (da)->size = 0, (da)->capacity = DA_MIN_CAPACITY, 0) \
: 0 \
, (da)->size >= (da)->capacity \
? ((da)->capacity = (da)->size*2, (da)->data = realloc((da)->data, (da)->capacity), 0) \
: 0\
, ( (da)->data[(da)->size++] = (item), 0)\
) \
: ( da_error("tried to da_append to NULL pointer.") \
, 1 \
)\
)
#define da_concat(lhs, rhs) \
( (lhs) != NULL \
? ( (rhs) != NULL \
? ( (lhs)->data == NULL \
? ( (lhs)->data = malloc(DA_MIN_CAPACITY), (lhs)->size = 0, (lhs)->capacity = DA_MIN_CAPACITY, 0) \
: 0 \
, (rhs)->data != NULL \
? ( ((lhs)->capacity < (rhs)->size) \
? ( (lhs)->capacity += (rhs)->size \
, (lhs)->data = realloc((lhs)->data, (lhs)->capacity) \
, 0 \
) \
: 0 \
, ((lhs)->capacity - (lhs)->size < (rhs)->size) \
? ( (lhs)->capacity *= 2 \
, (lhs)->data = realloc((lhs)->data, (lhs)->capacity) \
, 0 \
) \
: 0 \
, memcpy((lhs)->data + (lhs)->size, (rhs)->data, (rhs)->size) \
, (lhs)->size += (rhs)->size \
, 0\
) \
: 0 \
) \
: (da_error("tried to da_concat but rhs is NULL"), 1) \
) \
: (da_error("tried to da_concat but lhs is NULL."), 1) \
)
#define da_cstr_concat(da, str) \
( (da) != NULL \
? ( (str) != NULL \
? ( (da)->data == NULL \
? ( (da)->data = malloc(DA_MIN_CAPACITY), (da)->size = 0, (da)->capacity = DA_MIN_CAPACITY, memset((da)->data, 0, sizeof(*(da)->data) *DA_MIN_CAPACITY) ,0) \
: 0 \
, *(str) != '\0' \
? ( ((da)->capacity < strlen(str)) \
? ( (da)->capacity += strlen(str) + 1\
, (da)->data = realloc((da)->data, (da)->capacity) \
, 0 \
) \
: 0 \
, ((da)->capacity - (da)->size < strlen(str)) + 1 \
? ( (da)->capacity *= 2 \
, (da)->data = realloc((da)->data, (da)->capacity) \
, 0 \
) \
: 0 \
, memcpy((da)->data + (da)->size, (str), strlen(str)) \
, (da)->size += strlen(str) \
, (da)->data[(da)->size] = '\0' \
, 0\
) \
: 0 \
) \
: (da_error("tried to da_cstr_concat but str is NULL"), 1) \
) \
: (da_error("tried to da_cstr_concat but da is NULL."), 1) \
)
#define da_memcpy(da, mem, mem_size) \
( (da) != NULL \
? ( (mem) != NULL \
? ( (da)->data == NULL \
? ( (da)->data = malloc(DA_MIN_CAPACITY) \
, (da)->size = 0 \
, (da)->capacity = DA_MIN_CAPACITY \
, memset((da)->data, 0, sizeof(*((da)->data)) * DA_MIN_CAPACITY) \
, 0 \
) \
: 0 \
, (mem_size) > 0 \
? ( ((da)->capacity < (mem_size)) \
? ( (da)->capacity += (mem_size) \
, (da)->data = realloc((da)->data, (da)->capacity) \
, 0 \
) \
: 0 \
, ((da)->capacity - (da)->size < (mem_size)) \
? ( (da)->capacity *= 2 \
, (da)->data = realloc((da)->data, (da)->capacity) \
, 0 \
) \
: 0 \
, memcpy((da)->data + (da)->size, (mem), (mem_size)) \
, (da)->size += (mem_size) \
, 0\
) \
: 0 \
) \
: (da_error("tried to da_memcpy but mem is NULL"), 1) \
) \
: (da_error("tried to da_memcpy but da is NULL."), 1) \
)
/*
* ERRORS
* 1: str had a none digit character.
* 2: str was null
* 3: res was null
* 4: zero length, nothing to convert
*/
int sstrtoint(const char *str, size_t size, int *res);
int cstrtoint(const char *str, int *res);
#endif //__UTILS_H__