00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef STDAFX_H
00013 #define STDAFX_H
00014
00015 #if defined(__APPLE__)
00016 #include "os/macosx/osx_stdafx.h"
00017 #endif
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
00030
00031
00032
00033 #if !defined(__APPLE__) && (!defined(_MSC_VER) || _MSC_VER >= 1600) && !defined(__MORPHOS__)
00034 #if defined(SUNOS)
00035
00036
00037 #include <inttypes.h>
00038 #else
00039 #define __STDC_LIMIT_MACROS
00040 #include <stdint.h>
00041 #endif
00042 #endif
00043
00044
00045 #if !defined(UINT64_MAX)
00046 #define UINT64_MAX (18446744073709551615ULL)
00047 #endif
00048 #if !defined(INT64_MAX)
00049 #define INT64_MAX (9223372036854775807LL)
00050 #endif
00051 #if !defined(INT64_MIN)
00052 #define INT64_MIN (-INT64_MAX - 1)
00053 #endif
00054 #if !defined(UINT32_MAX)
00055 #define UINT32_MAX (4294967295U)
00056 #endif
00057 #if !defined(INT32_MAX)
00058 #define INT32_MAX (2147483647)
00059 #endif
00060 #if !defined(INT32_MIN)
00061 #define INT32_MIN (-INT32_MAX - 1)
00062 #endif
00063 #if !defined(UINT16_MAX)
00064 #define UINT16_MAX (65535U)
00065 #endif
00066 #if !defined(INT16_MAX)
00067 #define INT16_MAX (32767)
00068 #endif
00069 #if !defined(INT16_MIN)
00070 #define INT16_MIN (-INT16_MAX - 1)
00071 #endif
00072 #if !defined(UINT8_MAX)
00073 #define UINT8_MAX (255)
00074 #endif
00075 #if !defined(INT8_MAX)
00076 #define INT8_MAX (127)
00077 #endif
00078 #if !defined(INT8_MIN)
00079 #define INT8_MIN (-INT8_MAX - 1)
00080 #endif
00081
00082 #include <cstdio>
00083 #include <cstddef>
00084 #include <cstring>
00085 #include <cstdlib>
00086 #include <climits>
00087 #include <cassert>
00088
00089 #ifndef SIZE_MAX
00090 #define SIZE_MAX ((size_t)-1)
00091 #endif
00092
00093 #if defined(UNIX) || defined(__MINGW32__)
00094 #include <sys/types.h>
00095 #endif
00096
00097 #if defined(__OS2__)
00098 #include <types.h>
00099 #define strcasecmp stricmp
00100 #endif
00101
00102 #if defined(PSP)
00103 #include <psptypes.h>
00104 #include <pspdebug.h>
00105 #include <pspthreadman.h>
00106 #endif
00107
00108 #if defined(SUNOS) || defined(HPUX)
00109 #include <alloca.h>
00110 #endif
00111
00112 #if defined(__MORPHOS__)
00113
00114
00115
00116 #if defined(amigaos)
00117 #undef amigaos
00118 #endif
00119 #if defined(__amigaos__)
00120 #undef __amigaos__
00121 # endif
00122 #if defined(__AMIGA__)
00123 #undef __AMIGA__
00124 #endif
00125 #if defined(AMIGA)
00126 #undef AMIGA
00127 #endif
00128 #if defined(amiga)
00129 #undef amiga
00130 #endif
00131
00132
00133 #define CLIB_USERGROUP_PROTOS_H
00134 #endif
00135
00136 #if defined(PSP)
00137
00138
00139
00140 #define LIMITED_FDS 8
00141 #define printf pspDebugScreenPrintf
00142 #endif
00143
00144
00145 #if defined(__GNUC__)
00146 #define NORETURN __attribute__ ((noreturn))
00147 #define CDECL
00148 #define __int64 long long
00149 #define GCC_PACK __attribute__((packed))
00150
00151
00152 #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
00153 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
00154 #define FINAL final
00155 #else
00156 #define FINAL
00157 #endif
00158 #endif
00159
00160 #if defined(__WATCOMC__)
00161 #define NORETURN
00162 #define CDECL
00163 #define GCC_PACK
00164 #define WARN_FORMAT(string, args)
00165 #define FINAL
00166 #include <malloc.h>
00167 #endif
00168
00169 #if defined(__MINGW32__) || defined(__CYGWIN__)
00170 #include <malloc.h>
00171 #endif
00172
00173 #if defined(WIN32)
00174 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
00175 #endif
00176
00177
00178 #if defined(_MSC_VER)
00179 #pragma once
00180 #ifdef _WIN64
00181
00182 #define NTDDI_VERSION NTDDI_WINXP // Windows XP
00183 #define _WIN32_WINNT 0x501 // Windows XP
00184 #define _WIN32_WINDOWS 0x501 // Windows XP
00185 #define WINVER 0x0501 // Windows XP
00186 #define _WIN32_IE_ 0x0600 // 6.0 (XP+)
00187 #else
00188
00189
00190 #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
00191 #define _WIN32_WINNT 0x0500 // Windows 2000
00192 #define _WIN32_WINDOWS 0x400 // Windows 95
00193 #if !defined(WINCE)
00194 #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
00195 #endif
00196 #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
00197 #endif
00198 #define NOMINMAX // Disable min/max macros in windows.h.
00199
00200 #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
00201 #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
00202 #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
00203 #pragma warning(disable: 4355) // 'this' : used in base member initializer list
00204
00205 #if (_MSC_VER < 1400) // MSVC 2005 safety checks
00206 #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
00207 #endif
00208 #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)
00209 #pragma warning(disable: 4996) // 'function': was declared deprecated
00210 #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
00211 #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
00212 #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
00213 #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
00214 #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
00215 #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
00216 #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 ...
00217
00218 #include <malloc.h>
00219 #define NORETURN __declspec(noreturn)
00220 #define inline __forceinline
00221
00222 #if !defined(WINCE)
00223 #define CDECL _cdecl
00224 #endif
00225
00226 #define GCC_PACK
00227 #define WARN_FORMAT(string, args)
00228 #define FINAL sealed
00229
00230 int CDECL snprintf(char *str, size_t size, const char *format, ...) WARN_FORMAT(3, 4);
00231 #if defined(WINCE)
00232 int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap);
00233 #endif
00234
00235 #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
00236 #if !defined(_W64)
00237 #define _W64
00238 #endif
00239
00240 typedef _W64 int INT_PTR, *PINT_PTR;
00241 typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
00242 #endif
00243
00244 #if defined(_WIN64) || defined(WIN64)
00245 #define fseek _fseeki64
00246 #endif
00247
00248
00249 #if defined(WITH_ZLIB) || defined(WITH_PNG)
00250 #if !defined(ZLIB_WINAPI)
00251 #define ZLIB_WINAPI
00252 #endif
00253 #endif
00254
00255 #if defined(WINCE)
00256 #define strcasecmp _stricmp
00257 #define strncasecmp _strnicmp
00258 #undef DEBUG
00259 #else
00260 #define strcasecmp stricmp
00261 #define strncasecmp strnicmp
00262 #endif
00263
00264
00265 #define S_ISDIR(mode) (mode & S_IFDIR)
00266 #define S_ISREG(mode) (mode & S_IFREG)
00267
00268 #endif
00269
00270 #if defined(DOS)
00271
00272 #define strsignal(sig) ""
00273
00274
00275
00276 #define SIGBUS SIGNOFP
00277 #endif
00278
00279 #if defined(WINCE)
00280 #define strdup _strdup
00281 #endif
00282
00283
00284
00285 #if !defined(STRGEN) && !defined(SETTINGSGEN)
00286 #if defined(WIN32) || defined(WIN64)
00287 char *getcwd(char *buf, size_t size);
00288 #include <tchar.h>
00289 #include <io.h>
00290
00291
00292 #if !defined(WINCE)
00293 namespace std { using ::_tfopen; }
00294 #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
00295 #define unlink(file) _tunlink(OTTD2FS(file))
00296 #endif
00297
00298 const char *FS2OTTD(const TCHAR *name);
00299 const TCHAR *OTTD2FS(const char *name, bool console_cp = false);
00300 #define SQ2OTTD(name) FS2OTTD(name)
00301 #define OTTD2SQ(name) OTTD2FS(name)
00302 #else
00303 #define fopen(file, mode) fopen(OTTD2FS(file), mode)
00304 const char *FS2OTTD(const char *name);
00305 const char *OTTD2FS(const char *name);
00306 #define SQ2OTTD(name) (name)
00307 #define OTTD2SQ(name) (name)
00308 #endif
00309 #endif
00310
00311 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
00312 #define PATHSEP "\\"
00313 #define PATHSEPCHAR '\\'
00314 #else
00315 #define PATHSEP "/"
00316 #define PATHSEPCHAR '/'
00317 #endif
00318
00319
00320 #if defined(_MSC_VER) || defined(__MINGW32__)
00321 #define OTTD_PRINTF64 "%I64d"
00322 #define OTTD_PRINTFHEX64 "%I64x"
00323 #define PRINTF_SIZE "%Iu"
00324 #else
00325 #define OTTD_PRINTF64 "%lld"
00326 #define OTTD_PRINTFHEX64 "%llx"
00327 #define PRINTF_SIZE "%zu"
00328 #endif
00329
00330 typedef unsigned char byte;
00331
00332
00333 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
00334 typedef unsigned int uint;
00335 #endif
00336
00337 #if defined(TROUBLED_INTS)
00338
00339
00340 #define uint32 uint32_ugly_hack
00341 #define int32 int32_ugly_hack
00342 typedef unsigned int uint32_ugly_hack;
00343 typedef signed int int32_ugly_hack;
00344 #else
00345 typedef unsigned char uint8;
00346 typedef signed char int8;
00347 typedef unsigned short uint16;
00348 typedef signed short int16;
00349 typedef unsigned int uint32;
00350 typedef signed int int32;
00351 typedef unsigned __int64 uint64;
00352 typedef signed __int64 int64;
00353 #endif
00354
00355 #if !defined(WITH_PERSONAL_DIR)
00356 #define PERSONAL_DIR ""
00357 #endif
00358
00359
00360
00361
00362 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
00363
00364 #define assert_compile(expr) static_assert(expr, #expr )
00365 #define assert_tcompile(expr) assert_compile(expr)
00366 #elif defined(__OS2__)
00367
00368 #define assert_compile(expr)
00369 #define assert_tcompile(expr) assert_compile(expr)
00370 #else
00371 #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
00372 #define assert_tcompile(expr) assert(expr)
00373 #endif
00374
00375
00376 assert_compile(sizeof(uint64) == 8);
00377 assert_compile(sizeof(uint32) == 4);
00378 assert_compile(sizeof(uint16) == 2);
00379 assert_compile(sizeof(uint8) == 1);
00380 assert_compile(SIZE_MAX >= UINT32_MAX);
00381
00382 #ifndef M_PI_2
00383 #define M_PI_2 1.57079632679489661923
00384 #define M_PI 3.14159265358979323846
00385 #endif
00386
00395 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
00396
00403 #define endof(x) (&x[lengthof(x)])
00404
00411 #define lastof(x) (&x[lengthof(x) - 1])
00412
00413 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
00414 #if !defined(offsetof)
00415 #define offsetof(s, m) cpp_offsetof(s, m)
00416 #endif
00417
00424 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
00425
00432 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
00433
00434
00435
00436 #if defined(__APPLE__)
00437 #define GetString OTTD_GetString
00438 #define DrawString OTTD_DrawString
00439 #define CloseConnection OTTD_CloseConnection
00440 #endif
00441
00442 void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2);
00443 void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
00444 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
00445
00446
00447
00448
00449
00450 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
00451 #undef assert
00452 #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
00453 #endif
00454
00455
00456 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
00457 #define OTTD_ASSERT
00458 #endif
00459
00460 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
00461
00462 #define _stricmp stricmp
00463 #elif defined(OPENBSD)
00464
00465 #define _stricmp strcasecmp
00466 #endif
00467
00468 #if defined(MAX_PATH)
00469
00470 #elif defined(PATH_MAX) && PATH_MAX > 0
00471
00472 #define MAX_PATH PATH_MAX
00473 #else
00474
00475 #define MAX_PATH 260
00476 #endif
00477
00482 static inline void free(const void *ptr)
00483 {
00484 free(const_cast<void *>(ptr));
00485 }
00486
00491 #define MAX_UVALUE(type) ((type)~(type)0)
00492
00493 #endif