15 # define NTDDI_VERSION NTDDI_WINXP // Windows XP
16 # define _WIN32_WINNT 0x501 // Windows XP
17 # define _WIN32_WINDOWS 0x501 // Windows XP
18 # define WINVER 0x0501 // Windows XP
19 # define _WIN32_IE_ 0x0600 // 6.0 (XP+)
24 # define _CRT_SECURE_NO_DEPRECATE
25 # define _CRT_SECURE_NO_WARNINGS
26 # define _CRT_NONSTDC_NO_WARNINGS
29 #if defined(__APPLE__)
33 #if defined(__HAIKU__)
34 # include <SupportDefs.h>
36 # define _DEFAULT_SOURCE
38 # define TROUBLED_INTS
41 #if defined(__HAIKU__) || defined(__CYGWIN__)
49 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600)
53 # include <inttypes.h>
55 # define __STDC_LIMIT_MACROS
61 #if !defined(UINT64_MAX)
62 # define UINT64_MAX (18446744073709551615ULL)
64 #if !defined(INT64_MAX)
65 # define INT64_MAX (9223372036854775807LL)
67 #if !defined(INT64_MIN)
68 # define INT64_MIN (-INT64_MAX - 1)
70 #if !defined(UINT32_MAX)
71 # define UINT32_MAX (4294967295U)
73 #if !defined(INT32_MAX)
74 # define INT32_MAX (2147483647)
76 #if !defined(INT32_MIN)
77 # define INT32_MIN (-INT32_MAX - 1)
79 #if !defined(UINT16_MAX)
80 # define UINT16_MAX (65535U)
82 #if !defined(INT16_MAX)
83 # define INT16_MAX (32767)
85 #if !defined(INT16_MIN)
86 # define INT16_MIN (-INT16_MAX - 1)
88 #if !defined(UINT8_MAX)
89 # define UINT8_MAX (255)
91 #if !defined(INT8_MAX)
92 # define INT8_MAX (127)
94 #if !defined(INT8_MIN)
95 # define INT8_MIN (-INT8_MAX - 1)
109 # define SIZE_MAX ((size_t)-1)
112 #if defined(UNIX) || defined(__MINGW32__)
113 # include <sys/types.h>
118 # define strcasecmp stricmp
121 #if defined(SUNOS) || defined(HPUX) || defined(__CYGWIN__)
126 #if defined(__GNUC__) || (defined(__clang__) && !defined(_MSC_VER))
127 # define NORETURN __attribute__ ((noreturn))
129 # define __int64 long long
132 # define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
133 # define WARN_TIME_FORMAT(string) __attribute__ ((format (strftime, string, 0)))
134 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
142 # if __cplusplus > 201402L // C++17
143 # define FALLTHROUGH [[fallthrough]]
145 # define FALLTHROUGH __attribute__((fallthrough))
152 #if __GNUC__ > 11 || (__GNUC__ == 11 && __GNUC_MINOR__ >= 1)
153 # define NOACCESS(args) __attribute__ ((access (none, args)))
155 # define NOACCESS(args)
158 #if defined(__WATCOMC__)
161 # define WARN_FORMAT(string, args)
162 # define WARN_TIME_FORMAT(string)
168 #if defined(__MINGW32__)
173 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
177 #if defined(_MSC_VER)
179 # define NOMINMAX // Disable min/max macros in windows.h.
181 # pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
182 # pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
183 # pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
184 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
186 # if (_MSC_VER < 1400) // MSVC 2005 safety checks
187 # error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
189 # pragma warning(disable: 4291) // no matching operator delete found; memory will not be freed if initialization throws an exception (reason: our overloaded functions never throw an exception)
190 # pragma warning(disable: 4996) // 'function': was declared deprecated
191 # pragma warning(disable: 6308) // code analyzer: 'realloc' might return null pointer: assigning null pointer to 't_ptr', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
192 # pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
193 # pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
194 # pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
195 # pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
196 # pragma warning(disable: 6246) // code analyzer: Local declaration of 'statspec' hides declaration of the same name in outer scope. For additional information, see previous declaration at ...
198 # if (_MSC_VER == 1500) // Addresses item #13 on http://blogs.msdn.com/b/vcblog/archive/2008/08/11/tr1-fixes-in-vc9-sp1.aspx, for Visual Studio 2008
199 # define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
204 # define NORETURN __declspec(noreturn)
205 # if (_MSC_VER < 1900)
206 # define inline __forceinline
209 # define CDECL _cdecl
210 # define WARN_FORMAT(string, args)
211 # define WARN_TIME_FORMAT(string)
215 # if (_MSC_VER >= 1910) || defined(__clang__)
216 # define FALLTHROUGH [[fallthrough]]
221 # if defined(_WIN32) && !defined(_WIN64)
226 typedef _W64
int INT_PTR, *PINT_PTR;
227 typedef _W64
unsigned int UINT_PTR, *PUINT_PTR;
231 # define fseek _fseeki64
235 # if defined(WITH_ZLIB)
236 # if !defined(ZEXPORT)
237 # define ZEXPORT CDECL
242 # if defined(WITH_FREETYPE)
243 # if !defined(FT_EXPORT)
244 # define FT_EXPORT( x ) extern "C" x CDECL
249 # if defined(WITH_LIBLZMA)
250 # if !defined(LZMA_API_STATIC)
251 # define LZMA_API_STATIC
255 # define strcasecmp stricmp
256 # define strncasecmp strnicmp
257 # define strtoull _strtoui64
260 # define S_ISDIR(mode) (mode & S_IFDIR)
261 # define S_ISREG(mode) (mode & S_IFREG)
265 #if !defined(STRGEN) && !defined(SETTINGSGEN)
267 char *getcwd(
char *buf,
size_t size);
271 # define fopen(file, mode) _wfopen(OTTD2FS(file).c_str(), _T(mode))
272 # define unlink(file) _wunlink(OTTD2FS(file).c_str())
274 std::string
FS2OTTD(
const std::wstring &name);
275 std::wstring
OTTD2FS(
const std::string &name);
276 # elif defined(WITH_ICONV)
277 # define fopen(file, mode) fopen(OTTD2FS(file).c_str(), mode)
278 std::string
FS2OTTD(
const std::string &name);
279 std::string
OTTD2FS(
const std::string &name);
282 template <
typename T> std::string
FS2OTTD(T name) {
return name; }
283 template <
typename T> std::string
OTTD2FS(T name) {
return name; }
287 #if defined(_WIN32) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
288 # define PATHSEP "\\"
289 # define PATHSEPCHAR '\\'
292 # define PATHSEPCHAR '/'
295 #if defined(_MSC_VER) || defined(__WATCOMC__)
296 # define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
297 #elif defined(__MINGW32__)
298 # define PRAGMA(x) _Pragma(#x)
299 # define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
301 # define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
303 #define PACK(type_dec) PACK_N(type_dec, 1)
306 #if defined(_MSC_VER) || defined(__MINGW32__)
307 # define OTTD_PRINTF64 "%I64d"
308 # define OTTD_PRINTFHEX64 "%I64x"
309 # define PRINTF_SIZE "%Iu"
310 # define PRINTF_SIZEX "%IX"
312 # define OTTD_PRINTF64 "%lld"
313 # define OTTD_PRINTFHEX64 "%llx"
314 # define PRINTF_SIZE "%zu"
315 # define PRINTF_SIZEX "%zX"
318 typedef unsigned char byte;
321 #if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__) || defined(__CYGWIN__)
322 typedef unsigned int uint;
325 #if defined(TROUBLED_INTS)
330 # define uint32 uint32_ugly_hack
331 # define int32 int32_ugly_hack
332 # define uint64 uint64_ugly_hack
333 # define int64 int64_ugly_hack
334 typedef unsigned int uint32_ugly_hack;
335 typedef signed int int32_ugly_hack;
336 typedef unsigned __int64 uint64_ugly_hack;
337 typedef signed __int64 int64_ugly_hack;
339 typedef unsigned char uint8;
340 typedef signed char int8;
341 typedef unsigned short uint16;
342 typedef signed short int16;
343 typedef unsigned int uint32;
344 typedef signed int int32;
345 typedef unsigned __int64 uint64;
346 typedef signed __int64 int64;
349 #if !defined(WITH_PERSONAL_DIR)
350 # define PERSONAL_DIR ""
354 #if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
359 static_assert(
sizeof(uint64) == 8);
360 static_assert(
sizeof(uint32) == 4);
361 static_assert(
sizeof(uint16) == 2);
362 static_assert(
sizeof(uint8) == 1);
363 static_assert(SIZE_MAX >= UINT32_MAX);
366 #define M_PI_2 1.57079632679489661923
367 #define M_PI 3.14159265358979323846
378 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
386 #define endof(x) (&x[lengthof(x)])
394 #define lastof(x) (&x[lengthof(x) - 1])
402 #define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
410 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
414 #if defined(__APPLE__)
415 # define GetString OTTD_GetString
416 # define DrawString OTTD_DrawString
417 # define CloseConnection OTTD_CloseConnection
420 #if defined(__GNUC__) || defined(__clang__)
421 # define likely(x) __builtin_expect(!!(x), 1)
422 # define unlikely(x) __builtin_expect(!!(x), 0)
424 # define likely(x) (x)
425 # define unlikely(x) (x)
429 #define FMT_HEADER_ONLY
431 void NORETURN CDECL
usererror(
const char *str, ...) WARN_FORMAT(1, 2);
432 void NORETURN CDECL error(const
char *str, ...) WARN_FORMAT(1, 2);
433 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
436 #if defined(NDEBUG) && defined(WITH_ASSERT)
438 # define assert(expression) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
443 # define _stricmp strcasecmp
446 #if defined(MAX_PATH)
448 #elif defined(PATH_MAX) && PATH_MAX > 0
450 # define MAX_PATH PATH_MAX
453 # define MAX_PATH 260
460 static inline void free(
const void *ptr)
462 free(
const_cast<void *
>(ptr));
469 #define MAX_UVALUE(type) ((type)~(type)0)
471 #if defined(_MSC_VER) && !defined(_DEBUG)
472 # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
473 # define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
474 #elif defined(__GNUC__) && !defined(_DEBUG)
475 # define HELPER0(x) #x
476 # define HELPER1(x) HELPER0(GCC diagnostic ignored x)
477 # define HELPER2(y) HELPER1(#y)
478 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
479 # define IGNORE_UNINITIALIZED_WARNING_START \
480 _Pragma("GCC diagnostic push") \
481 _Pragma(HELPER2(-Wuninitialized)) \
482 _Pragma(HELPER2(-Wmaybe-uninitialized))
483 # define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
487 #ifndef IGNORE_UNINITIALIZED_WARNING_START
488 # define IGNORE_UNINITIALIZED_WARNING_START
489 # define IGNORE_UNINITIALIZED_WARNING_STOP