aboutsummaryrefslogtreecommitdiffstats
path: root/common.h
blob: ed4b05e67ddd422c6be85e12b385d0860aed8db6 (plain)
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
/*
 * Copyright (C) 2020-2021  Tomasz Kramkowski <tk@the-tk.com>
 * SPDX-License-Identifier: MIT
 */
#ifndef PACK_COMMON_H
#define PACK_COMMON_H

#include <limits.h>
#include <stddef.h>
#include <ctype.h>

#include "pack.h"

#define BITMASK(n) (UINTMAX_MAX >> (sizeof (uintmax_t) * CHAR_BIT - n))

#define SET_AND_GOTO(what, to, where) \
	do { (what) = (to); goto where; } while (0);

#ifndef PRIuSIZE
   #ifdef _WIN32
      #ifdef _WIN64
         #define PRIuSIZE PRIu64
      #else
         #define PRIuSIZE PRIu32
      #endif
   #else
      #define PRIuSIZE "zu"
   #endif
#endif

#define ITYPE_MACROS \
	T(SCHAR,  signed,   char,      int) \
	T(UCHAR,  unsigned, char,      int) \
	T(SHORT,  signed,   short,     int) \
	T(USHORT, unsigned, short,     int) \
	T(INT,    signed,   int,       int) \
	T(UINT,   unsigned, int,       int) \
	T(LONG,   signed,   long,      long) \
	T(ULONG,  unsigned, long,      long) \
	T(LLONG,  signed,   long long, long long) \
	T(ULLONG, unsigned, long long, long long)

// safe_islower: islower but no domain errors
static inline int safe_islower(int c)
{
	if (c < 0 || c > UCHAR_MAX) return 0;
	return islower(c);
}

size_t getsize(enum pack_type t);

#endif // !PACK_COMMON_H