4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file stdafx.h Definition of base types and functions in a cross-platform compatible way. */
15 #if defined(__APPLE__)
16 #include "os/macosx/osx_stdafx.h"
17 #endif /* __APPLE__ */
19 #if defined(__BEOS__) || defined(__HAIKU__)
20 #include <SupportDefs.h>
24 #elif defined(__NDS__)
25 #include <nds/jtypes.h>
29 /* It seems that we need to include stdint.h before anything else
30 * We need INT64_MAX, which for most systems comes from stdint.h. However, MSVC
31 * does not have stdint.h and apparently neither does MorphOS, so define
32 * INT64_MAX for them ourselves. */
33 #if defined(__APPLE__)
34 /* Already done in osx_stdafx.h */
35 #elif !defined(_MSC_VER) && !defined( __MORPHOS__) && !defined(_STDINT_H_)
37 /* SunOS/Solaris does not have stdint.h, but inttypes.h defines everything
38 * stdint.h defines and we need. */
41 #define __STDC_LIMIT_MACROS
45 #define UINT64_MAX (18446744073709551615ULL)
46 #define INT64_MAX (9223372036854775807LL)
47 #define INT64_MIN (-INT64_MAX - 1)
48 #define UINT32_MAX (4294967295U)
49 #define INT32_MAX (2147483647)
50 #define INT32_MIN (-INT32_MAX - 1)
51 #define UINT16_MAX (65535U)
52 #define INT16_MAX (32767)
53 #define INT16_MIN (-INT16_MAX - 1)
54 #define UINT8_MAX (255)
55 #define INT8_MAX (127)
56 #define INT8_MIN (-INT8_MAX - 1)
67 #define SIZE_MAX ((size_t)-1)
70 #if defined(UNIX) || defined(__MINGW32__)
71 #include <sys/types.h>
76 #define strcasecmp stricmp
82 #include <pspthreadman.h>
85 #if defined(SUNOS) || defined(HPUX)
89 #if defined(__MORPHOS__)
90 /* MorphOS defines certain Amiga defines per default, we undefine them
91 * here to make the rest of source less messy and more clear what is
92 * required for morphos and what for AmigaOS */
96 #if defined(__amigaos__)
99 #if defined(__AMIGA__)
108 /* Act like we already included this file, as it somehow gives linkage problems
109 * (mismatch linkage of C++ and C between this include and unistd.h). */
110 #define CLIB_USERGROUP_PROTOS_H
111 #endif /* __MORPHOS__ */
114 /* PSP can only have 10 file-descriptors open at any given time, but this
115 * switch only limits reads via the Fio system. So keep 2 fds free for things
116 * like saving a game. */
117 #define LIMITED_FDS 8
118 #define printf pspDebugScreenPrintf
122 #if defined(__GNUC__)
123 #define NORETURN __attribute__ ((noreturn))
124 #define FORCEINLINE inline
126 #define __int64 long long
127 #define GCC_PACK __attribute__((packed))
128 /* Warn about functions using 'printf' format syntax. First argument determines which parameter
129 * is the format string, second argument is start of values passed to printf. */
130 #define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args)))
131 #endif /* __GNUC__ */
133 #if defined(__WATCOMC__)
135 #define FORCEINLINE inline
138 #define WARN_FORMAT(string, args)
140 #endif /* __WATCOMC__ */
142 #if defined(__MINGW32__) || defined(__CYGWIN__)
143 #include <malloc.h> // alloca()
147 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
151 #if defined(_MSC_VER)
153 /* Define a win32 target platform, to override defaults of the SDK
154 * We need to define NTDDI version for Vista SDK, but win2k is minimum */
155 #define NTDDI_VERSION NTDDI_WIN2K // Windows 2000
156 #define _WIN32_WINNT 0x0500 // Windows 2000
157 #define _WIN32_WINDOWS 0x400 // Windows 95
159 #define WINVER 0x0400 // Windows NT 4.0 / Windows 95
161 #define _WIN32_IE_ 0x0401 // 4.01 (win98 and NT4SP5+)
163 #pragma warning(disable: 4244) // 'conversion' conversion from 'type1' to 'type2', possible loss of data
164 #pragma warning(disable: 4761) // integral size mismatch in argument : conversion supplied
165 #pragma warning(disable: 4200) // nonstandard extension used : zero-sized array in struct/union
167 #if (_MSC_VER < 1400) // MSVC 2005 safety checks
168 #error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
169 #endif /* (_MSC_VER < 1400) */
170 #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)
171 #pragma warning(disable: 4996) // 'function': was declared deprecated
172 #define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
173 #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
174 #pragma warning(disable: 6011) // code analyzer: Dereferencing NULL pointer 'pfGetAddrInfo': Lines: 995, 996, 998, 999, 1001
175 #pragma warning(disable: 6326) // code analyzer: potential comparison of a constant with another constant
176 #pragma warning(disable: 6031) // code analyzer: Return value ignored: 'ReadFile'
177 #pragma warning(disable: 6255) // code analyzer: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
178 #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 ...
179 #define WARN_FORMAT(string, args)
181 #include <malloc.h> // alloca()
182 #define NORETURN __declspec(noreturn)
183 #define FORCEINLINE __forceinline
184 #define inline _inline
192 int CDECL
snprintf(char *str
, size_t size
, const char *format
, ...) WARN_FORMAT(3, 4);
194 int CDECL
vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
197 #if defined(WIN32) && !defined(_WIN64) && !defined(WIN64)
202 typedef _W64
int INT_PTR
, *PINT_PTR
;
203 typedef _W64
unsigned int UINT_PTR
, *PUINT_PTR
;
204 #endif /* WIN32 && !_WIN64 && !WIN64 */
206 #if defined(_WIN64) || defined(WIN64)
207 #define fseek _fseeki64
208 #endif /* _WIN64 || WIN64 */
210 /* This is needed to zlib uses the stdcall calling convention on visual studio */
211 #if defined(WITH_ZLIB) || defined(WITH_PNG)
212 #if !defined(ZLIB_WINAPI)
218 #define strcasecmp _stricmp
219 #define strncasecmp _strnicmp
222 #define strcasecmp stricmp
223 #define strncasecmp strnicmp
226 /* MSVC doesn't have these :( */
227 #define S_ISDIR(mode) (mode & S_IFDIR)
228 #define S_ISREG(mode) (mode & S_IFREG)
230 #endif /* defined(_MSC_VER) */
233 /* The DOS port does not have all signals/signal functions. */
234 #define strsignal(sig) ""
235 /* Use 'no floating point' for bus errors; SIGBUS does not exist
236 * for DOS, SIGNOFP for other platforms. So it's fairly safe
237 * to interchange those. */
238 #define SIGBUS SIGNOFP
242 #define strdup _strdup
245 /* NOTE: the string returned by these functions is only valid until the next
246 * call to the same function and is not thread- or reentrancy-safe */
247 #if !defined(STRGEN) && !defined(SETTINGSGEN)
248 #if defined(WIN32) || defined(WIN64)
249 char *getcwd(char *buf
, size_t size
);
253 /* XXX - WinCE without MSVCRT doesn't support wfopen, so it seems */
255 namespace std
{ using ::_tfopen
; }
256 #define fopen(file, mode) _tfopen(OTTD2FS(file), _T(mode))
257 #define unlink(file) _tunlink(OTTD2FS(file))
260 const char *FS2OTTD(const TCHAR
*name
);
261 const TCHAR
*OTTD2FS(const char *name
);
262 #define SQ2OTTD(name) FS2OTTD(name)
263 #define OTTD2SQ(name) OTTD2FS(name)
265 #define fopen(file, mode) fopen(OTTD2FS(file), mode)
266 const char *FS2OTTD(const char *name
);
267 const char *OTTD2FS(const char *name
);
268 #define SQ2OTTD(name) (name)
269 #define OTTD2SQ(name) (name)
271 #endif /* STRGEN || SETTINGSGEN */
273 #if defined(WIN32) || defined(WIN64) || defined(__OS2__) && !defined(__INNOTEK_LIBC__)
275 #define PATHSEPCHAR '\\'
278 #define PATHSEPCHAR '/'
281 /* MSVCRT of course has to have a different syntax for long long *sigh* */
282 #if defined(_MSC_VER) || defined(__MINGW32__)
283 #define OTTD_PRINTF64 "%I64d"
284 #define OTTD_PRINTFHEX64 "%I64x"
285 #define PRINTF_SIZE "%Iu"
287 #define OTTD_PRINTF64 "%lld"
288 #define OTTD_PRINTFHEX64 "%llx"
289 #define PRINTF_SIZE "%zu"
292 typedef unsigned char byte
;
294 /* This is already defined in unix, but not in QNX Neutrino (6.x)*/
295 #if (!defined(UNIX) && !defined(__CYGWIN__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__MORPHOS__)) || defined(__QNXNTO__)
296 typedef unsigned int uint
;
299 #if defined(TROUBLED_INTS)
300 /* NDS'/BeOS'/Haiku's types for uint32/int32 are based on longs, which causes
301 * trouble all over the place in OpenTTD. */
302 #define uint32 uint32_ugly_hack
303 #define int32 int32_ugly_hack
304 typedef unsigned int uint32_ugly_hack
;
305 typedef signed int int32_ugly_hack
;
307 typedef unsigned char uint8
;
308 typedef signed char int8
;
309 typedef unsigned short uint16
;
310 typedef signed short int16
;
311 typedef unsigned int uint32
;
312 typedef signed int int32
;
313 typedef unsigned __int64 uint64
;
314 typedef signed __int64 int64
;
315 #endif /* !TROUBLED_INTS */
317 #if !defined(WITH_PERSONAL_DIR)
318 #define PERSONAL_DIR ""
321 /* Compile time assertions. Prefer c++0x static_assert().
322 * Older compilers cannot evaluate some expressions at compile time,
323 * typically when templates are involved, try assert_tcompile() in those cases. */
324 #if defined(__STDCXX_VERSION__) || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(__GXX_EXPERIMENTAL_CPP0X__) || defined(static_assert)
325 /* __STDCXX_VERSION__ is c++0x feature macro, __GXX_EXPERIMENTAL_CXX0X__ is used by gcc, __GXX_EXPERIMENTAL_CPP0X__ by icc */
326 #define assert_compile(expr) static_assert(expr, #expr )
327 #define assert_tcompile(expr) assert_compile(expr)
328 #elif defined(__OS2__)
329 /* Disabled for OS/2 */
330 #define assert_compile(expr)
331 #define assert_tcompile(expr) assert_compile(expr)
333 #define assert_compile(expr) typedef int __ct_assert__[1 - 2 * !(expr)]
334 #define assert_tcompile(expr) assert(expr)
337 /* Check if the types have the bitsizes like we are using them */
338 assert_compile(sizeof(uint64
) == 8);
339 assert_compile(sizeof(uint32
) == 4);
340 assert_compile(sizeof(uint16
) == 2);
341 assert_compile(sizeof(uint8
) == 1);
342 assert_compile(SIZE_MAX
>= UINT32_MAX
);
345 #define M_PI_2 1.57079632679489661923
346 #define M_PI 3.14159265358979323846
350 * Return the length of an fixed size array.
351 * Unlike sizeof this function returns the number of elements
354 * @param x The pointer to the first element of the array
355 * @return The number of elements
357 #define lengthof(x) (sizeof(x) / sizeof(x[0]))
360 * Get the end element of an fixed size array.
362 * @param x The pointer to the first element of the array
363 * @return The pointer past to the last element of the array
365 #define endof(x) (&x[lengthof(x)])
368 * Get the last element of an fixed size array.
370 * @param x The pointer to the first element of the array
371 * @return The pointer to the last element of the array
373 #define lastof(x) (&x[lengthof(x) - 1])
375 #define cpp_offsetof(s, m) (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
376 #if !defined(offsetof)
377 #define offsetof(s, m) cpp_offsetof(s, m)
378 #endif /* offsetof */
381 * Gets the size of a variable within a class.
382 * @param base The class the variable is in.
383 * @param variable The variable to get the size of.
384 * @return the size of the variable
386 #define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
389 * Gets the length of an array variable within a class.
390 * @param base The class the variable is in.
391 * @param variable The array variable to get the size of.
392 * @return the length of the array
394 #define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
397 /* take care of some name clashes on MacOS */
398 #if defined(__APPLE__)
399 #define GetString OTTD_GetString
400 #define DrawString OTTD_DrawString
401 #define CloseConnection OTTD_CloseConnection
402 #endif /* __APPLE__ */
404 void NORETURN CDECL
usererror(const char *str
, ...) WARN_FORMAT(1, 2);
405 void NORETURN CDECL
error(const char *str
, ...) WARN_FORMAT(1, 2);
406 #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
408 /* For non-debug builds with assertions enabled use the special assertion handler:
409 * - For MSVC: NDEBUG is set for all release builds and WITH_ASSERT overrides the disabling of asserts.
410 * - For non MSVC: NDEBUG is set when assertions are disables, _DEBUG is set for non-release builds.
412 #if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
414 #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
417 /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
418 #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
422 #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
423 /* MorphOS and NDS don't have C++ conformant _stricmp... */
424 #define _stricmp stricmp
425 #elif defined(OPENBSD)
426 /* OpenBSD uses strcasecmp(3) */
427 #define _stricmp strcasecmp
430 #if defined(MAX_PATH)
431 /* It's already defined, no need to override */
432 #elif defined(PATH_MAX) && PATH_MAX > 0
433 /* Use the value from PATH_MAX, if it exists */
434 #define MAX_PATH PATH_MAX
436 /* If all else fails, hardcode something :( */
441 * The largest value that can be entered in a variable
442 * @param type the type of the variable
444 #define MAX_UVALUE(type) ((type)~(type)0)
446 #endif /* STDAFX_H */