-
Notifications
You must be signed in to change notification settings - Fork 19
/
common.h
137 lines (101 loc) · 2.31 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
/*
* gbsplay is a Gameboy sound player
*
* 2003-2020 (C) by Tobias Diedrich <ranma+gbsplay@tdiedrich.de>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#ifndef _COMMON_H_
#define _COMMON_H_
#include "config.h"
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
/* mingw64 also defines __MINGW32__ in addition to __MINGW64__ */
#ifdef __MINGW32__
#define HAVE_MINGW 1
#endif
#ifndef true
#define true (0==0)
#define false (!true)
#endif
#define WARN_N(n, ...) { \
static long ctr = n; \
if (ctr) { \
ctr--; \
fprintf(stderr, __VA_ARGS__); \
} \
}
#define WARN_ONCE(...) WARN_N(1, __VA_ARGS__)
#define CONTAINER_OF(ptr, type, member) \
((type *)((void *)(ptr) - offsetof(type, member)))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define TEXTDOMAIN "gbsplay"
#define N_(x) x
#if USE_I18N == 1
# include <locale.h>
# include <libintl.h>
# if GBS_PRESERVE_TEXTDOMAIN == 1
static inline char* _(const char *msgid)
{
char *olddomain = textdomain(NULL);
char *olddir = bindtextdomain(olddomain, NULL);
char *res;
bindtextdomain(TEXTDOMAIN, LOCALE_PREFIX);
res = dgettext(TEXTDOMAIN, msgid);
bindtextdomain(olddomain, olddir);
return res;
}
# else
static inline char* _(const char *msgid)
{
return gettext(msgid);
}
static inline void i18n_init(void)
{
if (setlocale(LC_ALL, "") == NULL) {
fprintf(stderr, "setlocale() failed\n");
}
if (bindtextdomain(TEXTDOMAIN, LOCALE_PREFIX) == NULL) {
fprintf(stderr, "bindtextdomain() failed: %s\n", strerror(errno));
}
if (textdomain(TEXTDOMAIN) == NULL) {
fprintf(stderr, "textdomain() failed: %s\n", strerror(errno));
}
}
# endif
#else
# define _(x) (x)
static inline void i18n_init(void) {}
#endif
#include <sys/param.h>
#ifndef BYTE_ORDER
# define BIG_ENDIAN 1
# define LITTLE_ENDIAN 2
# ifdef _BIG_ENDIAN
# define BYTE_ORDER BIG_ENDIAN
# endif
# ifdef _LITTLE_ENDIAN
# define BYTE_ORDER LITTLE_ENDIAN
# endif
#endif /* BYTE_ORDER */
#if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
# error endian defines missing
#endif
#if BYTE_ORDER == BIG_ENDIAN
static inline long is_le_machine() {
return false;
}
static inline long is_be_machine() {
return true;
}
#else
static inline long is_le_machine() {
return true;
}
static inline long is_be_machine() {
return false;
}
#endif
#endif