# stdafx.h

Go to the documentation of this file.

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