15 # define _CRT_SECURE_NO_DEPRECATE
16 # define _CRT_SECURE_NO_WARNINGS
17 # define _CRT_NONSTDC_NO_WARNINGS
20 #if defined(__APPLE__)
24 #if defined(__HAIKU__)
25 # include <SupportDefs.h>
28 # define TROUBLED_INTS
31 #if defined(__HAIKU__) || defined(__CYGWIN__)
39 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600)
43 # include <inttypes.h>
45 # define __STDC_LIMIT_MACROS
51 #if !defined(UINT64_MAX)
52 # define UINT64_MAX (18446744073709551615ULL)
54 #if !defined(INT64_MAX)
55 # define INT64_MAX (9223372036854775807LL)
57 #if !defined(INT64_MIN)
58 # define INT64_MIN (-INT64_MAX - 1)
60 #if !defined(UINT32_MAX)
61 # define UINT32_MAX (4294967295U)
63 #if !defined(INT32_MAX)
64 # define INT32_MAX (2147483647)
66 #if !defined(INT32_MIN)
67 # define INT32_MIN (-INT32_MAX - 1)
69 #if !defined(UINT16_MAX)
70 # define UINT16_MAX (65535U)
72 #if !defined(INT16_MAX)
73 # define INT16_MAX (32767)
75 #if !defined(INT16_MIN)
76 # define INT16_MIN (-INT16_MAX - 1)
78 #if !defined(UINT8_MAX)
79 # define UINT8_MAX (255)
81 #if !defined(INT8_MAX)
82 # define INT8_MAX (127)
84 #if !defined(INT8_MIN)
85 # define INT8_MIN (-INT8_MAX - 1)
98 # define SIZE_MAX ((size_t)-1)
101 #if defined(UNIX) || defined(__MINGW32__)
102 # include <sys/types.h>
107 # define strcasecmp stricmp
110 #if defined(SUNOS) || defined(HPUX) || defined(__CYGWIN__)
115 #if defined(__GNUC__) || defined(__clang__)
116 # define NORETURN __attribute__ ((noreturn))
118 # define __int64 long long
121 # define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
122 # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
130 # if __cplusplus > 201402L // C++17
131 # define FALLTHROUGH [[fallthrough]]
133 # define FALLTHROUGH __attribute__((fallthrough))
140 #if defined(__WATCOMC__)
143 # define WARN_FORMAT(string, args)
149 #if defined(__MINGW32__)
154 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
158 #if defined(_MSC_VER)
160 # define NTDDI_VERSION NTDDI_WINXP // Windows XP
161 # define _WIN32_WINNT 0x501 // Windows XP
162 # define _WIN32_WINDOWS 0x501 // Windows XP
163 # define WINVER 0x0501 // Windows XP
164 # define _WIN32_IE_ 0x0600 // 6.0 (XP+)
166 # define NOMINMAX // Disable min/max macros in windows.h.
168 # pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
169 # pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
170 # pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
171 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
173 # if (_MSC_VER < 1400) // MSVC 2005 safety checks
174 # error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
176 # 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)
177 # pragma warning(disable: 4996) // 'function': was declared deprecated
178 # 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
179 # pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
180 # pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
181 # pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
182 # pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
183 # 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 ...
185 # 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
186 # define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
191 # define NORETURN __declspec(noreturn)
192 # if (_MSC_VER < 1900)
193 # define inline __forceinline
196 # define CDECL _cdecl
197 # define WARN_FORMAT(string, args)
199 # define FINAL sealed
205 # if (_MSC_VER >= 1910)
206 # define FALLTHROUGH [[fallthrough]]
211 # if defined(_WIN32) && !defined(_WIN64)
216 typedef _W64
int INT_PTR, *PINT_PTR;
217 typedef _W64
unsigned int UINT_PTR, *PUINT_PTR;
221 # define fseek _fseeki64
225 # if defined(WITH_ZLIB)
226 # if !defined(ZEXPORT)
227 # define ZEXPORT CDECL
232 # if defined(WITH_FREETYPE)
233 # if !defined(FT_EXPORT)
234 # define FT_EXPORT( x ) extern "C" x CDECL
239 # if defined(WITH_LIBLZMA)
240 # if !defined(LZMA_API_STATIC)
241 # define LZMA_API_STATIC
245 # define strcasecmp stricmp
246 # define strncasecmp strnicmp
247 # define strtoull _strtoui64
250 # define S_ISDIR(mode) (mode & S_IFDIR)
251 # define S_ISREG(mode) (mode & S_IFREG)
257 #if !defined(STRGEN) && !defined(SETTINGSGEN)
259 char *getcwd(
char *buf,
size_t size);
263 namespace std { using ::_wfopen; }
264 # define fopen(file, mode) _wfopen(OTTD2FS(file), _T(mode))
265 # define unlink(file) _wunlink(OTTD2FS(file))
267 const char *
FS2OTTD(
const wchar_t *name);
268 const wchar_t *
OTTD2FS(
const char *name,
bool console_cp =
false);
270 # define fopen(file, mode) fopen(OTTD2FS(file), mode)
271 const char *
FS2OTTD(
const char *name);
272 const char *
OTTD2FS(
const char *name);
276 #if defined(_WIN32) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
277 # define PATHSEP "\\"
278 # define PATHSEPCHAR '\\'
281 # define PATHSEPCHAR '/'
284 #if defined(_MSC_VER) || defined(__WATCOMC__)
285 # define PACK_N(type_dec, n) __pragma(pack(push, n)) type_dec; __pragma(pack(pop))
286 #elif defined(__MINGW32__)
287 # define PRAGMA(x) _Pragma(#x)
288 # define PACK_N(type_dec, n) PRAGMA(pack(push, n)) type_dec; PRAGMA(pack(pop))
290 # define PACK_N(type_dec, n) type_dec __attribute__((__packed__, aligned(n)))
292 #define PACK(type_dec) PACK_N(type_dec, 1)
295 #if defined(_MSC_VER) || defined(__MINGW32__)
296 # define OTTD_PRINTF64 "%I64d"
297 # define OTTD_PRINTFHEX64 "%I64x"
298 # define PRINTF_SIZE "%Iu"
299 # define PRINTF_SIZEX "%IX"
301 # define OTTD_PRINTF64 "%lld"
302 # define OTTD_PRINTFHEX64 "%llx"
303 # define PRINTF_SIZE "%zu"
304 # define PRINTF_SIZEX "%zX"
307 typedef unsigned char byte;
310 #if (!defined(UNIX) && !defined(__HAIKU__)) || defined(__QNXNTO__) || defined(__CYGWIN__)
311 typedef unsigned int uint;
314 #if defined(TROUBLED_INTS)
319 # define uint32 uint32_ugly_hack
320 # define int32 int32_ugly_hack
321 # define uint64 uint64_ugly_hack
322 # define int64 int64_ugly_hack
323 typedef unsigned int uint32_ugly_hack;
324 typedef signed int int32_ugly_hack;
325 typedef unsigned __int64 uint64_ugly_hack;
326 typedef signed __int64 int64_ugly_hack;
328 typedef unsigned char uint8;
329 typedef signed char int8;
330 typedef unsigned short uint16;
331 typedef signed short int16;
332 typedef unsigned int uint32;
333 typedef signed int int32;
334 typedef unsigned __int64 uint64;
335 typedef signed __int64 int64;
338 #if !defined(WITH_PERSONAL_DIR)
339 # define PERSONAL_DIR ""
343 #if defined(WITH_PERSONAL_DIR) && defined(UNIX) && !defined(__APPLE__)
348 static_assert(
sizeof(uint64) == 8);
349 static_assert(
sizeof(uint32) == 4);
350 static_assert(
sizeof(uint16) == 2);
351 static_assert(
sizeof(uint8) == 1);
352 static_assert(SIZE_MAX >= UINT32_MAX);
355 #define M_PI_2 1.57079632679489661923
356 #define M_PI 3.14159265358979323846
367 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
375 #define endof(x) (&x[lengthof(x)])
383 #define lastof(x) (&x[lengthof(x) - 1])
391 #define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
399 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
403 #if defined(__APPLE__)
404 # define GetString OTTD_GetString
405 # define DrawString OTTD_DrawString
406 # define CloseConnection OTTD_CloseConnection
409 #if defined(__GNUC__) || defined(__clang__)
410 # define likely(x) __builtin_expect(!!(x), 1)
411 # define unlikely(x) __builtin_expect(!!(x), 0)
413 # define likely(x) (x)
414 # define unlikely(x) (x)
417 void NORETURN CDECL
usererror(
const char *str, ...) WARN_FORMAT(1, 2);
418 void NORETURN CDECL error(const
char *str, ...) WARN_FORMAT(1, 2);
419 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
422 #if defined(NDEBUG) && defined(WITH_ASSERT)
424 # define assert(expression) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
428 #if !defined(NDEBUG) || defined(WITH_ASSERT)
430 # define assert_msg(expression, msg, ...) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s\n\t" msg, __LINE__, __FILE__, #expression, __VA_ARGS__);
432 # define assert_msg(expression, msg, ...)
437 # define _stricmp strcasecmp
440 #if defined(MAX_PATH)
442 #elif defined(PATH_MAX) && PATH_MAX > 0
444 # define MAX_PATH PATH_MAX
447 # define MAX_PATH 260
454 static inline void free(
const void *ptr)
456 free(
const_cast<void *
>(ptr));
463 #define MAX_UVALUE(type) ((type)~(type)0)
465 #if defined(_MSC_VER) && !defined(_DEBUG)
466 # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700))
467 # define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))
468 #elif defined(__GNUC__) && !defined(_DEBUG)
469 # define HELPER0(x) #x
470 # define HELPER1(x) HELPER0(GCC diagnostic ignored x)
471 # define HELPER2(y) HELPER1(#y)
472 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
473 # define IGNORE_UNINITIALIZED_WARNING_START \
474 _Pragma("GCC diagnostic push") \
475 _Pragma(HELPER2(-Wuninitialized)) \
476 _Pragma(HELPER2(-Wmaybe-uninitialized))
477 # define IGNORE_UNINITIALIZED_WARNING_STOP _Pragma("GCC diagnostic pop")
481 #ifndef IGNORE_UNINITIALIZED_WARNING_START
482 # define IGNORE_UNINITIALIZED_WARNING_START
483 # define IGNORE_UNINITIALIZED_WARNING_STOP