-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.h
156 lines (136 loc) · 5.15 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
This file is part of the Fairytale project
Copyright (C) 2018 Márcio Pais
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COMMON_H
#define COMMON_H
#if defined(_WIN32) || defined(_MSC_VER)
# ifndef WINDOWS
# define WINDOWS //to compile for Windows
# endif
#elif defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__)
# ifndef UNIX
# define UNIX //to compile for Unix, Linux, Solaris, MacOS / Darwin, etc)
# endif
#endif
#if !defined(WINDOWS) && !defined(UNIX)
# error Unknown target system
#endif
enum class Endian
{
Little,
Big
};
#ifdef BIG_ENDIAN
# define betoh64(x) (x) // Big-Endian to Host translation, 64 bit
# define betoh32(x) (x) // Big-Endian to Host translation, 32 bit
# define betoh16(x) (x) // Big-Endian to Host translation, 16 bit
# if defined(__GNUG__) || defined(__clang__)
# define letoh64(x) (x = __builtin_bswap64(x)) // Little-Endian to Host translation, 64 bit
# define letoh32(x) (x = __builtin_bswap32(x)) // Little-Endian to Host translation, 32 bit
# define letoh16(x) (x = __builtin_bswap16(x)) // Little-Endian to Host translation, 16 bit
# elif defined(_MSC_VER)
# define letoh64(x) (x = _byteswap_uint64(x)) // Little-Endian to Host translation, 64 bit
# define letoh32(x) (x = _byteswap_ulong(x)) // Little-Endian to Host translation, 32 bit
# define letoh16(x) (x = _byteswap_ushort(x)) // Little-Endian to Host translation, 16 bit
# endif
#else
# define letoh64(x) (x) // Little-Endian to Host translation, 64 bit
# define letoh32(x) (x) // Little-Endian to Host translation, 32 bit
# define letoh16(x) (x) // Little-Endian to Host translation, 16 bit
# if defined(__GNUG__) || defined(__clang__)
# define betoh64(x) (x = __builtin_bswap64(x)) // Big-Endian to Host translation, 64 bit
# define betoh32(x) (x = __builtin_bswap32(x)) // Big-Endian to Host translation, 32 bit
# define betoh16(x) (x = __builtin_bswap16(x)) // Big-Endian to Host translation, 16 bit
# elif defined(_MSC_VER)
# define betoh64(x) (x = _byteswap_uint64(x)) // Big-Endian to Host translation, 64 bit
# define betoh32(x) (x = _byteswap_ulong(x)) // Big-Endian to Host translation, 32 bit
# define betoh16(x) (x = _byteswap_ushort(x)) // Big-Endian to Host translation, 16 bit
# endif
#endif
#ifdef UNIX
# include <dirent.h> //opendir(), readdir(), dirent()
# include <errno.h> //errno
# include <limits.h> //PATH_MAX (for OSX)
# include <string.h> //strlen(), strcpy(), strcat(), strerror(), memset(), memcpy(), memmove()
# include <unistd.h> //isatty()
# include <new>
#else
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <Fcntl.h> //_O_APPEND
# include <io.h> //_open_osfhandle
# include <time.h> //clock_t, CLOCKS_PER_SEC
# include <windows.h>
#endif
// Platform-independent includes
#include <assert.h>
#include <math.h> //floor(), sqrt()
#include <sys/stat.h> //stat(), mkdir(), stat()
#include <cinttypes> //PRIu64 (C99 standard, available since VS 2013 RTM)
#include <stdexcept> //std::exception
#ifndef _MSC_VER
# include <stddef.h>
#endif
#define _FILE_OFFSET_BITS 64
#if defined(WINDOWS) && (defined(__x86_64) || defined(_M_X64))
# define off_t int64_t
#endif
#ifdef _MSC_VER
# define fseeko(a, b, c) _fseeki64(a, b, c)
# define ftello(a) _ftelli64(a)
#else
# ifndef UNIX
# ifndef fseeko
# define fseeko(a, b, c) fseeko64(a, b, c)
# endif
# ifndef ftello
# define ftello(a) ftello64(a)
# endif
# endif
#endif
#ifdef WINDOWS
# ifdef GetTempFileName
# undef GetTempFileName
# endif
# define GetTempFileName GetTempFileNameW
# ifdef CreateFile
# undef CreateFile
# endif
# define CreateFile CreateFileW
#endif
extern int verbose;
#define LOG(fmt, ...) \
do { \
if (verbose) \
printf(fmt, ##__VA_ARGS__); \
} while (0)
#define TRACE(fmt, ...) \
do { \
if (verbose > 1) \
fprintf(stderr, fmt, ##__VA_ARGS__); \
} while (0)
#define MAX_INDEXABLE size_t((1ull << (sizeof(void*) * 8 - 1)) - 1)
#define MEM_LIMIT(mem) (mem > (int64_t)MAX_INDEXABLE ? MAX_INDEXABLE : size_t(mem))
#define GENERIC_BUFFER_SIZE 0x8000ll //32KB
#if (GENERIC_BUFFER_SIZE & (GENERIC_BUFFER_SIZE - 1)) || (GENERIC_BUFFER_SIZE <= 4096)
# error GENERIC_BUFFER_SIZE must be a power of 2 bigger than 4096
#endif
#define MAX_RECURSION_LEVEL 4
class ExhaustedStorageException : public std::exception {};
# define TAB 0x09
# define NEW_LINE 0x0A
# define CARRIAGE_RETURN 0x0D
# define SPACE 0x20
#endif