SDL_stdinc.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_stdinc.h
  20. *
  21. * This is a general header that includes C language support.
  22. */
  23. #ifndef SDL_stdinc_h_
  24. #define SDL_stdinc_h_
  25. #include <SDL3/SDL_platform_defines.h>
  26. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  27. #include <inttypes.h>
  28. #endif
  29. #include <stdarg.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <wchar.h>
  33. #ifndef SDL_DISABLE_ALLOCA
  34. # ifndef alloca
  35. # ifdef HAVE_ALLOCA_H
  36. # include <alloca.h>
  37. # elif defined(SDL_PLATFORM_NETBSD)
  38. # if defined(__STRICT_ANSI__)
  39. # define SDL_DISABLE_ALLOCA
  40. # else
  41. # include <stdlib.h>
  42. # endif
  43. # elif defined(__GNUC__)
  44. # define alloca __builtin_alloca
  45. # elif defined(_MSC_VER)
  46. # include <malloc.h>
  47. # define alloca _alloca
  48. # elif defined(__WATCOMC__)
  49. # include <malloc.h>
  50. # elif defined(__BORLANDC__)
  51. # include <malloc.h>
  52. # elif defined(__DMC__)
  53. # include <stdlib.h>
  54. # elif defined(SDL_PLATFORM_AIX)
  55. # pragma alloca
  56. # elif defined(__MRC__)
  57. void *alloca(unsigned);
  58. # else
  59. char *alloca();
  60. # endif
  61. # endif
  62. #endif
  63. #ifdef SIZE_MAX
  64. # define SDL_SIZE_MAX SIZE_MAX
  65. #else
  66. # define SDL_SIZE_MAX ((size_t) -1)
  67. #endif
  68. /**
  69. * Check if the compiler supports a given builtin.
  70. * Supported by virtually all clang versions and recent gcc. Use this
  71. * instead of checking the clang version if possible.
  72. */
  73. #ifdef __has_builtin
  74. #define SDL_HAS_BUILTIN(x) __has_builtin(x)
  75. #else
  76. #define SDL_HAS_BUILTIN(x) 0
  77. #endif
  78. /**
  79. * The number of elements in an array.
  80. */
  81. #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
  82. #define SDL_TABLESIZE(table) SDL_arraysize(table)
  83. /**
  84. * Macro useful for building other macros with strings in them.
  85. *
  86. * For example:
  87. *
  88. * ```c
  89. * #define LOG_ERROR(X) OutputDebugString(SDL_STRINGIFY_ARG(__FUNCTION__) ": " X "\n")`
  90. * ```
  91. */
  92. #define SDL_STRINGIFY_ARG(arg) #arg
  93. /**
  94. * \name Cast operators
  95. *
  96. * Use proper C++ casts when compiled as C++ to be compatible with the option
  97. * -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above).
  98. */
  99. /* @{ */
  100. #ifdef __cplusplus
  101. #define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
  102. #define SDL_static_cast(type, expression) static_cast<type>(expression)
  103. #define SDL_const_cast(type, expression) const_cast<type>(expression)
  104. #else
  105. #define SDL_reinterpret_cast(type, expression) ((type)(expression))
  106. #define SDL_static_cast(type, expression) ((type)(expression))
  107. #define SDL_const_cast(type, expression) ((type)(expression))
  108. #endif
  109. /* @} *//* Cast operators */
  110. /* Define a four character code as a Uint32 */
  111. #define SDL_FOURCC(A, B, C, D) \
  112. ((SDL_static_cast(Uint32, SDL_static_cast(Uint8, (A))) << 0) | \
  113. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (B))) << 8) | \
  114. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (C))) << 16) | \
  115. (SDL_static_cast(Uint32, SDL_static_cast(Uint8, (D))) << 24))
  116. /**
  117. * \name Basic data types
  118. */
  119. /* @{ */
  120. /**
  121. * A boolean type.
  122. */
  123. #define SDL_FALSE 0
  124. #define SDL_TRUE 1
  125. typedef int SDL_bool;
  126. /**
  127. * A signed 8-bit integer type.
  128. */
  129. #define SDL_MAX_SINT8 ((Sint8)0x7F) /* 127 */
  130. #define SDL_MIN_SINT8 ((Sint8)(~0x7F)) /* -128 */
  131. typedef int8_t Sint8;
  132. /**
  133. * An unsigned 8-bit integer type.
  134. */
  135. #define SDL_MAX_UINT8 ((Uint8)0xFF) /* 255 */
  136. #define SDL_MIN_UINT8 ((Uint8)0x00) /* 0 */
  137. typedef uint8_t Uint8;
  138. /**
  139. * A signed 16-bit integer type.
  140. */
  141. #define SDL_MAX_SINT16 ((Sint16)0x7FFF) /* 32767 */
  142. #define SDL_MIN_SINT16 ((Sint16)(~0x7FFF)) /* -32768 */
  143. typedef int16_t Sint16;
  144. /**
  145. * An unsigned 16-bit integer type.
  146. */
  147. #define SDL_MAX_UINT16 ((Uint16)0xFFFF) /* 65535 */
  148. #define SDL_MIN_UINT16 ((Uint16)0x0000) /* 0 */
  149. typedef uint16_t Uint16;
  150. /**
  151. * A signed 32-bit integer type.
  152. */
  153. #define SDL_MAX_SINT32 ((Sint32)0x7FFFFFFF) /* 2147483647 */
  154. #define SDL_MIN_SINT32 ((Sint32)(~0x7FFFFFFF)) /* -2147483648 */
  155. typedef int32_t Sint32;
  156. /**
  157. * An unsigned 32-bit integer type.
  158. */
  159. #define SDL_MAX_UINT32 ((Uint32)0xFFFFFFFFu) /* 4294967295 */
  160. #define SDL_MIN_UINT32 ((Uint32)0x00000000) /* 0 */
  161. typedef uint32_t Uint32;
  162. /**
  163. * A signed 64-bit integer type.
  164. */
  165. #define SDL_MAX_SINT64 ((Sint64)0x7FFFFFFFFFFFFFFFll) /* 9223372036854775807 */
  166. #define SDL_MIN_SINT64 ((Sint64)(~0x7FFFFFFFFFFFFFFFll)) /* -9223372036854775808 */
  167. typedef int64_t Sint64;
  168. /**
  169. * An unsigned 64-bit integer type.
  170. */
  171. #define SDL_MAX_UINT64 ((Uint64)0xFFFFFFFFFFFFFFFFull) /* 18446744073709551615 */
  172. #define SDL_MIN_UINT64 ((Uint64)(0x0000000000000000ull)) /* 0 */
  173. typedef uint64_t Uint64;
  174. /**
  175. * SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1, 1970).
  176. *
  177. * They can be converted between POSIX time_t values with SDL_NS_TO_SECONDS() and SDL_SECONDS_TO_NS(),
  178. * and between Windows FILETIME values with SDL_TimeToWindows() and SDL_TimeFromWindows().
  179. */
  180. #define SDL_MAX_TIME SDL_MAX_SINT64
  181. #define SDL_MIN_TIME SDL_MIN_SINT64
  182. typedef Sint64 SDL_Time;
  183. /* @} *//* Basic data types */
  184. /**
  185. * \name Floating-point constants
  186. */
  187. /* @{ */
  188. #ifdef FLT_EPSILON
  189. #define SDL_FLT_EPSILON FLT_EPSILON
  190. #else
  191. #define SDL_FLT_EPSILON 1.1920928955078125e-07F /* 0x0.000002p0 */
  192. #endif
  193. /* @} *//* Floating-point constants */
  194. /* Make sure we have macros for printing width-based integers.
  195. * <stdint.h> should define these but this is not true all platforms.
  196. * (for example win32) */
  197. #ifndef SDL_PRIs64
  198. #ifdef PRIs64
  199. #define SDL_PRIs64 PRIs64
  200. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  201. #define SDL_PRIs64 "I64d"
  202. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  203. #define SDL_PRIs64 "ld"
  204. #else
  205. #define SDL_PRIs64 "lld"
  206. #endif
  207. #endif
  208. #ifndef SDL_PRIu64
  209. #ifdef PRIu64
  210. #define SDL_PRIu64 PRIu64
  211. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  212. #define SDL_PRIu64 "I64u"
  213. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  214. #define SDL_PRIu64 "lu"
  215. #else
  216. #define SDL_PRIu64 "llu"
  217. #endif
  218. #endif
  219. #ifndef SDL_PRIx64
  220. #ifdef PRIx64
  221. #define SDL_PRIx64 PRIx64
  222. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  223. #define SDL_PRIx64 "I64x"
  224. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  225. #define SDL_PRIx64 "lx"
  226. #else
  227. #define SDL_PRIx64 "llx"
  228. #endif
  229. #endif
  230. #ifndef SDL_PRIX64
  231. #ifdef PRIX64
  232. #define SDL_PRIX64 PRIX64
  233. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  234. #define SDL_PRIX64 "I64X"
  235. #elif defined(__LP64__) && !defined(SDL_PLATFORM_APPLE)
  236. #define SDL_PRIX64 "lX"
  237. #else
  238. #define SDL_PRIX64 "llX"
  239. #endif
  240. #endif
  241. #ifndef SDL_PRIs32
  242. #ifdef PRId32
  243. #define SDL_PRIs32 PRId32
  244. #else
  245. #define SDL_PRIs32 "d"
  246. #endif
  247. #endif
  248. #ifndef SDL_PRIu32
  249. #ifdef PRIu32
  250. #define SDL_PRIu32 PRIu32
  251. #else
  252. #define SDL_PRIu32 "u"
  253. #endif
  254. #endif
  255. #ifndef SDL_PRIx32
  256. #ifdef PRIx32
  257. #define SDL_PRIx32 PRIx32
  258. #else
  259. #define SDL_PRIx32 "x"
  260. #endif
  261. #endif
  262. #ifndef SDL_PRIX32
  263. #ifdef PRIX32
  264. #define SDL_PRIX32 PRIX32
  265. #else
  266. #define SDL_PRIX32 "X"
  267. #endif
  268. #endif
  269. /* Annotations to help code analysis tools */
  270. #ifdef SDL_DISABLE_ANALYZE_MACROS
  271. #define SDL_IN_BYTECAP(x)
  272. #define SDL_INOUT_Z_CAP(x)
  273. #define SDL_OUT_Z_CAP(x)
  274. #define SDL_OUT_CAP(x)
  275. #define SDL_OUT_BYTECAP(x)
  276. #define SDL_OUT_Z_BYTECAP(x)
  277. #define SDL_PRINTF_FORMAT_STRING
  278. #define SDL_SCANF_FORMAT_STRING
  279. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  280. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  281. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  282. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  283. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  284. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  285. #else
  286. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  287. #include <sal.h>
  288. #define SDL_IN_BYTECAP(x) _In_bytecount_(x)
  289. #define SDL_INOUT_Z_CAP(x) _Inout_z_cap_(x)
  290. #define SDL_OUT_Z_CAP(x) _Out_z_cap_(x)
  291. #define SDL_OUT_CAP(x) _Out_cap_(x)
  292. #define SDL_OUT_BYTECAP(x) _Out_bytecap_(x)
  293. #define SDL_OUT_Z_BYTECAP(x) _Out_z_bytecap_(x)
  294. #define SDL_PRINTF_FORMAT_STRING _Printf_format_string_
  295. #define SDL_SCANF_FORMAT_STRING _Scanf_format_string_impl_
  296. #else
  297. #define SDL_IN_BYTECAP(x)
  298. #define SDL_INOUT_Z_CAP(x)
  299. #define SDL_OUT_Z_CAP(x)
  300. #define SDL_OUT_CAP(x)
  301. #define SDL_OUT_BYTECAP(x)
  302. #define SDL_OUT_Z_BYTECAP(x)
  303. #define SDL_PRINTF_FORMAT_STRING
  304. #define SDL_SCANF_FORMAT_STRING
  305. #endif
  306. #ifdef __GNUC__
  307. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __printf__, fmtargnumber, fmtargnumber+1 )))
  308. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __printf__, fmtargnumber, 0 )))
  309. #define SDL_SCANF_VARARG_FUNC( fmtargnumber ) __attribute__ (( format( __scanf__, fmtargnumber, fmtargnumber+1 )))
  310. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber ) __attribute__(( format( __scanf__, fmtargnumber, 0 )))
  311. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wprintf__, fmtargnumber, fmtargnumber+1 ))) */
  312. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber ) /* __attribute__ (( format( __wscanf__, fmtargnumber, fmtargnumber+1 ))) */
  313. #else
  314. #define SDL_PRINTF_VARARG_FUNC( fmtargnumber )
  315. #define SDL_PRINTF_VARARG_FUNCV( fmtargnumber )
  316. #define SDL_SCANF_VARARG_FUNC( fmtargnumber )
  317. #define SDL_SCANF_VARARG_FUNCV( fmtargnumber )
  318. #define SDL_WPRINTF_VARARG_FUNC( fmtargnumber )
  319. #define SDL_WSCANF_VARARG_FUNC( fmtargnumber )
  320. #endif
  321. #endif /* SDL_DISABLE_ANALYZE_MACROS */
  322. #ifndef SDL_COMPILE_TIME_ASSERT
  323. #ifdef __cplusplus
  324. #if (__cplusplus >= 201103L)
  325. #define SDL_COMPILE_TIME_ASSERT(name, x) static_assert(x, #x)
  326. #endif
  327. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  328. #define SDL_COMPILE_TIME_ASSERT(name, x) _Static_assert(x, #x)
  329. #endif
  330. #endif /* !SDL_COMPILE_TIME_ASSERT */
  331. #ifndef SDL_COMPILE_TIME_ASSERT
  332. /* universal, but may trigger -Wunused-local-typedefs */
  333. #define SDL_COMPILE_TIME_ASSERT(name, x) \
  334. typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
  335. #endif
  336. /** \cond */
  337. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  338. SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
  339. SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
  340. SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
  341. SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
  342. SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
  343. SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
  344. SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
  345. SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
  346. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  347. /** \endcond */
  348. /* Check to make sure enums are the size of ints, for structure packing.
  349. For both Watcom C/C++ and Borland C/C++ the compiler option that makes
  350. enums having the size of an int must be enabled.
  351. This is "-b" for Borland C/C++ and "-ei" for Watcom C/C++ (v11).
  352. */
  353. /** \cond */
  354. #ifndef DOXYGEN_SHOULD_IGNORE_THIS
  355. #if !defined(SDL_PLATFORM_VITA) && !defined(SDL_PLATFORM_3DS)
  356. /* TODO: include/SDL_stdinc.h:390: error: size of array 'SDL_dummy_enum' is negative */
  357. typedef enum
  358. {
  359. DUMMY_ENUM_VALUE
  360. } SDL_DUMMY_ENUM;
  361. SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
  362. #endif
  363. #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
  364. /** \endcond */
  365. #include <SDL3/SDL_begin_code.h>
  366. /* Set up for C function definitions, even when using C++ */
  367. #ifdef __cplusplus
  368. extern "C" {
  369. #endif
  370. #ifndef SDL_DISABLE_ALLOCA
  371. #define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*(count))
  372. #define SDL_stack_free(data)
  373. #else
  374. #define SDL_stack_alloc(type, count) (type*)SDL_malloc(sizeof(type)*(count))
  375. #define SDL_stack_free(data) SDL_free(data)
  376. #endif
  377. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_malloc(size_t size);
  378. extern DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) void *SDLCALL SDL_calloc(size_t nmemb, size_t size);
  379. extern DECLSPEC SDL_ALLOC_SIZE(2) void *SDLCALL SDL_realloc(void *mem, size_t size);
  380. extern DECLSPEC void SDLCALL SDL_free(void *mem);
  381. typedef void *(SDLCALL *SDL_malloc_func)(size_t size);
  382. typedef void *(SDLCALL *SDL_calloc_func)(size_t nmemb, size_t size);
  383. typedef void *(SDLCALL *SDL_realloc_func)(void *mem, size_t size);
  384. typedef void (SDLCALL *SDL_free_func)(void *mem);
  385. /**
  386. * Get the original set of SDL memory functions.
  387. *
  388. * \param malloc_func filled with malloc function
  389. * \param calloc_func filled with calloc function
  390. * \param realloc_func filled with realloc function
  391. * \param free_func filled with free function
  392. *
  393. * \since This function is available since SDL 3.0.0.
  394. */
  395. extern DECLSPEC void SDLCALL SDL_GetOriginalMemoryFunctions(SDL_malloc_func *malloc_func,
  396. SDL_calloc_func *calloc_func,
  397. SDL_realloc_func *realloc_func,
  398. SDL_free_func *free_func);
  399. /**
  400. * Get the current set of SDL memory functions.
  401. *
  402. * \param malloc_func filled with malloc function
  403. * \param calloc_func filled with calloc function
  404. * \param realloc_func filled with realloc function
  405. * \param free_func filled with free function
  406. *
  407. * \since This function is available since SDL 3.0.0.
  408. */
  409. extern DECLSPEC void SDLCALL SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
  410. SDL_calloc_func *calloc_func,
  411. SDL_realloc_func *realloc_func,
  412. SDL_free_func *free_func);
  413. /**
  414. * Replace SDL's memory allocation functions with a custom set.
  415. *
  416. * \param malloc_func custom malloc function
  417. * \param calloc_func custom calloc function
  418. * \param realloc_func custom realloc function
  419. * \param free_func custom free function
  420. * \returns 0 on success or a negative error code on failure; call
  421. * SDL_GetError() for more information.
  422. *
  423. * \since This function is available since SDL 3.0.0.
  424. */
  425. extern DECLSPEC int SDLCALL SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
  426. SDL_calloc_func calloc_func,
  427. SDL_realloc_func realloc_func,
  428. SDL_free_func free_func);
  429. /**
  430. * Allocate memory aligned to a specific value.
  431. *
  432. * If `alignment` is less than the size of `void *`, then it will be increased
  433. * to match that.
  434. *
  435. * The returned memory address will be a multiple of the alignment value, and
  436. * the amount of memory allocated will be a multiple of the alignment value.
  437. *
  438. * The memory returned by this function must be freed with SDL_aligned_free()
  439. *
  440. * \param alignment the alignment requested
  441. * \param size the size to allocate
  442. * \returns a pointer to the aligned memory
  443. *
  444. * \since This function is available since SDL 3.0.0.
  445. *
  446. * \sa SDL_aligned_free
  447. */
  448. extern DECLSPEC SDL_MALLOC void *SDLCALL SDL_aligned_alloc(size_t alignment, size_t size);
  449. /**
  450. * Free memory allocated by SDL_aligned_alloc().
  451. *
  452. * \since This function is available since SDL 3.0.0.
  453. *
  454. * \sa SDL_aligned_alloc
  455. */
  456. extern DECLSPEC void SDLCALL SDL_aligned_free(void *mem);
  457. /**
  458. * Get the number of outstanding (unfreed) allocations.
  459. *
  460. * \returns the number of allocations
  461. *
  462. * \since This function is available since SDL 3.0.0.
  463. */
  464. extern DECLSPEC int SDLCALL SDL_GetNumAllocations(void);
  465. extern DECLSPEC char *SDLCALL SDL_getenv(const char *name);
  466. extern DECLSPEC int SDLCALL SDL_setenv(const char *name, const char *value, int overwrite);
  467. extern DECLSPEC void SDLCALL SDL_qsort(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
  468. extern DECLSPEC void * SDLCALL SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (const void *, const void *));
  469. extern DECLSPEC void SDLCALL SDL_qsort_r(void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (void *, const void *, const void *), void *userdata);
  470. extern DECLSPEC void * SDLCALL SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (SDLCALL *compare) (void *, const void *, const void *), void *userdata);
  471. extern DECLSPEC int SDLCALL SDL_abs(int x);
  472. /* NOTE: these double-evaluate their arguments, so you should never have side effects in the parameters */
  473. #define SDL_min(x, y) (((x) < (y)) ? (x) : (y))
  474. #define SDL_max(x, y) (((x) > (y)) ? (x) : (y))
  475. #define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
  476. /**
  477. * Query if a character is alphabetic (a letter).
  478. *
  479. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  480. * for English 'a-z' and 'A-Z' as true.
  481. *
  482. * \param x character value to check.
  483. * \returns non-zero if x falls within the character class, zero otherwise.
  484. *
  485. * \threadsafety It is safe to call this function from any thread.
  486. *
  487. * \since This function is available since SDL 3.0.0.
  488. */
  489. extern DECLSPEC int SDLCALL SDL_isalpha(int x);
  490. /**
  491. * Query if a character is alphabetic (a letter) or a number.
  492. *
  493. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  494. * for English 'a-z', 'A-Z', and '0-9' as true.
  495. *
  496. * \param x character value to check.
  497. * \returns non-zero if x falls within the character class, zero otherwise.
  498. *
  499. * \threadsafety It is safe to call this function from any thread.
  500. *
  501. * \since This function is available since SDL 3.0.0.
  502. */
  503. extern DECLSPEC int SDLCALL SDL_isalnum(int x);
  504. /**
  505. * Report if a character is blank (a space or tab).
  506. *
  507. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  508. * 0x20 (space) or 0x9 (tab) as true.
  509. *
  510. * \param x character value to check.
  511. * \returns non-zero if x falls within the character class, zero otherwise.
  512. *
  513. * \threadsafety It is safe to call this function from any thread.
  514. *
  515. * \since This function is available since SDL 3.0.0.
  516. */
  517. extern DECLSPEC int SDLCALL SDL_isblank(int x);
  518. /**
  519. * Report if a character is a control character.
  520. *
  521. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  522. * 0 through 0x1F, and 0x7F, as true.
  523. *
  524. * \param x character value to check.
  525. * \returns non-zero if x falls within the character class, zero otherwise.
  526. *
  527. * \threadsafety It is safe to call this function from any thread.
  528. *
  529. * \since This function is available since SDL 3.0.0.
  530. */
  531. extern DECLSPEC int SDLCALL SDL_iscntrl(int x);
  532. /**
  533. * Report if a character is a numeric digit.
  534. *
  535. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  536. * '0' (0x30) through '9' (0x39), as true.
  537. *
  538. * \param x character value to check.
  539. * \returns non-zero if x falls within the character class, zero otherwise.
  540. *
  541. * \threadsafety It is safe to call this function from any thread.
  542. *
  543. * \since This function is available since SDL 3.0.0.
  544. */
  545. extern DECLSPEC int SDLCALL SDL_isdigit(int x);
  546. /**
  547. * Report if a character is a hexadecimal digit.
  548. *
  549. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  550. * 'A' through 'F', 'a' through 'f', and '0' through '9', as true.
  551. *
  552. * \param x character value to check.
  553. * \returns non-zero if x falls within the character class, zero otherwise.
  554. *
  555. * \threadsafety It is safe to call this function from any thread.
  556. *
  557. * \since This function is available since SDL 3.0.0.
  558. */
  559. extern DECLSPEC int SDLCALL SDL_isxdigit(int x);
  560. /**
  561. * Report if a character is a punctuation mark.
  562. *
  563. * **WARNING**: Regardless of system locale, this is equivalent to
  564. * `((SDL_isgraph(x)) && (!SDL_isalnum(x)))`.
  565. *
  566. * \param x character value to check.
  567. * \returns non-zero if x falls within the character class, zero otherwise.
  568. *
  569. * \threadsafety It is safe to call this function from any thread.
  570. *
  571. * \since This function is available since SDL 3.0.0.
  572. *
  573. * \sa SDL_isgraph
  574. * \sa SDL_isalnum
  575. */
  576. extern DECLSPEC int SDLCALL SDL_ispunct(int x);
  577. /**
  578. * Report if a character is whitespace.
  579. *
  580. * **WARNING**: Regardless of system locale, this will only treat the
  581. * following ASCII values as true:
  582. *
  583. * - space (0x20)
  584. * - tab (0x09)
  585. * - newline (0x0A)
  586. * - vertical tab (0x0B)
  587. * - form feed (0x0C)
  588. * - return (0x0D)
  589. *
  590. * \param x character value to check.
  591. * \returns non-zero if x falls within the character class, zero otherwise.
  592. *
  593. * \threadsafety It is safe to call this function from any thread.
  594. *
  595. * \since This function is available since SDL 3.0.0.
  596. */
  597. extern DECLSPEC int SDLCALL SDL_isspace(int x);
  598. /**
  599. * Report if a character is upper case.
  600. *
  601. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  602. * 'A' through 'Z' as true.
  603. *
  604. * \param x character value to check.
  605. * \returns non-zero if x falls within the character class, zero otherwise.
  606. *
  607. * \threadsafety It is safe to call this function from any thread.
  608. *
  609. * \since This function is available since SDL 3.0.0.
  610. */
  611. extern DECLSPEC int SDLCALL SDL_isupper(int x);
  612. /**
  613. * Report if a character is lower case.
  614. *
  615. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  616. * 'a' through 'z' as true.
  617. *
  618. * \param x character value to check.
  619. * \returns non-zero if x falls within the character class, zero otherwise.
  620. *
  621. * \threadsafety It is safe to call this function from any thread.
  622. *
  623. * \since This function is available since SDL 3.0.0.
  624. */
  625. extern DECLSPEC int SDLCALL SDL_islower(int x);
  626. /**
  627. * Report if a character is "printable".
  628. *
  629. * Be advised that "printable" has a definition that goes back to text
  630. * terminals from the dawn of computing, making this a sort of special case
  631. * function that is not suitable for Unicode (or most any) text management.
  632. *
  633. * **WARNING**: Regardless of system locale, this will only treat ASCII values
  634. * ' ' (0x20) through '~' (0x7E) as true.
  635. *
  636. * \param x character value to check.
  637. * \returns non-zero if x falls within the character class, zero otherwise.
  638. *
  639. * \threadsafety It is safe to call this function from any thread.
  640. *
  641. * \since This function is available since SDL 3.0.0.
  642. */
  643. extern DECLSPEC int SDLCALL SDL_isprint(int x);
  644. /**
  645. * Report if a character is any "printable" except space.
  646. *
  647. * Be advised that "printable" has a definition that goes back to text
  648. * terminals from the dawn of computing, making this a sort of special case
  649. * function that is not suitable for Unicode (or most any) text management.
  650. *
  651. * **WARNING**: Regardless of system locale, this is equivalent to
  652. * `(SDL_isprint(x)) && ((x) != ' ')`.
  653. *
  654. * \param x character value to check.
  655. * \returns non-zero if x falls within the character class, zero otherwise.
  656. *
  657. * \threadsafety It is safe to call this function from any thread.
  658. *
  659. * \since This function is available since SDL 3.0.0.
  660. *
  661. * \sa SDL_isprint
  662. */
  663. extern DECLSPEC int SDLCALL SDL_isgraph(int x);
  664. /**
  665. * Convert low-ASCII English letters to uppercase.
  666. *
  667. * **WARNING**: Regardless of system locale, this will only convert ASCII
  668. * values 'a' through 'z' to uppercase.
  669. *
  670. * This function returns the uppercase equivalent of `x`. If a character
  671. * cannot be converted, or is already uppercase, this function returns `x`.
  672. *
  673. * \param x character value to check.
  674. * \returns Capitalized version of x, or x if no conversion available.
  675. *
  676. * \threadsafety It is safe to call this function from any thread.
  677. *
  678. * \since This function is available since SDL 3.0.0.
  679. */
  680. extern DECLSPEC int SDLCALL SDL_toupper(int x);
  681. /**
  682. * Convert low-ASCII English letters to lowercase.
  683. *
  684. * **WARNING**: Regardless of system locale, this will only convert ASCII
  685. * values 'A' through 'Z' to lowercase.
  686. *
  687. * This function returns the lowercase equivalent of `x`. If a character
  688. * cannot be converted, or is already lowercase, this function returns `x`.
  689. *
  690. * \param x character value to check.
  691. * \returns Lowercase version of x, or x if no conversion available.
  692. *
  693. * \threadsafety It is safe to call this function from any thread.
  694. *
  695. * \since This function is available since SDL 3.0.0.
  696. */
  697. extern DECLSPEC int SDLCALL SDL_tolower(int x);
  698. extern DECLSPEC Uint16 SDLCALL SDL_crc16(Uint16 crc, const void *data, size_t len);
  699. extern DECLSPEC Uint32 SDLCALL SDL_crc32(Uint32 crc, const void *data, size_t len);
  700. extern DECLSPEC void *SDLCALL SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  701. /* Take advantage of compiler optimizations for memcpy */
  702. #ifndef SDL_SLOW_MEMCPY
  703. #ifdef SDL_memcpy
  704. #undef SDL_memcpy
  705. #endif
  706. #define SDL_memcpy memcpy
  707. #endif
  708. #define SDL_copyp(dst, src) \
  709. { SDL_COMPILE_TIME_ASSERT(SDL_copyp, sizeof (*(dst)) == sizeof (*(src))); } \
  710. SDL_memcpy((dst), (src), sizeof(*(src)))
  711. extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len);
  712. /* Take advantage of compiler optimizations for memmove */
  713. #ifndef SDL_SLOW_MEMMOVE
  714. #ifdef SDL_memmove
  715. #undef SDL_memmove
  716. #endif
  717. #define SDL_memmove memmove
  718. #endif
  719. extern DECLSPEC void *SDLCALL SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len);
  720. extern DECLSPEC void *SDLCALL SDL_memset4(void *dst, Uint32 val, size_t dwords);
  721. /* Take advantage of compiler optimizations for memset */
  722. #ifndef SDL_SLOW_MEMSET
  723. #ifdef SDL_memset
  724. #undef SDL_memset
  725. #endif
  726. #define SDL_memset memset
  727. #endif
  728. #define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))
  729. #define SDL_zerop(x) SDL_memset((x), 0, sizeof(*(x)))
  730. #define SDL_zeroa(x) SDL_memset((x), 0, sizeof((x)))
  731. extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
  732. extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
  733. extern DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
  734. extern DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  735. extern DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
  736. extern DECLSPEC wchar_t *SDLCALL SDL_wcsdup(const wchar_t *wstr);
  737. extern DECLSPEC wchar_t *SDLCALL SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle);
  738. extern DECLSPEC wchar_t *SDLCALL SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen);
  739. /**
  740. * Compare two null-terminated wide strings.
  741. *
  742. * This only compares wchar_t values until it hits a null-terminating
  743. * character; it does not care if the string is well-formed UTF-16 (or UTF-32,
  744. * depending on your platform's wchar_t size), or uses valid Unicode values.
  745. *
  746. * \param str1 The first string to compare. NULL is not permitted!
  747. * \param str2 The second string to compare. NULL is not permitted!
  748. * \returns less than zero if str1 is "less than" str2, greater than zero if
  749. * str1 is "greater than" str2, and zero if the strings match
  750. * exactly.
  751. *
  752. * \threadsafety It is safe to call this function from any thread.
  753. *
  754. * \since This function is available since SDL 3.0.0.
  755. */
  756. extern DECLSPEC int SDLCALL SDL_wcscmp(const wchar_t *str1, const wchar_t *str2);
  757. /**
  758. * Compare two wide strings up to a number of wchar_t values.
  759. *
  760. * This only compares wchar_t values; it does not care if the string is
  761. * well-formed UTF-16 (or UTF-32, depending on your platform's wchar_t size),
  762. * or uses valid Unicode values.
  763. *
  764. * Note that while this function is intended to be used with UTF-16 (or
  765. * UTF-32, depending on your platform's definition of wchar_t), it is
  766. * comparing raw wchar_t values and not Unicode codepoints: `maxlen` specifies
  767. * a wchar_t limit! If the limit lands in the middle of a multi-wchar UTF-16
  768. * sequence, it will only compare a portion of the final character.
  769. *
  770. * `maxlen` specifies a maximum number of wchar_t to compare; if the strings
  771. * match to this number of wide chars (or both have matched to a
  772. * null-terminator character before this count), they will be considered
  773. * equal.
  774. *
  775. * \param str1 The first string to compare. NULL is not permitted!
  776. * \param str2 The second string to compare. NULL is not permitted!
  777. * \param maxlen The maximum number of wchar_t to compare.
  778. * \returns less than zero if str1 is "less than" str2, greater than zero if
  779. * str1 is "greater than" str2, and zero if the strings match
  780. * exactly.
  781. *
  782. * \threadsafety It is safe to call this function from any thread.
  783. *
  784. * \since This function is available since SDL 3.0.0.
  785. */
  786. extern DECLSPEC int SDLCALL SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  787. /**
  788. * Compare two null-terminated wide strings, case-insensitively.
  789. *
  790. * This will work with Unicode strings, using a technique called
  791. * "case-folding" to handle the vast majority of case-sensitive human
  792. * languages regardless of system locale. It can deal with expanding values: a
  793. * German Eszett character can compare against two ASCII 's' chars and be
  794. * considered a match, for example. A notable exception: it does not handle
  795. * the Turkish 'i' character; human language is complicated!
  796. *
  797. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  798. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  799. * handles Unicode, it expects the string to be well-formed and not a
  800. * null-terminated string of arbitrary bytes. Characters that are not valid
  801. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  802. * CHARACTER), which is to say two strings of random bits may turn out to
  803. * match if they convert to the same amount of replacement characters.
  804. *
  805. * \param str1 The first string to compare. NULL is not permitted!
  806. * \param str2 The second string to compare. NULL is not permitted!
  807. * \returns less than zero if str1 is "less than" str2, greater than zero if
  808. * str1 is "greater than" str2, and zero if the strings match
  809. * exactly.
  810. *
  811. * \threadsafety It is safe to call this function from any thread.
  812. *
  813. * \since This function is available since SDL 3.0.0.
  814. */
  815. extern DECLSPEC int SDLCALL SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2);
  816. /**
  817. * Compare two wide strings, case-insensitively, up to a number of wchar_t.
  818. *
  819. * This will work with Unicode strings, using a technique called
  820. * "case-folding" to handle the vast majority of case-sensitive human
  821. * languages regardless of system locale. It can deal with expanding values: a
  822. * German Eszett character can compare against two ASCII 's' chars and be
  823. * considered a match, for example. A notable exception: it does not handle
  824. * the Turkish 'i' character; human language is complicated!
  825. *
  826. * Depending on your platform, "wchar_t" might be 2 bytes, and expected to be
  827. * UTF-16 encoded (like Windows), or 4 bytes in UTF-32 format. Since this
  828. * handles Unicode, it expects the string to be well-formed and not a
  829. * null-terminated string of arbitrary bytes. Characters that are not valid
  830. * UTF-16 (or UTF-32) are treated as Unicode character U+FFFD (REPLACEMENT
  831. * CHARACTER), which is to say two strings of random bits may turn out to
  832. * match if they convert to the same amount of replacement characters.
  833. *
  834. * Note that while this function might deal with variable-sized characters,
  835. * `maxlen` specifies a _wchar_ limit! If the limit lands in the middle of a
  836. * multi-byte UTF-16 sequence, it may convert a portion of the final character
  837. * to one or more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not
  838. * to overflow a buffer.
  839. *
  840. * `maxlen` specifies a maximum number of wchar_t values to compare; if the
  841. * strings match to this number of wchar_t (or both have matched to a
  842. * null-terminator character before this number of bytes), they will be
  843. * considered equal.
  844. *
  845. * \param str1 The first string to compare. NULL is not permitted!
  846. * \param str2 The second string to compare. NULL is not permitted!
  847. * \param maxlen The maximum number of wchar_t values to compare.
  848. * \returns less than zero if str1 is "less than" str2, greater than zero if
  849. * str1 is "greater than" str2, and zero if the strings match
  850. * exactly.
  851. *
  852. * \threadsafety It is safe to call this function from any thread.
  853. *
  854. * \since This function is available since SDL 3.0.0.
  855. */
  856. extern DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen);
  857. extern DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
  858. extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
  859. extern DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
  860. extern DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  861. extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
  862. extern DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
  863. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strdup(const char *str);
  864. extern DECLSPEC SDL_MALLOC char *SDLCALL SDL_strndup(const char *str, size_t maxlen);
  865. extern DECLSPEC char *SDLCALL SDL_strrev(char *str);
  866. /**
  867. * Convert a string to uppercase.
  868. *
  869. * **WARNING**: Regardless of system locale, this will only convert ASCII
  870. * values 'A' through 'Z' to uppercase.
  871. *
  872. * This function operates on a null-terminated string of bytes--even if it is
  873. * malformed UTF-8!--and converts ASCII characters 'a' through 'z' to their
  874. * uppercase equivalents in-place, returning the original `str` pointer.
  875. *
  876. * \threadsafety It is safe to call this function from any thread.
  877. *
  878. * \since This function is available since SDL 3.0.0.
  879. *
  880. * \sa SDL_strlwr
  881. */
  882. extern DECLSPEC char *SDLCALL SDL_strupr(char *str);
  883. /**
  884. * Convert a string to lowercase.
  885. *
  886. * **WARNING**: Regardless of system locale, this will only convert ASCII
  887. * values 'A' through 'Z' to lowercase.
  888. *
  889. * This function operates on a null-terminated string of bytes--even if it is
  890. * malformed UTF-8!--and converts ASCII characters 'A' through 'Z' to their
  891. * lowercase equivalents in-place, returning the original `str` pointer.
  892. *
  893. * \param str The string to convert in-place.
  894. * \returns The `str` pointer passed into this function.
  895. *
  896. * \threadsafety It is safe to call this function from any thread.
  897. *
  898. * \since This function is available since SDL 3.0.0.
  899. *
  900. * \sa SDL_strupr
  901. */
  902. extern DECLSPEC char *SDLCALL SDL_strlwr(char *str);
  903. extern DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c);
  904. extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
  905. extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
  906. extern DECLSPEC char *SDLCALL SDL_strnstr(const char *haystack, const char *needle, size_t maxlen);
  907. extern DECLSPEC char *SDLCALL SDL_strcasestr(const char *haystack, const char *needle);
  908. extern DECLSPEC char *SDLCALL SDL_strtok_r(char *s1, const char *s2, char **saveptr);
  909. extern DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
  910. extern DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
  911. extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
  912. extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
  913. extern DECLSPEC char *SDLCALL SDL_ltoa(long value, char *str, int radix);
  914. extern DECLSPEC char *SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
  915. extern DECLSPEC char *SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
  916. extern DECLSPEC char *SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
  917. extern DECLSPEC int SDLCALL SDL_atoi(const char *str);
  918. extern DECLSPEC double SDLCALL SDL_atof(const char *str);
  919. extern DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int base);
  920. extern DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
  921. extern DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
  922. extern DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
  923. extern DECLSPEC double SDLCALL SDL_strtod(const char *str, char **endp);
  924. /**
  925. * Compare two null-terminated UTF-8 strings.
  926. *
  927. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  928. * since effectively this function just compares bytes until it hits a
  929. * null-terminating character. Also due to the nature of UTF-8, this can be
  930. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  931. *
  932. * \param str1 The first string to compare. NULL is not permitted!
  933. * \param str2 The second string to compare. NULL is not permitted!
  934. * \returns less than zero if str1 is "less than" str2, greater than zero if
  935. * str1 is "greater than" str2, and zero if the strings match
  936. * exactly.
  937. *
  938. * \threadsafety It is safe to call this function from any thread.
  939. *
  940. * \since This function is available since SDL 3.0.0.
  941. */
  942. extern DECLSPEC int SDLCALL SDL_strcmp(const char *str1, const char *str2);
  943. /**
  944. * Compare two UTF-8 strings up to a number of bytes.
  945. *
  946. * Due to the nature of UTF-8 encoding, this will work with Unicode strings,
  947. * since effectively this function just compares bytes until it hits a
  948. * null-terminating character. Also due to the nature of UTF-8, this can be
  949. * used with SDL_qsort() to put strings in (roughly) alphabetical order.
  950. *
  951. * Note that while this function is intended to be used with UTF-8, it is
  952. * doing a bytewise comparison, and `maxlen` specifies a _byte_ limit! If the
  953. * limit lands in the middle of a multi-byte UTF-8 sequence, it will only
  954. * compare a portion of the final character.
  955. *
  956. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  957. * match to this number of bytes (or both have matched to a null-terminator
  958. * character before this number of bytes), they will be considered equal.
  959. *
  960. * \param str1 The first string to compare. NULL is not permitted!
  961. * \param str2 The second string to compare. NULL is not permitted!
  962. * \param maxlen The maximum number of _bytes_ to compare.
  963. * \returns less than zero if str1 is "less than" str2, greater than zero if
  964. * str1 is "greater than" str2, and zero if the strings match
  965. * exactly.
  966. *
  967. * \threadsafety It is safe to call this function from any thread.
  968. *
  969. * \since This function is available since SDL 3.0.0.
  970. */
  971. extern DECLSPEC int SDLCALL SDL_strncmp(const char *str1, const char *str2, size_t maxlen);
  972. /**
  973. * Compare two null-terminated UTF-8 strings, case-insensitively.
  974. *
  975. * This will work with Unicode strings, using a technique called
  976. * "case-folding" to handle the vast majority of case-sensitive human
  977. * languages regardless of system locale. It can deal with expanding values: a
  978. * German Eszett character can compare against two ASCII 's' chars and be
  979. * considered a match, for example. A notable exception: it does not handle
  980. * the Turkish 'i' character; human language is complicated!
  981. *
  982. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  983. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  984. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  985. * CHARACTER), which is to say two strings of random bits may turn out to
  986. * match if they convert to the same amount of replacement characters.
  987. *
  988. * \param str1 The first string to compare. NULL is not permitted!
  989. * \param str2 The second string to compare. NULL is not permitted!
  990. * \returns less than zero if str1 is "less than" str2, greater than zero if
  991. * str1 is "greater than" str2, and zero if the strings match
  992. * exactly.
  993. *
  994. * \threadsafety It is safe to call this function from any thread.
  995. *
  996. * \since This function is available since SDL 3.0.0.
  997. */
  998. extern DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2);
  999. /**
  1000. * Compare two UTF-8 strings, case-insensitively, up to a number of bytes.
  1001. *
  1002. * This will work with Unicode strings, using a technique called
  1003. * "case-folding" to handle the vast majority of case-sensitive human
  1004. * languages regardless of system locale. It can deal with expanding values: a
  1005. * German Eszett character can compare against two ASCII 's' chars and be
  1006. * considered a match, for example. A notable exception: it does not handle
  1007. * the Turkish 'i' character; human language is complicated!
  1008. *
  1009. * Since this handles Unicode, it expects the string to be well-formed UTF-8
  1010. * and not a null-terminated string of arbitrary bytes. Bytes that are not
  1011. * valid UTF-8 are treated as Unicode character U+FFFD (REPLACEMENT
  1012. * CHARACTER), which is to say two strings of random bits may turn out to
  1013. * match if they convert to the same amount of replacement characters.
  1014. *
  1015. * Note that while this function is intended to be used with UTF-8, `maxlen`
  1016. * specifies a _byte_ limit! If the limit lands in the middle of a multi-byte
  1017. * UTF-8 sequence, it may convert a portion of the final character to one or
  1018. * more Unicode character U+FFFD (REPLACEMENT CHARACTER) so as not to overflow
  1019. * a buffer.
  1020. *
  1021. * `maxlen` specifies a maximum number of bytes to compare; if the strings
  1022. * match to this number of bytes (or both have matched to a null-terminator
  1023. * character before this number of bytes), they will be considered equal.
  1024. *
  1025. * \param str1 The first string to compare. NULL is not permitted!
  1026. * \param str2 The second string to compare. NULL is not permitted!
  1027. * \param maxlen The maximum number of bytes to compare.
  1028. * \returns less than zero if str1 is "less than" str2, greater than zero if
  1029. * str1 is "greater than" str2, and zero if the strings match
  1030. * exactly.
  1031. *
  1032. * \threadsafety It is safe to call this function from any thread.
  1033. *
  1034. * \since This function is available since SDL 3.0.0.
  1035. */
  1036. extern DECLSPEC int SDLCALL SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen);
  1037. extern DECLSPEC int SDLCALL SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...) SDL_SCANF_VARARG_FUNC(2);
  1038. extern DECLSPEC int SDLCALL SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap) SDL_SCANF_VARARG_FUNCV(2);
  1039. extern DECLSPEC int SDLCALL SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ... ) SDL_PRINTF_VARARG_FUNC(3);
  1040. extern DECLSPEC int SDLCALL SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ... ) SDL_WPRINTF_VARARG_FUNC(3);
  1041. extern DECLSPEC int SDLCALL SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(3);
  1042. extern DECLSPEC int SDLCALL SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap);
  1043. extern DECLSPEC int SDLCALL SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
  1044. extern DECLSPEC int SDLCALL SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
  1045. #ifndef SDL_PI_D
  1046. #define SDL_PI_D 3.141592653589793238462643383279502884 /**< pi (double) */
  1047. #endif
  1048. #ifndef SDL_PI_F
  1049. #define SDL_PI_F 3.141592653589793238462643383279502884F /**< pi (float) */
  1050. #endif
  1051. /**
  1052. * Use this function to compute arc cosine of `x`.
  1053. *
  1054. * The definition of `y = acos(x)` is `x = cos(y)`.
  1055. *
  1056. * Domain: `-1 <= x <= 1`
  1057. *
  1058. * Range: `0 <= y <= Pi`
  1059. *
  1060. * \param x floating point value, in radians.
  1061. * \returns arc cosine of `x`.
  1062. *
  1063. * \threadsafety It is safe to call this function from any thread.
  1064. *
  1065. * \since This function is available since SDL 3.0.0.
  1066. */
  1067. extern DECLSPEC double SDLCALL SDL_acos(double x);
  1068. extern DECLSPEC float SDLCALL SDL_acosf(float x);
  1069. extern DECLSPEC double SDLCALL SDL_asin(double x);
  1070. extern DECLSPEC float SDLCALL SDL_asinf(float x);
  1071. extern DECLSPEC double SDLCALL SDL_atan(double x);
  1072. extern DECLSPEC float SDLCALL SDL_atanf(float x);
  1073. extern DECLSPEC double SDLCALL SDL_atan2(double y, double x);
  1074. extern DECLSPEC float SDLCALL SDL_atan2f(float y, float x);
  1075. extern DECLSPEC double SDLCALL SDL_ceil(double x);
  1076. extern DECLSPEC float SDLCALL SDL_ceilf(float x);
  1077. extern DECLSPEC double SDLCALL SDL_copysign(double x, double y);
  1078. extern DECLSPEC float SDLCALL SDL_copysignf(float x, float y);
  1079. extern DECLSPEC double SDLCALL SDL_cos(double x);
  1080. extern DECLSPEC float SDLCALL SDL_cosf(float x);
  1081. extern DECLSPEC double SDLCALL SDL_exp(double x);
  1082. extern DECLSPEC float SDLCALL SDL_expf(float x);
  1083. extern DECLSPEC double SDLCALL SDL_fabs(double x);
  1084. extern DECLSPEC float SDLCALL SDL_fabsf(float x);
  1085. extern DECLSPEC double SDLCALL SDL_floor(double x);
  1086. extern DECLSPEC float SDLCALL SDL_floorf(float x);
  1087. extern DECLSPEC double SDLCALL SDL_trunc(double x);
  1088. extern DECLSPEC float SDLCALL SDL_truncf(float x);
  1089. extern DECLSPEC double SDLCALL SDL_fmod(double x, double y);
  1090. extern DECLSPEC float SDLCALL SDL_fmodf(float x, float y);
  1091. extern DECLSPEC double SDLCALL SDL_log(double x);
  1092. extern DECLSPEC float SDLCALL SDL_logf(float x);
  1093. extern DECLSPEC double SDLCALL SDL_log10(double x);
  1094. extern DECLSPEC float SDLCALL SDL_log10f(float x);
  1095. extern DECLSPEC double SDLCALL SDL_modf(double x, double *y);
  1096. extern DECLSPEC float SDLCALL SDL_modff(float x, float *y);
  1097. extern DECLSPEC double SDLCALL SDL_pow(double x, double y);
  1098. extern DECLSPEC float SDLCALL SDL_powf(float x, float y);
  1099. extern DECLSPEC double SDLCALL SDL_round(double x);
  1100. extern DECLSPEC float SDLCALL SDL_roundf(float x);
  1101. extern DECLSPEC long SDLCALL SDL_lround(double x);
  1102. extern DECLSPEC long SDLCALL SDL_lroundf(float x);
  1103. extern DECLSPEC double SDLCALL SDL_scalbn(double x, int n);
  1104. extern DECLSPEC float SDLCALL SDL_scalbnf(float x, int n);
  1105. extern DECLSPEC double SDLCALL SDL_sin(double x);
  1106. extern DECLSPEC float SDLCALL SDL_sinf(float x);
  1107. extern DECLSPEC double SDLCALL SDL_sqrt(double x);
  1108. extern DECLSPEC float SDLCALL SDL_sqrtf(float x);
  1109. extern DECLSPEC double SDLCALL SDL_tan(double x);
  1110. extern DECLSPEC float SDLCALL SDL_tanf(float x);
  1111. /* The SDL implementation of iconv() returns these error codes */
  1112. #define SDL_ICONV_ERROR (size_t)-1
  1113. #define SDL_ICONV_E2BIG (size_t)-2
  1114. #define SDL_ICONV_EILSEQ (size_t)-3
  1115. #define SDL_ICONV_EINVAL (size_t)-4
  1116. /* SDL_iconv_* are now always real symbols/types, not macros or inlined. */
  1117. typedef struct SDL_iconv_data_t *SDL_iconv_t;
  1118. extern DECLSPEC SDL_iconv_t SDLCALL SDL_iconv_open(const char *tocode,
  1119. const char *fromcode);
  1120. extern DECLSPEC int SDLCALL SDL_iconv_close(SDL_iconv_t cd);
  1121. extern DECLSPEC size_t SDLCALL SDL_iconv(SDL_iconv_t cd, const char **inbuf,
  1122. size_t * inbytesleft, char **outbuf,
  1123. size_t * outbytesleft);
  1124. /**
  1125. * This function converts a buffer or string between encodings in one pass,
  1126. * returning a string that must be freed with SDL_free() or NULL on error.
  1127. *
  1128. * \since This function is available since SDL 3.0.0.
  1129. */
  1130. extern DECLSPEC char *SDLCALL SDL_iconv_string(const char *tocode,
  1131. const char *fromcode,
  1132. const char *inbuf,
  1133. size_t inbytesleft);
  1134. #define SDL_iconv_utf8_locale(S) SDL_iconv_string("", "UTF-8", S, SDL_strlen(S)+1)
  1135. #define SDL_iconv_utf8_ucs2(S) (Uint16 *)SDL_iconv_string("UCS-2", "UTF-8", S, SDL_strlen(S)+1)
  1136. #define SDL_iconv_utf8_ucs4(S) (Uint32 *)SDL_iconv_string("UCS-4", "UTF-8", S, SDL_strlen(S)+1)
  1137. #define SDL_iconv_wchar_utf8(S) SDL_iconv_string("UTF-8", "WCHAR_T", (char *)S, (SDL_wcslen(S)+1)*sizeof(wchar_t))
  1138. /* force builds using Clang's static analysis tools to use literal C runtime
  1139. here, since there are possibly tests that are ineffective otherwise. */
  1140. #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
  1141. /* The analyzer knows about strlcpy even when the system doesn't provide it */
  1142. #if !defined(HAVE_STRLCPY) && !defined(strlcpy)
  1143. size_t strlcpy(char* dst, const char* src, size_t size);
  1144. #endif
  1145. /* The analyzer knows about strlcat even when the system doesn't provide it */
  1146. #if !defined(HAVE_STRLCAT) && !defined(strlcat)
  1147. size_t strlcat(char* dst, const char* src, size_t size);
  1148. #endif
  1149. #if !defined(HAVE_WCSLCPY) && !defined(wcslcpy)
  1150. size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
  1151. #endif
  1152. #if !defined(HAVE_WCSLCAT) && !defined(wcslcat)
  1153. size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
  1154. #endif
  1155. /* Starting LLVM 16, the analyser errors out if these functions do not have
  1156. their prototype defined (clang-diagnostic-implicit-function-declaration) */
  1157. #include <stdlib.h>
  1158. #include <stdio.h>
  1159. #define SDL_malloc malloc
  1160. #define SDL_calloc calloc
  1161. #define SDL_realloc realloc
  1162. #define SDL_free free
  1163. #ifndef SDL_memcpy
  1164. #define SDL_memcpy memcpy
  1165. #endif
  1166. #ifndef SDL_memmove
  1167. #define SDL_memmove memmove
  1168. #endif
  1169. #ifndef SDL_memset
  1170. #define SDL_memset memset
  1171. #endif
  1172. #define SDL_memcmp memcmp
  1173. #define SDL_strlcpy strlcpy
  1174. #define SDL_strlcat strlcat
  1175. #define SDL_strlen strlen
  1176. #define SDL_wcslen wcslen
  1177. #define SDL_wcslcpy wcslcpy
  1178. #define SDL_wcslcat wcslcat
  1179. #define SDL_strdup strdup
  1180. #define SDL_wcsdup wcsdup
  1181. #define SDL_strchr strchr
  1182. #define SDL_strrchr strrchr
  1183. #define SDL_strstr strstr
  1184. #define SDL_wcsstr wcsstr
  1185. #define SDL_strtok_r strtok_r
  1186. #define SDL_strcmp strcmp
  1187. #define SDL_wcscmp wcscmp
  1188. #define SDL_strncmp strncmp
  1189. #define SDL_wcsncmp wcsncmp
  1190. #define SDL_strcasecmp strcasecmp
  1191. #define SDL_strncasecmp strncasecmp
  1192. #define SDL_sscanf sscanf
  1193. #define SDL_vsscanf vsscanf
  1194. #define SDL_snprintf snprintf
  1195. #define SDL_vsnprintf vsnprintf
  1196. #endif
  1197. /**
  1198. * If a * b would overflow, return -1. Otherwise store a * b via ret
  1199. * and return 0.
  1200. *
  1201. * \since This function is available since SDL 3.0.0.
  1202. */
  1203. SDL_FORCE_INLINE int SDL_size_mul_overflow (size_t a,
  1204. size_t b,
  1205. size_t *ret)
  1206. {
  1207. if (a != 0 && b > SDL_SIZE_MAX / a) {
  1208. return -1;
  1209. }
  1210. *ret = a * b;
  1211. return 0;
  1212. }
  1213. #if SDL_HAS_BUILTIN(__builtin_mul_overflow)
  1214. /* This needs to be wrapped in an inline rather than being a direct #define,
  1215. * because __builtin_mul_overflow() is type-generic, but we want to be
  1216. * consistent about interpreting a and b as size_t. */
  1217. SDL_FORCE_INLINE int SDL_size_mul_overflow_builtin (size_t a,
  1218. size_t b,
  1219. size_t *ret)
  1220. {
  1221. return __builtin_mul_overflow(a, b, ret) == 0 ? 0 : -1;
  1222. }
  1223. #define SDL_size_mul_overflow(a, b, ret) (SDL_size_mul_overflow_builtin(a, b, ret))
  1224. #endif
  1225. /**
  1226. * If a + b would overflow, return -1. Otherwise store a + b via ret
  1227. * and return 0.
  1228. *
  1229. * \since This function is available since SDL 3.0.0.
  1230. */
  1231. SDL_FORCE_INLINE int SDL_size_add_overflow (size_t a,
  1232. size_t b,
  1233. size_t *ret)
  1234. {
  1235. if (b > SDL_SIZE_MAX - a) {
  1236. return -1;
  1237. }
  1238. *ret = a + b;
  1239. return 0;
  1240. }
  1241. #if SDL_HAS_BUILTIN(__builtin_add_overflow)
  1242. /* This needs to be wrapped in an inline rather than being a direct #define,
  1243. * the same as the call to __builtin_mul_overflow() above. */
  1244. SDL_FORCE_INLINE int SDL_size_add_overflow_builtin (size_t a,
  1245. size_t b,
  1246. size_t *ret)
  1247. {
  1248. return __builtin_add_overflow(a, b, ret) == 0 ? 0 : -1;
  1249. }
  1250. #define SDL_size_add_overflow(a, b, ret) (SDL_size_add_overflow_builtin(a, b, ret))
  1251. #endif
  1252. /* This is a generic function pointer which should be cast to the type you expect */
  1253. #ifdef SDL_FUNCTION_POINTER_IS_VOID_POINTER
  1254. typedef void *SDL_FunctionPointer;
  1255. #else
  1256. typedef void (*SDL_FunctionPointer)(void);
  1257. #endif
  1258. /* Ends C function definitions when using C++ */
  1259. #ifdef __cplusplus
  1260. }
  1261. #endif
  1262. #include <SDL3/SDL_close_code.h>
  1263. #endif /* SDL_stdinc_h_ */