-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
48 lines (39 loc) · 1.73 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define WARN(...) do { fprintf(stderr, "\033[1m%s:\033[0m In function <\033[1m%s\033[0m>:\n\033[1m%s:%d: \033[35;1mwarning:\033[0m %s\n", __FILE__, __func__, __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); } while(0)
#define ERROR(...) do { fprintf(stderr, "\033[1m%s:\033[0m In function <\033[1m%s\033[0m>:\n\033[1m%s:%d: \033[31;1merror:\033[0m ", __FILE__, __func__, __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); abort(); } while(0)
#ifndef NDEBUG
#define ASSERT(X) do { if (!(X)) { ERROR("Assertion \"%s\" failed", #X); } } while(0)
#else
#define ASSERT(X)
#endif
#define FMT_FOR_TYPE(X) _Generic(X, char: "%c", int: "%i", long int: "%li", long long int: "%lli", unsigned int: "%u", long unsigned int: "%lu", long long unsigned int: "%llu")
#define DISP(X) do { printf("\033[1m%s:\033[0m In function <\033[1m%s\033[0m>\n\033[1m%s:%d: \033[36mdisp:\033[0m %s = ", __FILE__, __func__, __FILE__, __LINE__, #X); printf(FMT_FOR_TYPE(X), X); printf("\n"); } while (0)
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#define MIN(A,B) ((A)<(B)?(A):(B))
#define MAX(A,B) ((A)>(B)?(A):(B))
#define ABS(X) ((X)>=0?(X):-(X))
#define ARRAYCOUNT(X) (sizeof(X) / sizeof(X[0]))
char const * make_c_string (char const * start, char const * end) {
ASSERT(start);
ASSERT(end);
ASSERT(end >= start);
size_t len = end - start;
char * str = malloc(len+1);
memcpy(str, start, len);
str[len] = '\0';
return str;
}