# stdafx.h

Go to the documentation of this file.

    00001 /* $Id: stdafx.h 18862 2010-01-18 15:41:38Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD 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.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef STDAFX_H
    00013 #define STDAFX_H
    00014
    00015 #if defined(__APPLE__)
    00016   #include "os/macosx/osx_stdafx.h"
    00017 #endif /* __APPLE__ */
    00018
    00019 #if defined(__BEOS__) || defined(__HAIKU__)
    00020   #include <SupportDefs.h>
    00021   #include <unistd.h>
    00022   #define _GNU_SOURCE
    00023   #define TROUBLED_INTS
    00024 #elif defined(__NDS__)
    00025   #include <nds/jtypes.h>
    00026   #define TROUBLED_INTS
    00027 #endif
    00028
    00029 /* It seems that we need to include stdint.h before anything else
    00030  * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
    00031  * does not have stdint.h and apparently neither does MorphOS, so define
    00032  * INT64_MAX for them ourselves. */
    00033 #if defined(__APPLE__)
    00034   /* Already done in osx_stdafx.h */
    00035 #elif !defined(_MSC_VER) && !defined( __MORPHOS__) && !defined(_STDINT_H_)
    00036   #if defined(SUNOS)
    00037     /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
    00038      * stdint.h defines and we need. */
    00039     #include <inttypes.h>
    00040   # else
    00041     #define __STDC_LIMIT_MACROS
    00042     #include <stdint.h>
    00043   #endif
    00044 #else
    00045   #define UINT64_MAX (18446744073709551615ULL)
    00046   #define INT64_MAX  (9223372036854775807LL)
    00047   #define INT64_MIN  (-INT64_MAX - 1)
    00048   #define UINT32_MAX (4294967295U)
    00049   #define INT32_MAX  (2147483647)
    00050   #define INT32_MIN  (-INT32_MAX - 1)
    00051   #define UINT16_MAX (65535U)
    00052   #define INT16_MAX  (32767)
    00053   #define INT16_MIN  (-INT16_MAX - 1)
    00054 #endif
    00055
    00056 #include <cstdio>
    00057 #include <cstddef>
    00058 #include <cstring>
    00059 #include <cstdlib>
    00060 #include <climits>
    00061 #include <cassert>
    00062
    00063 #if defined(UNIX) || defined(__MINGW32__)
    00064   #include <sys/types.h>
    00065 #endif
    00066
    00067 #if defined(__OS2__)
    00068   #include <types.h>
    00069   #define strcasecmp stricmp
    00070 #endif
    00071
    00072 #if defined(PSP)
    00073   #include <psptypes.h>
    00074   #include <pspdebug.h>
    00075   #include <pspthreadman.h>
    00076 #endif
    00077
    00078 #if defined(SUNOS) || defined(HPUX)
    00079   #include <alloca.h>
    00080 #endif
    00081
    00082 #if defined(__MORPHOS__)
    00083   /* MorphOS defines certain Amiga defines per default, we undefine them
    00084    * here to make the rest of source less messy and more clear what is
    00085    * required for morphos and what for AmigaOS */
    00086   #if defined(amigaos)
    00087     #undef amigaos
    00088   #endif
    00089   #if defined(__amigaos__)
    00090     #undef __amigaos__
    00091   # endif
    00092   #if defined(__AMIGA__)
    00093     #undef __AMIGA__
    00094   #endif
    00095   #if defined(AMIGA)
    00096     #undef AMIGA
    00097   #endif
    00098   #if defined(amiga)
    00099     #undef amiga
    00100   #endif
    00101   /* Act like we already included this file, as it somehow gives linkage problems
    00102    *  (mismatch linkage of C++ and C between this include and unistd.h). */
    00103   #define CLIB_USERGROUP_PROTOS_H
    00104 #endif /* __MORPHOS__ */
    00105
    00106 #if defined(PSP)
    00107   /* PSP can only have 10 file-descriptors open at any given time, but this
    00108    *  switch only limits reads via the Fio system. So keep 2 fds free for things
    00109    *  like saving a game. */
    00110   #define LIMITED_FDS 8
    00111   #define printf pspDebugScreenPrintf
    00112 #endif /* PSP */
    00113
    00114 /* Stuff for GCC */
    00115 #if defined(__GNUC__)
    00116   #define NORETURN __attribute__ ((noreturn))
    00117   #define FORCEINLINE inline
    00118   #define CDECL
    00119   #define __int64 long long
    00120   #define GCC_PACK __attribute__((packed))
    00121   /* Warn about functions using 'printf' format syntax. First argument determines which parameter
    00122    * is the format string, second argument is start of values passed to printf. */
    00123   #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
    00124 #endif /* __GNUC__ */
    00125
    00126 #if defined(__WATCOMC__)
    00127   #define NORETURN
    00128   #define FORCEINLINE inline
    00129   #define CDECL
    00130   #define GCC_PACK
    00131   #define WARN_FORMAT(string, args)
    00132   #include <malloc.h>
    00133 #endif /* __WATCOMC__ */
    00134
    00135 #if defined(__MINGW32__) || defined(__CYGWIN__)
    00136   #include <malloc.h> // alloca()
    00137 #endif
    00138
    00139 #if defined(WIN32)
    00140   #define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
    00141 #endif
    00142
    00143 /* Stuff for MSVC */
    00144 #if defined(_MSC_VER)
    00145   #pragma once
    00146   /* Define a win32 target platform, to override defaults of the SDK
    00147    * We need to define NTDDI version for Vista SDK, but win2k is minimum */
    00148   #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
    00149   #define _WIN32_WINNT 0x0500       // Windows 2000
    00150   #define _WIN32_WINDOWS 0x400      // Windows 95
    00151   #if !defined(WINCE)
    00152     #define WINVER 0x0400     // Windows NT 4.0 / Windows 95
    00153   #endif
    00154   #define _WIN32_IE_ 0x0401         // 4.01 (win98 and NT4SP5+)
    00155
    00156   #pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', possible loss of data
    00157   #pragma warning(disable: 4761)  // integral size mismatch in argument : conversion supplied
    00158   #pragma warning(disable: 4200)  // nonstandard extension used : zero-sized array in struct/union
    00159
    00160   #if (_MSC_VER < 1400)                   // MSVC 2005 safety checks
    00161     #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
    00162   #endif /* (_MSC_VER < 1400) */
    00163   #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)
    00164   #pragma warning(disable: 4996)   // 'strdup' was declared deprecated
    00165   #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
    00166   #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
    00167   #pragma warning(disable: 6011)   // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
    00168   #pragma warning(disable: 6326)   // code analyzer: potential comparison of a constant with another constant
    00169   #pragma warning(disable: 6031)   // code analyzer: Return value ignored: 'ReadFile'
    00170   #pragma warning(disable: 6255)   // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
    00171   #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 ...
    00172   #define WARN_FORMAT(string, args)
    00173
    00174   #include <malloc.h> // alloca()
    00175   #define NORETURN __declspec(noreturn)
    00176   #define FORCEINLINE __forceinline
    00177   #define inline _inline
    00178
    00179   #if !defined(WINCE)
    00180     #define CDECL _cdecl
    00181   #endif
    00182
    00183   #define GCC_PACK
    00184
    00185   int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
    00186   #if defined(WINCE)
    00187     int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
    00188   #endif
    00189
    00190   #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
    00191     #if !defined(_W64)
    00192       #define _W64
    00193     #endif
    00194
    00195     typedef _W64 int INT_PTR, *PINT_PTR;
    00196     typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
    00197   #endif /* WIN32 && !_WIN64 && !WIN64 */
    00198
    00199   #if defined(_WIN64) || defined(WIN64)
    00200     #define fseek _fseeki64
    00201   #endif /* _WIN64 || WIN64 */
    00202
    00203   /* This is needed to zlib uses the stdcall calling convention on visual studio */
    00204   #if defined(WITH_ZLIB) || defined(WITH_PNG)
    00205     #if !defined(ZLIB_WINAPI)
    00206       #define ZLIB_WINAPI
    00207     #endif
    00208   #endif
    00209
    00210   #if defined(WINCE)
    00211     #define strcasecmp _stricmp
    00212     #define strncasecmp _strnicmp
    00213     #undef DEBUG
    00214   #else
    00215     #define strcasecmp stricmp
    00216     #define strncasecmp strnicmp
    00217   #endif
    00218
    00219   /* MSVC doesn't have these :( */
    00220   #define S_ISDIR(mode) (mode & S_IFDIR)
    00221   #define S_ISREG(mode) (mode & S_IFREG)
    00222
    00223 #endif /* defined(_MSC_VER) */
    00224
    00225 #if defined(WINCE)
    00226   #define strdup _strdup
    00227 #endif /* WINCE */
    00228
    00229 /* NOTE: the string returned by these functions is only valid until the next
    00230  * call to the same function and is not thread- or reentrancy-safe */
    00231 #if !defined(STRGEN)
    00232   #if defined(WIN32) || defined(WIN64)
    00233     char *getcwd(char *buf, size_t size);
    00234     #include <tchar.h>
    00235     #include <io.h>
    00236
    00237     /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
    00238     #if !defined(WINCE)
    00239       #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
    00240       #define unlink(file) _tunlink(OTTD2FS(file))
    00241     #endif /* WINCE */
    00242
    00243     const char *FS2OTTD(const TCHAR *name);
    00244     const TCHAR *OTTD2FS(const char *name);
    00245     #define SQ2OTTD(name) FS2OTTD(name)
    00246     #define OTTD2SQ(name) OTTD2FS(name)
    00247   #else
    00248     #define fopen(file, mode) fopen(OTTD2FS(file), mode)
    00249     const char *FS2OTTD(const char *name);
    00250     const char *OTTD2FS(const char *name);
    00251     #define SQ2OTTD(name) (name)
    00252     #define OTTD2SQ(name) (name)
    00253   #endif /* WIN32 */
    00254 #endif /* STRGEN */
    00255
    00256 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
    00257   #define PATHSEP "\\"
    00258   #define PATHSEPCHAR '\\'
    00259 #else
    00260   #define PATHSEP "/"
    00261   #define PATHSEPCHAR '/'
    00262 #endif
    00263
    00264 /* MSVCRT of course has to have a different syntax for long long *sigh* */
    00265 #if defined(_MSC_VER) || defined(__MINGW32__)
    00266   #define OTTD_PRINTF64 "%I64d"
    00267   #define PRINTF_SIZE "%Iu"
    00268 #else
    00269   #define OTTD_PRINTF64 "%lld"
    00270   #define PRINTF_SIZE "%zu"
    00271 #endif
    00272
    00273 typedef unsigned char byte;
    00274
    00275 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
    00276 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
    00277   typedef unsigned int uint;
    00278 #endif
    00279
    00280 #if defined(TROUBLED_INTS)
    00281   /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
    00282    * trouble all over the place in OpenTTD. */
    00283   #define uint32 uint32_ugly_hack
    00284   #define int32 int32_ugly_hack
    00285   typedef unsigned int uint32_ugly_hack;
    00286   typedef signed int int32_ugly_hack;
    00287 #else
    00288   typedef unsigned char    uint8;
    00289   typedef   signed char     int8;
    00290   typedef unsigned short   uint16;
    00291   typedef   signed short    int16;
    00292   typedef unsigned int     uint32;
    00293   typedef   signed int      int32;
    00294   typedef unsigned __int64 uint64;
    00295   typedef   signed __int64  int64;
    00296 #endif /* !TROUBLED_INTS */
    00297
    00298 #if !defined(WITH_PERSONAL_DIR)
    00299   #define PERSONAL_DIR ""
    00300 #endif
    00301
    00302 /* Compile time assertions. Prefer c++0x static_assert() */
    00303 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
    00304   /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
    00305   #define assert_compile(expr) static_assert(expr, #expr )
    00306 #elif defined(__OS2__)
    00307   /* Disabled for OS/2 */
    00308   #define assert_compile(expr)
    00309 #else
    00310   #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
    00311 #endif
    00312
    00313 /* Check if the types have the bitsizes like we are using them */
    00314 assert_compile(sizeof(uint64) == 8);
    00315 assert_compile(sizeof(uint32) == 4);
    00316 assert_compile(sizeof(uint16) == 2);
    00317 assert_compile(sizeof(uint8)  == 1);
    00318
    00327 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
    00328
    00335 #define endof(x) (&x[lengthof(x)])
    00336
    00343 #define lastof(x) (&x[lengthof(x) - 1])
    00344
    00345 #define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
    00346 #if !defined(offsetof)
    00347   #define offsetof(s, m) cpp_offsetof(s, m)
    00348 #endif /* offsetof */
    00349
    00350
    00351 /* take care of some name clashes on MacOS */
    00352 #if defined(__APPLE__)
    00353   #define GetString OTTD_GetString
    00354   #define DrawString OTTD_DrawString
    00355   #define CloseConnection OTTD_CloseConnection
    00356 #endif /* __APPLE__ */
    00357
    00358 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
    00359 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
    00360 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
    00361
    00362 /* For non-debug builds with assertions enabled use the special assertion handler:
    00363  * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
    00364  * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
    00365  */
    00366 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
    00367   #undef assert
    00368   #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
    00369 #endif
    00370
    00371 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
    00372   /* MorphOS and NDS don't have C++ conformant _stricmp... */
    00373   #define _stricmp stricmp
    00374 #elif defined(OPENBSD)
    00375   /* OpenBSD uses strcasecmp(3) */
    00376   #define _stricmp strcasecmp
    00377 #endif
    00378
    00379 #if !defined(MAX_PATH)
    00380   #define MAX_PATH 260
    00381 #endif
    00382
    00387 #define MAX_UVALUE(type) ((type)~(type)0)
    00388
    00389 #endif /* STDAFX_H */