# stdafx.h

Go to the documentation of this file.

    00001 /* $Id: stdafx.h 18047 2009-11-11 22:09:34Z rubidium $ */
    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   /* Warn about functions using 'printf' format syntax. First argument determines which parameter
    00130    * is the format string, second argument is start of values passed to printf. */
    00131   #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
    00132
    00133   #if (__GNUC__ == 2)
    00134     #undef VARARRAY_SIZE
    00135     #define VARARRAY_SIZE 0
    00136   #endif
    00137 #endif /* __GNUC__ */
    00138
    00139 #if defined(__WATCOMC__)
    00140   #define NORETURN
    00141   #define FORCEINLINE inline
    00142   #define CDECL
    00143   #define GCC_PACK
    00144   #define WARN_FORMAT(string, args)
    00145   #include <malloc.h>
    00146 #endif /* __WATCOMC__ */
    00147
    00148 #if defined(__MINGW32__) || defined(__CYGWIN__)
    00149   #include <malloc.h> // alloca()
    00150 #endif
    00151
    00152 #if defined(WIN32)
    00153   #define WIN32_LEAN_AND_MEAN     // Exclude rarely-used stuff from Windows headers
    00154 #endif
    00155
    00156 /* Stuff for MSVC */
    00157 #if defined(_MSC_VER)
    00158   #pragma once
    00159   /* Define a win32 target platform, to override defaults of the SDK
    00160    * We need to define NTDDI version for Vista SDK, but win2k is minimum */
    00161   #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
    00162   #define _WIN32_WINNT 0x0500       // Windows 2000
    00163   #define _WIN32_WINDOWS 0x400      // Windows 95
    00164   #if !defined(WINCE)
    00165     #define WINVER 0x0400     // Windows NT 4.0 / Windows 95
    00166   #endif
    00167   #define _WIN32_IE_ 0x0401         // 4.01 (win98 and NT4SP5+)
    00168
    00169   #pragma warning(disable: 4244)  // 'conversion' conversion from 'type1' to 'type2', possible loss of data
    00170   #pragma warning(disable: 4761)  // integral size mismatch in argument : conversion supplied
    00171   #pragma warning(disable: 4200)  // nonstandard extension used : zero-sized array in struct/union
    00172
    00173   #if (_MSC_VER < 1400)                   // MSVC 2005 safety checks
    00174     #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
    00175   #endif /* (_MSC_VER < 1400) */
    00176   #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)
    00177   #pragma warning(disable: 4996)   // 'strdup' was declared deprecated
    00178   #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
    00179   #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
    00180   #pragma warning(disable: 6011)   // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
    00181   #pragma warning(disable: 6326)   // code analyzer: potential comparison of a constant with another constant
    00182   #pragma warning(disable: 6031)   // code analyzer: Return value ignored: 'ReadFile'
    00183   #pragma warning(disable: 6255)   // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
    00184   #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 ...
    00185   #define WARN_FORMAT(string, args)
    00186
    00187   #include <malloc.h> // alloca()
    00188   #define NORETURN __declspec(noreturn)
    00189   #define FORCEINLINE __forceinline
    00190   #define inline _inline
    00191
    00192   #if !defined(WINCE)
    00193     #define CDECL _cdecl
    00194   #endif
    00195
    00196   int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
    00197   #if defined(WINCE)
    00198     int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
    00199   #endif
    00200
    00201   #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
    00202     #if !defined(_W64)
    00203       #define _W64
    00204     #endif
    00205
    00206     typedef _W64 int INT_PTR, *PINT_PTR;
    00207     typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
    00208   #endif /* WIN32 && !_WIN64 && !WIN64 */
    00209
    00210   #if defined(_WIN64) || defined(WIN64)
    00211     #define fseek _fseeki64
    00212   #endif /* _WIN64 || WIN64 */
    00213
    00214
    00215   #define GCC_PACK
    00216
    00217   /* This is needed to zlib uses the stdcall calling convention on visual studio */
    00218   #if defined(WITH_ZLIB) || defined(WITH_PNG)
    00219     #if !defined(ZLIB_WINAPI)
    00220       #define ZLIB_WINAPI
    00221     #endif
    00222   #endif
    00223
    00224   #if defined(WINCE)
    00225     #define strcasecmp _stricmp
    00226     #define strncasecmp _strnicmp
    00227     #undef DEBUG
    00228   #else
    00229     #define strcasecmp stricmp
    00230     #define strncasecmp strnicmp
    00231   #endif
    00232
    00233   void SetExceptionString(const char *s, ...) WARN_FORMAT(1, 2);
    00234
    00235   #if defined(NDEBUG) && defined(WITH_ASSERT)
    00236     #undef assert
    00237     #define assert(expression) if (!(expression)) { SetExceptionString("Assertion failed at %s:%d: %s", __FILE__, __LINE__, #expression); *(byte*)0 = 0; }
    00238   #endif
    00239
    00240   /* MSVC doesn't have these :( */
    00241   #define S_ISDIR(mode) (mode & S_IFDIR)
    00242   #define S_ISREG(mode) (mode & S_IFREG)
    00243
    00244 #endif /* defined(_MSC_VER) */
    00245
    00246 #if defined(WINCE)
    00247   #define strdup _strdup
    00248 #endif /* WINCE */
    00249
    00250 /* NOTE: the string returned by these functions is only valid until the next
    00251  * call to the same function and is not thread- or reentrancy-safe */
    00252 #if !defined(STRGEN)
    00253   #if defined(WIN32) || defined(WIN64)
    00254     char *getcwd(char *buf, size_t size);
    00255     #include <tchar.h>
    00256     #include <io.h>
    00257
    00258     /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
    00259     #if !defined(WINCE)
    00260       #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
    00261       #define unlink(file) _tunlink(OTTD2FS(file))
    00262     #endif /* WINCE */
    00263
    00264     const char *FS2OTTD(const TCHAR *name);
    00265     const TCHAR *OTTD2FS(const char *name);
    00266   #else
    00267     #define fopen(file, mode) fopen(OTTD2FS(file), mode)
    00268     const char *FS2OTTD(const char *name);
    00269     const char *OTTD2FS(const char *name);
    00270   #endif /* WIN32 */
    00271 #endif /* STRGEN */
    00272
    00273 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
    00274   #define PATHSEP "\\"
    00275   #define PATHSEPCHAR '\\'
    00276 #else
    00277   #define PATHSEP "/"
    00278   #define PATHSEPCHAR '/'
    00279 #endif
    00280
    00281 /* MSVCRT of course has to have a different syntax for long long *sigh* */
    00282 #if defined(_MSC_VER) || defined(__MINGW32__)
    00283   #define OTTD_PRINTF64 "%I64d"
    00284   #define PRINTF_SIZE "%Iu"
    00285 #else
    00286   #define OTTD_PRINTF64 "%lld"
    00287   #define PRINTF_SIZE "%zu"
    00288 #endif
    00289
    00290 typedef unsigned char byte;
    00291
    00292 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
    00293 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
    00294   typedef unsigned int uint;
    00295 #endif
    00296
    00297 #if !defined(__BEOS__) && !defined(__NDS__) /* Already defined on BEOS and NDS */
    00298   typedef unsigned char    uint8;
    00299   typedef   signed char     int8;
    00300   typedef unsigned short   uint16;
    00301   typedef   signed short    int16;
    00302   typedef unsigned int     uint32;
    00303   typedef   signed int      int32;
    00304   typedef unsigned __int64 uint64;
    00305   typedef   signed __int64  int64;
    00306 #endif /* !__BEOS__ && !__NDS__ */
    00307
    00308 #if !defined(WITH_PERSONAL_DIR)
    00309   #define PERSONAL_DIR ""
    00310 #endif
    00311
    00312 /* Compile time assertions */
    00313 #if defined(__OS2__)
    00314   #define assert_compile(expr)
    00315 #else
    00316   #define assert_compile(expr) extern const int __ct_assert__[1 - 2 * !(expr)]
    00317 #endif /* __OS2__ */
    00318
    00319 /* Check if the types have the bitsizes like we are using them */
    00320 assert_compile(sizeof(uint64) == 8);
    00321 assert_compile(sizeof(uint32) == 4);
    00322 assert_compile(sizeof(uint16) == 2);
    00323 assert_compile(sizeof(uint8)  == 1);
    00324
    00333 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
    00334
    00341 #define endof(x) (&x[lengthof(x)])
    00342
    00349 #define lastof(x) (&x[lengthof(x) - 1])
    00350
    00351 #define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
    00352 #if !defined(offsetof)
    00353   #define offsetof(s, m) cpp_offsetof(s, m)
    00354 #endif /* offsetof */
    00355
    00356
    00357 /* take care of some name clashes on MacOS */
    00358 #if defined(__APPLE__)
    00359   #define GetString OTTD_GetString
    00360   #define DrawString OTTD_DrawString
    00361   #define CloseConnection OTTD_CloseConnection
    00362 #endif /* __APPLE__ */
    00363
    00364 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
    00365 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
    00366 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
    00367
    00368 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
    00369   /* MorphOS and NDS don't have C++ conformant _stricmp... */
    00370   #define _stricmp stricmp
    00371 #elif defined(OPENBSD)
    00372   /* OpenBSD uses strcasecmp(3) */
    00373   #define _stricmp strcasecmp
    00374 #endif
    00375
    00376 #if !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) && !defined(__DJGPP__)
    00377   /* NDS, MorphOS & OpenBSD don't know wchars, the rest does :( */
    00378   #define HAS_WCHAR
    00379 #endif /* !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) */
    00380
    00381 #if !defined(MAX_PATH)
    00382   #define MAX_PATH 260
    00383 #endif
    00384
    00389 #define MAX_UVALUE(type) ((type)~(type)0)
    00390
    00391 #endif /* STDAFX_H */