aboutsummaryrefslogtreecommitdiffstats
path: root/src/macros.h
blob: f15cdd77a2c72b31f43e5eb6fb1c434d455ead01 (plain) (blame)
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
#ifndef L2SU_MACROS_H_INCLUDED
#define L2SU_MACROS_H_INCLUDED

#include "config.h"

#define L2_ARRLEN(_a) (sizeof(_a) / sizeof(*(_a)))
#define L2_CSTRLEN(_s) (L2_ARRLEN(_s) - 1)
#define L2_UNUSED(_v) ((void)(_v))

#include <alloca.h>
#include <string.h>
#include <stdio.h>

/* I LOVE ALLOCA */
#define L2_ASTRCAT2(_var, _s1, _len1, _s2, _len2) do { \
  _var = alloca(_len1 + _len2 + 1);                    \
  memcpy(_var, (_s1), (_len1));                        \
  memcpy(_var + (_len1), (_s2), (_len2));              \
  (_var)[((_len1) + (_len2))] = '\0';                  \
} while (0)

#define L2_ASPRINTF(_var, _tmp, _fmt, ...) do { \
  _tmp = snprintf(NULL, 0, _fmt, __VA_ARGS__);  \
  _var = alloca(_tmp + 1);                      \
  snprintf(_var, _tmp + 1, _fmt, __VA_ARGS__);  \
} while (0)

/* astrdup is a gnu extension */
/* _var: the variable to which the duplicated string is assigned
 * _sz:  the length of the string duplicated (contractually)
 * _in:  the NUL-terminated string to duplicate */
#define L2_ASTRDUP(_var, _sz, _in) do { \
  _sz = strlen(_in);                    \
  _var = alloca(_sz + 1);               \
  memcpy(_var, _in, _sz);               \
  (_var)[(_sz)] = '\0';                 \
} while (0)

#define L2_BADMIN(_v1, _v2) (((_v1) < (_v2)) ? (_v1) : (_v2))
#define L2_BADMAX(_v1, _v2) (((_v1) > (_v2)) ? (_v1) : (_v2))

#define L2_USER_AGENT PROJECT_NAME "/0.1.0 <bigfoot+l2su@figboot.dev>"

#define L2_URL_META_BASE "https://piston-meta.mojang.com"
#define L2_URL_META_VERSION_MANIFEST L2_URL_META_BASE "/mc/game/version_manifest_v2.json"

#ifdef __GNUC__
#define L2_FORMAT(_flavor, _stridx, _argidx) __attribute__((format (_flavor, _stridx, _argidx)))
#else
#define L2_FORMAT(_unused1, _unused2, _unused3)
#endif

#endif /* include guard */