lt/utils.h -rw-r--r-- 1.5 KiB View raw
                                                                                
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
49
50
51
52
53
54
55
56
57
58
#ifndef UTILS_H
#define UTILS_H

#include <stdio.h>

#define ERROR(str, ...) fprintf(stderr, "Error: " str "\n", ##__VA_ARGS__)

#define COUNT_OF(ptr) sizeof(ptr) / sizeof(ptr[0])
#define STATIC_ARRAY_FOREACH(item, array) \
    for (size_t __f_k = 1, __f_i = 0; __f_k && __f_i < COUNT_OF(array); __f_k = !__f_k, __f_i++) \
        for (item = &array[__f_i]; __f_k; __f_k = !__f_k)

#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X, Y) ((X) > (Y) ? (Y) : (X))
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define DIVCEIL(a, b) (((a) + ((b) - 1)) / (b))
#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec) * 1000 + (t1.tv_nsec - t2.tv_nsec) / 1E6)
#define IS_SET(mode, flag) ((mode & (flag)) != 0)
#define IS_CONTROLC0(c)  (BETWEEN(c, 1, 0x1f) || (c) == '\177')
#define IS_CONTROLC1(c)  (BETWEEN(c, 0x80, 0x9f))
#define IS_CONTROL(c)    (IS_CONTROLC0(c) || IS_CONTROLC1(c))
#define QUOTE_ME(x) #x
#define STR(x) QUOTE_ME(x)

static inline unsigned short sixd_to_16bit(int x)
{
    return x == 0 ? 0 : 0x3737 + 0x2828 * x;
}

static const char *__color_name[] = {
    "black",
    "red3",
    "green3",
    "yellow3",
    "blue2",
    "magenta3",
    "cyan3",
    "gray90",
    "gray50",
    "red",
    "green",
    "yellow",
    "#5c5cff",
    "magenta",
    "cyan",
    "white",

    [255] = 0
};

static const char *__ascii_printable =
    " !\"#$%&'()*+,-./0123456789:;<=>?"
    "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
    "`abcdefghijklmnopqrstuvwxyz{|}~";

#endif /* UTILS_H */