# stdafx.h

Go to the documentation of this file.

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