# stdafx.h

Go to the documentation of this file.

    00001 /* $Id: stdafx.h 16273 2009-05-10 21:33:55Z 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: 4996)   // 'strdup' was declared deprecated
    00177   #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
    00178   #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
    00179   #pragma warning(disable: 6011)   // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
    00180   #pragma warning(disable: 6326)   // code analyzer: potential comparison of a constant with another constant
    00181   #pragma warning(disable: 6031)   // code analyzer: Return value ignored: 'ReadFile'
    00182   #pragma warning(disable: 6255)   // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
    00183   #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 ...
    00184   #define WARN_FORMAT(string, args)
    00185
    00186   #include <malloc.h> // alloca()
    00187   #define NORETURN __declspec(noreturn)
    00188   #define FORCEINLINE __forceinline
    00189   #define inline _inline
    00190
    00191   #if !defined(WINCE)
    00192     #define CDECL _cdecl
    00193   #endif
    00194
    00195   int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
    00196   #if defined(WINCE)
    00197     int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
    00198   #endif
    00199
    00200   #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
    00201     #if !defined(_W64)
    00202       #define _W64
    00203     #endif
    00204
    00205     typedef _W64 int INT_PTR, *PINT_PTR;
    00206     typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
    00207   #endif /* WIN32 && !_WIN64 && !WIN64 */
    00208
    00209   #if defined(_WIN64) || defined(WIN64)
    00210     #define fseek _fseeki64
    00211   #endif /* _WIN64 || WIN64 */
    00212
    00213
    00214   #define GCC_PACK
    00215
    00216   /* This is needed to zlib uses the stdcall calling convention on visual studio */
    00217   #if defined(WITH_ZLIB) || defined(WITH_PNG)
    00218     #if !defined(ZLIB_WINAPI)
    00219       #define ZLIB_WINAPI
    00220     #endif
    00221   #endif
    00222
    00223   #if defined(WINCE)
    00224     #define strcasecmp _stricmp
    00225     #define strncasecmp _strnicmp
    00226     #undef DEBUG
    00227   #else
    00228     #define strcasecmp stricmp
    00229     #define strncasecmp strnicmp
    00230   #endif
    00231
    00232   void SetExceptionString(const char *s, ...) WARN_FORMAT(1, 2);
    00233
    00234   #if defined(NDEBUG) && defined(WITH_ASSERT)
    00235     #undef assert
    00236     #define assert(expression) if (!(expression)) { SetExceptionString("Assertion failed at %s:%d: %s", __FILE__, __LINE__, #expression); *(byte*)0 = 0; }
    00237   #endif
    00238
    00239   /* MSVC doesn't have these :( */
    00240   #define S_ISDIR(mode) (mode & S_IFDIR)
    00241   #define S_ISREG(mode) (mode & S_IFREG)
    00242
    00243 #endif /* defined(_MSC_VER) */
    00244
    00245 #if defined(WINCE)
    00246   #define strdup _strdup
    00247 #endif /* WINCE */
    00248
    00249 /* NOTE: the string returned by these functions is only valid until the next
    00250  * call to the same function and is not thread- or reentrancy-safe */
    00251 #if !defined(STRGEN)
    00252   #if defined(WIN32) || defined(WIN64)
    00253     char *getcwd(char *buf, size_t size);
    00254     #include <tchar.h>
    00255     #include <io.h>
    00256
    00257     /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
    00258     #if !defined(WINCE)
    00259       #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
    00260       #define unlink(file) _tunlink(OTTD2FS(file))
    00261     #endif /* WINCE */
    00262
    00263     const char *FS2OTTD(const TCHAR *name);
    00264     const TCHAR *OTTD2FS(const char *name);
    00265   #else
    00266     #define fopen(file, mode) fopen(OTTD2FS(file), mode)
    00267     const char *FS2OTTD(const char *name);
    00268     const char *OTTD2FS(const char *name);
    00269   #endif /* WIN32 */
    00270 #endif /* STRGEN */
    00271
    00272 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
    00273   #define PATHSEP "\\"
    00274   #define PATHSEPCHAR '\\'
    00275 #else
    00276   #define PATHSEP "/"
    00277   #define PATHSEPCHAR '/'
    00278 #endif
    00279
    00280 /* MSVCRT of course has to have a different syntax for long long *sigh* */
    00281 #if defined(_MSC_VER) || defined(__MINGW32__)
    00282   #define OTTD_PRINTF64 "%I64d"
    00283   #define PRINTF_SIZE "%Iu"
    00284 #else
    00285   #define OTTD_PRINTF64 "%lld"
    00286   #define PRINTF_SIZE "%zu"
    00287 #endif
    00288
    00289 typedef unsigned char byte;
    00290
    00291 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
    00292 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
    00293   typedef unsigned int uint;
    00294 #endif
    00295
    00296 #if !defined(__BEOS__) && !defined(__NDS__) /* Already defined on BEOS and NDS */
    00297   typedef unsigned char    uint8;
    00298   typedef   signed char     int8;
    00299   typedef unsigned short   uint16;
    00300   typedef   signed short    int16;
    00301   typedef unsigned int     uint32;
    00302   typedef   signed int      int32;
    00303   typedef unsigned __int64 uint64;
    00304   typedef   signed __int64  int64;
    00305 #endif /* !__BEOS__ && !__NDS__ */
    00306
    00307 #if !defined(WITH_PERSONAL_DIR)
    00308   #define PERSONAL_DIR ""
    00309 #endif
    00310
    00311 /* Compile time assertions */
    00312 #if defined(__OS2__)
    00313   #define assert_compile(expr)
    00314 #else
    00315   #define assert_compile(expr) extern const int __ct_assert__[1 - 2 * !(expr)]
    00316 #endif /* __OS2__ */
    00317
    00318 /* Check if the types have the bitsizes like we are using them */
    00319 assert_compile(sizeof(uint64) == 8);
    00320 assert_compile(sizeof(uint32) == 4);
    00321 assert_compile(sizeof(uint16) == 2);
    00322 assert_compile(sizeof(uint8)  == 1);
    00323
    00332 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
    00333
    00340 #define endof(x) (&x[lengthof(x)])
    00341
    00348 #define lastof(x) (&x[lengthof(x) - 1])
    00349
    00350 #define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
    00351 #if !defined(offsetof)
    00352   #define offsetof(s, m) cpp_offsetof(s, m)
    00353 #endif /* offsetof */
    00354
    00355
    00356 /* take care of some name clashes on MacOS */
    00357 #if defined(__APPLE__)
    00358   #define GetString OTTD_GetString
    00359   #define DrawString OTTD_DrawString
    00360   #define CloseConnection OTTD_CloseConnection
    00361 #endif /* __APPLE__ */
    00362
    00363 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
    00364 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
    00365 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
    00366
    00367 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
    00368   /* MorphOS and NDS don't have C++ conformant _stricmp... */
    00369   #define _stricmp stricmp
    00370 #elif defined(OPENBSD)
    00371   /* OpenBSD uses strcasecmp(3) */
    00372   #define _stricmp strcasecmp
    00373 #endif
    00374
    00375 #if !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) && !defined(__DJGPP__)
    00376   /* NDS, MorphOS & OpenBSD don't know wchars, the rest does :( */
    00377   #define HAS_WCHAR
    00378 #endif /* !defined(MORPHOS) && !defined(OPENBSD) && !defined(__NDS__) */
    00379
    00380 #if !defined(MAX_PATH)
    00381   #define MAX_PATH 260
    00382 #endif
    00383
    00388 #define MAX_UVALUE(type) ((type)~(type)0)
    00389
    00390 #endif /* STDAFX_H */