tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file stdafx.h Definition of base types and functions in a cross-platform compatible way. */ belugas@6420: Darkvater@2436: #ifndef STDAFX_H Darkvater@2436: #define STDAFX_H truelight@0: rubidium@8373: #if defined(__NDS__) rubidium@8373: #include rubidium@8373: /* NDS' types for uint32/int32 are based on longs, which causes rubidium@8373: * trouble all over the place in OpenTTD. */ rubidium@8373: #define uint32 uint32_ugly_hack rubidium@8373: #define int32 int32_ugly_hack rubidium@8373: typedef unsigned int uint32_ugly_hack; rubidium@8373: typedef signed int int32_ugly_hack; rubidium@8373: #endif /* __NDS__ */ rubidium@8373: bjarni@5820: /* It seems that we need to include stdint.h before anything else bjarni@5820: * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC bjarni@5820: * does not have stdint.h and apparently neither does MorphOS, so define bjarni@7775: * INT64_MAX for them ourselves. bjarni@7775: * Sometimes OSX headers manages to include stdint.h before this but without bjarni@7775: * __STDC_LIMIT_MACROS so it will be without INT64_*. We need to define those bjarni@7775: * too if this is the case. */ bjarni@7775: #if !defined(_MSC_VER) && !defined( __MORPHOS__) && !defined(_STDINT_H_) skidd13@8004: #if defined(SUNOS) skidd13@8004: /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything skidd13@8004: * stdint.h defines and we need. */ skidd13@8004: #include skidd13@8004: # else skidd13@8004: #define __STDC_LIMIT_MACROS skidd13@8004: #include skidd13@8004: #endif bjarni@5820: #else smatz@8825: #define UINT64_MAX (18446744073709551615ULL) smatz@8825: #define INT64_MAX (9223372036854775807LL) smatz@8825: #define INT64_MIN (-INT64_MAX - 1) smatz@8825: #define UINT32_MAX (4294967295U) smatz@8825: #define INT32_MAX (2147483647) smatz@8825: #define INT32_MIN (-INT32_MAX - 1) smatz@8825: #define UINT16_MAX (65535U) smatz@8825: #define INT16_MAX (32767) smatz@8825: #define INT16_MIN (-INT16_MAX - 1) bjarni@5820: #endif bjarni@5820: rubidium@5933: #include rubidium@5933: #include rubidium@5933: #include rubidium@5933: #include rubidium@5933: #include truelight@0: rubidium@5934: /* MacOS X will use an NSAlert to display failed assertaions since they're lost unless running from a terminal rubidium@5934: * strgen always runs from terminal and don't need a window for asserts */ bjarni@2731: #if !defined(__APPLE__) || defined(STRGEN) skidd13@8004: #include bjarni@2223: #else skidd13@8004: #include "os/macosx/macos.h" bjarni@2223: #endif bjarni@2223: darkvater@858: #if defined(UNIX) || defined(__MINGW32__) skidd13@8004: #include truelight@0: #endif truelight@0: truelight@781: #if defined(__OS2__) skidd13@8004: #include skidd13@8004: #define strcasecmp stricmp truelight@781: #endif truelight@781: truelight@5976: #if defined(PSP) skidd13@8004: #include skidd13@8004: #include skidd13@8004: #include tron@446: #endif tron@446: skidd13@8004: #if defined(__BEOS__) skidd13@8004: #include skidd13@8004: #endif skidd13@8004: rubidium@9072: #if defined(SUNOS) || defined(HPUX) skidd13@8004: #include skidd13@8004: #endif skidd13@8004: skidd13@8004: #if defined(__MORPHOS__) skidd13@8004: /* MorphOS defines certain Amiga defines per default, we undefine them skidd13@8004: * here to make the rest of source less messy and more clear what is skidd13@8004: * required for morphos and what for AmigaOS */ skidd13@8004: #if defined(amigaos) skidd13@8004: #undef amigaos skidd13@8004: #endif skidd13@8004: #if defined(__amigaos__) skidd13@8004: #undef __amigaos__ skidd13@8004: # endif skidd13@8004: #if defined(__AMIGA__) skidd13@8004: #undef __AMIGA__ skidd13@8004: #endif skidd13@8004: #if defined(AMIGA) skidd13@8004: #undef AMIGA skidd13@8004: #endif skidd13@8004: #if defined(amiga) skidd13@8004: #undef amiga skidd13@8004: #endif skidd13@8004: /* Act like we already included this file, as it somehow gives linkage problems skidd13@8004: * (mismatch linkage of C++ and C between this include and unistd.h). */ skidd13@8004: #define CLIB_USERGROUP_PROTOS_H bjarni@770: #endif /* __MORPHOS__ */ bjarni@770: skidd13@8004: #if defined(__APPLE__) skidd13@8004: #include "os/macosx/osx_stdafx.h" Darkvater@3289: #endif /* __APPLE__ */ truelight@0: truelight@5967: #if defined(PSP) skidd13@8004: /* PSP can only have 10 file-descriptors open at any given time, but this skidd13@8004: * switch only limits reads via the Fio system. So keep 2 fds free for things skidd13@8004: * like saving a game. */ skidd13@8004: #define LIMITED_FDS 8 skidd13@8004: #define printf pspDebugScreenPrintf truelight@5967: #endif /* PSP */ truelight@5967: rubidium@5934: /* by default we use [] var arrays */ truelight@0: #define VARARRAY_SIZE truelight@0: rubidium@5934: /* Stuff for GCC */ truelight@0: #if defined(__GNUC__) skidd13@8004: #define NORETURN __attribute__ ((noreturn)) skidd13@8004: #define FORCEINLINE inline skidd13@8004: #define CDECL skidd13@8004: #define __int64 long long skidd13@8004: #define GCC_PACK __attribute__((packed)) truelight@0: skidd13@8004: #if (__GNUC__ == 2) skidd13@8004: #undef VARARRAY_SIZE skidd13@8004: #define VARARRAY_SIZE 0 skidd13@8004: #endif Darkvater@2485: #endif /* __GNUC__ */ truelight@0: truelight@781: #if defined(__WATCOMC__) skidd13@8004: #define NORETURN skidd13@8004: #define FORCEINLINE inline skidd13@8004: #define CDECL skidd13@8004: #define GCC_PACK skidd13@8004: #include Darkvater@2485: #endif /* __WATCOMC__ */ truelight@781: darkvater@796: #if defined(__MINGW32__) || defined(__CYGWIN__) skidd13@8004: #include // alloca() darkvater@796: #endif darkvater@796: glx@9256: #if defined(WIN32) glx@9256: #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers glx@9256: #endif glx@9256: rubidium@5934: /* Stuff for MSVC */ truelight@0: #if defined(_MSC_VER) skidd13@8004: #pragma once skidd13@8004: /* Define a win32 target platform, to override defaults of the SDK skidd13@8004: * We need to define NTDDI version for Vista SDK, but win2k is minimum */ skidd13@8004: #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000 skidd13@8004: #define _WIN32_WINNT 0x0500 // Windows 2000 skidd13@8004: #define _WIN32_WINDOWS 0x400 // Windows 95 skidd13@8004: #if !defined(WINCE) skidd13@8004: #define WINVER 0x0400 // Windows NT 4.0 / Windows 95 skidd13@8004: #endif skidd13@8004: #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+) Darkvater@2695: skidd13@8004: #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data skidd13@8004: #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied skidd13@8004: #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union Darkvater@2485: rubidium@8409: #if (_MSC_VER < 1400) // MSVC 2005 safety checks rubidium@8409: #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not!. Upgrade your compiler." rubidium@8409: #endif /* (_MSC_VER < 1400) */ rubidium@8409: #pragma warning(disable: 4996) // 'strdup' was declared deprecated rubidium@8409: #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions rubidium@8409: #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 rubidium@8409: #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001 rubidium@8409: #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant rubidium@8409: #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile' rubidium@8409: #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead rubidium@8409: #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 ... Darkvater@2694: skidd13@8004: #include // alloca() skidd13@8004: #define NORETURN __declspec(noreturn) skidd13@8004: #define FORCEINLINE __forceinline skidd13@8004: #define inline _inline Darkvater@2694: skidd13@8004: #if !defined(WINCE) skidd13@8004: #define CDECL _cdecl skidd13@8004: #endif skidd13@8004: skidd13@8004: int CDECL snprintf(char *str, size_t size, const char *format, ...); skidd13@8004: #if (_MSC_VER < 1400) || defined(WINCE) skidd13@8004: int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap); skidd13@8004: #endif skidd13@8004: skidd13@8004: #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64) skidd13@8004: #if !defined(_W64) skidd13@8004: #define _W64 skidd13@8004: #endif skidd13@8004: skidd13@8004: typedef _W64 int INT_PTR, *PINT_PTR; skidd13@8004: typedef _W64 unsigned int UINT_PTR, *PUINT_PTR; skidd13@8004: #endif /* WIN32 && !_WIN64 && !WIN64 */ skidd13@8004: skidd13@8004: #define GCC_PACK skidd13@8004: skidd13@8004: /* This is needed to zlib uses the stdcall calling convention on visual studio */ skidd13@8004: #if defined(WITH_ZLIB) || defined(WITH_PNG) skidd13@8004: #if !defined(ZLIB_WINAPI) skidd13@8004: #define ZLIB_WINAPI skidd13@8004: #endif skidd13@8004: #endif skidd13@8004: skidd13@8004: #if defined(WINCE) skidd13@8004: #define strcasecmp _stricmp skidd13@8004: #define strncasecmp _strnicmp skidd13@8004: #undef DEBUG skidd13@8004: #else skidd13@8004: #define strcasecmp stricmp skidd13@8004: #define strncasecmp strnicmp skidd13@8004: #endif glx@8051: glx@8051: void SetExceptionString(const char* s, ...); glx@8051: glx@8051: #if defined(NDEBUG) && defined(WITH_ASSERT) glx@8051: #undef assert glx@8051: #define assert(expression) if (!(expression)) { SetExceptionString("Assertion failed at %s:%d: %s", __FILE__, __LINE__, #expression); *(byte*)0 = 0; } glx@8051: #endif Darkvater@2485: #endif /* defined(_MSC_VER) */ Darkvater@2483: truelight@5755: #if defined(WINCE) skidd13@8004: #define strdup _strdup truelight@5755: #endif /* WINCE */ truelight@5755: Darkvater@5167: /* NOTE: the string returned by these functions is only valid until the next Darkvater@5167: * call to the same function and is not thread- or reentrancy-safe */ Darkvater@5167: #if !defined(STRGEN) skidd13@8004: #if defined(WIN32) || defined(WIN64) skidd13@8004: char *getcwd(char *buf, size_t size); skidd13@8004: #include Darkvater@6251: skidd13@8004: /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */ skidd13@8004: #if !defined(WINCE) skidd13@8004: #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode)) skidd13@8004: #endif /* WINCE */ Darkvater@6251: skidd13@8004: const char *FS2OTTD(const TCHAR *name); skidd13@8004: const TCHAR *OTTD2FS(const char *name); skidd13@8004: #else skidd13@8004: #define fopen(file, mode) fopen(OTTD2FS(file), mode) skidd13@8004: const char *FS2OTTD(const char *name); skidd13@8004: const char *OTTD2FS(const char *name); skidd13@8004: #endif /* WIN32 */ Darkvater@5167: #endif /* STRGEN */ Darkvater@2482: orudge@5591: #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__) skidd13@8004: #define PATHSEP "\\" skidd13@8004: #define PATHSEPCHAR '\\' truelight@4379: #else skidd13@8004: #define PATHSEP "/" skidd13@8004: #define PATHSEPCHAR '/' truelight@0: #endif truelight@0: truelight@0: typedef unsigned char byte; skidd13@8004: skidd13@8004: /* This is already defined in unix, but not in QNX Neutrino (6.x)*/ skidd13@8004: #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__MORPHOS__)) || defined(__QNXNTO__) skidd13@8004: typedef unsigned int uint; truelight@0: #endif truelight@0: rubidium@8373: #if !defined(__BEOS__) && !defined(__NDS__) /* Already defined on BEOS and NDS */ skidd13@8004: typedef unsigned char uint8; skidd13@8004: typedef signed char int8; skidd13@8004: typedef unsigned short uint16; skidd13@8004: typedef signed short int16; skidd13@8004: typedef unsigned int uint32; skidd13@8004: typedef signed int int32; skidd13@8004: typedef unsigned __int64 uint64; skidd13@8004: typedef signed __int64 int64; rubidium@8373: #endif /* !__BEOS__ && !__NDS__ */ truelight@0: rubidium@6929: #if !defined(WITH_PERSONAL_DIR) skidd13@8004: #define PERSONAL_DIR "" truelight@0: #endif truelight@0: rubidium@5934: /* Compile time assertions */ skidd13@8004: #if defined(__OS2__) skidd13@8004: #define assert_compile(expr) truelight@781: #else rubidium@9117: #define assert_compile(expr) extern const int __ct_assert__[1 - 2 * !(expr)] KUDr@3900: #endif /* __OS2__ */ truelight@0: skidd13@8678: /* Check if the types have the bitsizes like we are using them */ skidd13@8678: assert_compile(sizeof(uint64) == 8); truelight@0: assert_compile(sizeof(uint32) == 4); truelight@0: assert_compile(sizeof(uint16) == 2); tron@2639: assert_compile(sizeof(uint8) == 1); truelight@0: rubidium@9117: #define lengthof(x) (sizeof(x) / sizeof(x[0])) truelight@0: #define endof(x) (&x[lengthof(x)]) tron@1317: #define lastof(x) (&x[lengthof(x) - 1]) KUDr@5671: rubidium@8969: #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast((((s*)(char*)8)->m))) - 8) skidd13@8004: #if !defined(offsetof) rubidium@8969: #define offsetof(s, m) cpp_offsetof(s, m) rubidium@6083: #endif /* offsetof */ truelight@0: truelight@0: rubidium@5934: /* take care of some name clashes on MacOS */ truelight@0: #if defined(__APPLE__) skidd13@8004: #define GetString OTTD_GetString skidd13@8004: #define DrawString OTTD_DrawString skidd13@8004: #define CloseConnection OTTD_CloseConnection skidd13@8126: #endif /* __APPLE__ */ bjarni@569: smatz@8983: void NORETURN CDECL error(const char *str, ...); rubidium@6622: #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__) rubidium@6622: rubidium@8373: #if defined(MORPHOS) || defined(__NDS__) rubidium@8373: /* MorphOS and NDS don't have C++ conformant _stricmp... */ skidd13@8004: #define _stricmp stricmp rubidium@7275: #elif defined(OPENBSD) skidd13@8004: /* OpenBSD uses strcasecmp(3) */ skidd13@8004: #define _stricmp strcasecmp rubidium@7275: #endif rubidium@7275: rubidium@8373: #if !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) rubidium@8373: /* NDS, MorphOS & OpenBSD don't know wchars, the rest does :( */ skidd13@8004: #define HAS_WCHAR rubidium@8373: #endif /* !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) */ rubidium@7255: rubidium@7266: #if !defined(MAX_PATH) skidd13@8004: #define MAX_PATH 260 rubidium@7266: #endif rubidium@7266: rubidium@8123: /** rubidium@8123: * The largest value that can be entered in a variable rubidium@8123: * @param type the type of the variable rubidium@8123: */ rubidium@8123: #define MAX_UVALUE(type) ((type)~(type)0) rubidium@8123: Darkvater@2436: #endif /* STDAFX_H */