SDL_string.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #include "SDL_internal.h"
  19. /* This file contains portable string manipulation functions for SDL */
  20. #include "SDL_vacopy.h"
  21. #ifdef __vita__
  22. #include <psp2/kernel/clib.h>
  23. #endif
  24. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_WCSTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  25. #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
  26. #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
  27. #endif
  28. #define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
  29. #define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
  30. static size_t UTF8_GetTrailingBytes(unsigned char c)
  31. {
  32. if (c >= 0xC0 && c <= 0xDF) {
  33. return 1;
  34. } else if (c >= 0xE0 && c <= 0xEF) {
  35. return 2;
  36. } else if (c >= 0xF0 && c <= 0xF4) {
  37. return 3;
  38. }
  39. return 0;
  40. }
  41. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  42. static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
  43. {
  44. const char *textstart = text;
  45. long value = 0;
  46. SDL_bool negative = SDL_FALSE;
  47. if (*text == '-') {
  48. negative = SDL_TRUE;
  49. ++text;
  50. }
  51. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  52. text += 2;
  53. }
  54. for (;;) {
  55. int v;
  56. if (SDL_isdigit((unsigned char)*text)) {
  57. v = *text - '0';
  58. } else if (radix == 16 && SDL_isupperhex(*text)) {
  59. v = 10 + (*text - 'A');
  60. } else if (radix == 16 && SDL_islowerhex(*text)) {
  61. v = 10 + (*text - 'a');
  62. } else {
  63. break;
  64. }
  65. value *= radix;
  66. value += v;
  67. ++text;
  68. if (count > 0 && (text - textstart) == count) {
  69. break;
  70. }
  71. }
  72. if (valuep && text > textstart) {
  73. if (negative && value) {
  74. *valuep = -value;
  75. } else {
  76. *valuep = value;
  77. }
  78. }
  79. return text - textstart;
  80. }
  81. #endif
  82. #if !defined(HAVE_WCSTOL)
  83. static size_t SDL_ScanLongW(const wchar_t *text, int count, int radix, long *valuep)
  84. {
  85. const wchar_t *textstart = text;
  86. long value = 0;
  87. SDL_bool negative = SDL_FALSE;
  88. if (*text == '-') {
  89. negative = SDL_TRUE;
  90. ++text;
  91. }
  92. if (radix == 16 && SDL_wcsncmp(text, L"0x", 2) == 0) {
  93. text += 2;
  94. }
  95. for (;;) {
  96. int v;
  97. if (*text >= '0' && *text <= '9') {
  98. v = *text - '0';
  99. } else if (radix == 16 && SDL_isupperhex(*text)) {
  100. v = 10 + (*text - 'A');
  101. } else if (radix == 16 && SDL_islowerhex(*text)) {
  102. v = 10 + (*text - 'a');
  103. } else {
  104. break;
  105. }
  106. value *= radix;
  107. value += v;
  108. ++text;
  109. if (count > 0 && (text - textstart) == count) {
  110. break;
  111. }
  112. }
  113. if (valuep && text > textstart) {
  114. if (negative && value) {
  115. *valuep = -value;
  116. } else {
  117. *valuep = value;
  118. }
  119. }
  120. return text - textstart;
  121. }
  122. #endif
  123. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  124. static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
  125. {
  126. const char *textstart = text;
  127. unsigned long value = 0;
  128. if (*text == '-') {
  129. return SDL_ScanLong(text, count, radix, (long *)valuep);
  130. }
  131. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  132. text += 2;
  133. }
  134. for (;;) {
  135. int v;
  136. if (SDL_isdigit((unsigned char)*text)) {
  137. v = *text - '0';
  138. } else if (radix == 16 && SDL_isupperhex(*text)) {
  139. v = 10 + (*text - 'A');
  140. } else if (radix == 16 && SDL_islowerhex(*text)) {
  141. v = 10 + (*text - 'a');
  142. } else {
  143. break;
  144. }
  145. value *= radix;
  146. value += v;
  147. ++text;
  148. if (count > 0 && (text - textstart) == count) {
  149. break;
  150. }
  151. }
  152. if (valuep && text > textstart) {
  153. *valuep = value;
  154. }
  155. return text - textstart;
  156. }
  157. #endif
  158. #ifndef HAVE_VSSCANF
  159. static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
  160. {
  161. const char *textstart = text;
  162. uintptr_t value = 0;
  163. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  164. text += 2;
  165. }
  166. for (;;) {
  167. int v;
  168. if (SDL_isdigit((unsigned char)*text)) {
  169. v = *text - '0';
  170. } else if (radix == 16 && SDL_isupperhex(*text)) {
  171. v = 10 + (*text - 'A');
  172. } else if (radix == 16 && SDL_islowerhex(*text)) {
  173. v = 10 + (*text - 'a');
  174. } else {
  175. break;
  176. }
  177. value *= radix;
  178. value += v;
  179. ++text;
  180. }
  181. if (valuep && text > textstart) {
  182. *valuep = value;
  183. }
  184. return text - textstart;
  185. }
  186. #endif
  187. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  188. static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
  189. {
  190. const char *textstart = text;
  191. Sint64 value = 0;
  192. SDL_bool negative = SDL_FALSE;
  193. if (*text == '-') {
  194. negative = SDL_TRUE;
  195. ++text;
  196. }
  197. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  198. text += 2;
  199. }
  200. for (;;) {
  201. int v;
  202. if (SDL_isdigit((unsigned char)*text)) {
  203. v = *text - '0';
  204. } else if (radix == 16 && SDL_isupperhex(*text)) {
  205. v = 10 + (*text - 'A');
  206. } else if (radix == 16 && SDL_islowerhex(*text)) {
  207. v = 10 + (*text - 'a');
  208. } else {
  209. break;
  210. }
  211. value *= radix;
  212. value += v;
  213. ++text;
  214. if (count > 0 && (text - textstart) == count) {
  215. break;
  216. }
  217. }
  218. if (valuep && text > textstart) {
  219. if (negative && value) {
  220. *valuep = -value;
  221. } else {
  222. *valuep = value;
  223. }
  224. }
  225. return text - textstart;
  226. }
  227. #endif
  228. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
  229. static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
  230. {
  231. const char *textstart = text;
  232. Uint64 value = 0;
  233. if (*text == '-') {
  234. return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
  235. }
  236. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  237. text += 2;
  238. }
  239. for (;;) {
  240. int v;
  241. if (SDL_isdigit((unsigned char)*text)) {
  242. v = *text - '0';
  243. } else if (radix == 16 && SDL_isupperhex(*text)) {
  244. v = 10 + (*text - 'A');
  245. } else if (radix == 16 && SDL_islowerhex(*text)) {
  246. v = 10 + (*text - 'a');
  247. } else {
  248. break;
  249. }
  250. value *= radix;
  251. value += v;
  252. ++text;
  253. if (count > 0 && (text - textstart) == count) {
  254. break;
  255. }
  256. }
  257. if (valuep && text > textstart) {
  258. *valuep = value;
  259. }
  260. return text - textstart;
  261. }
  262. #endif
  263. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
  264. static size_t SDL_ScanFloat(const char *text, double *valuep)
  265. {
  266. const char *textstart = text;
  267. unsigned long lvalue = 0;
  268. double value = 0.0;
  269. SDL_bool negative = SDL_FALSE;
  270. if (*text == '-') {
  271. negative = SDL_TRUE;
  272. ++text;
  273. }
  274. text += SDL_ScanUnsignedLong(text, 0, 10, &lvalue);
  275. value += lvalue;
  276. if (*text == '.') {
  277. int mult = 10;
  278. ++text;
  279. while (SDL_isdigit((unsigned char)*text)) {
  280. lvalue = *text - '0';
  281. value += (double)lvalue / mult;
  282. mult *= 10;
  283. ++text;
  284. }
  285. }
  286. if (valuep && text > textstart) {
  287. if (negative && value) {
  288. *valuep = -value;
  289. } else {
  290. *valuep = value;
  291. }
  292. }
  293. return text - textstart;
  294. }
  295. #endif
  296. void *SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
  297. {
  298. #ifdef HAVE_MEMMOVE
  299. return memmove(dst, src, len);
  300. #else
  301. char *srcp = (char *)src;
  302. char *dstp = (char *)dst;
  303. if (src < dst) {
  304. srcp += len - 1;
  305. dstp += len - 1;
  306. while (len--) {
  307. *dstp-- = *srcp--;
  308. }
  309. } else {
  310. while (len--) {
  311. *dstp++ = *srcp++;
  312. }
  313. }
  314. return dst;
  315. #endif /* HAVE_MEMMOVE */
  316. }
  317. int SDL_memcmp(const void *s1, const void *s2, size_t len)
  318. {
  319. #ifdef __vita__
  320. /*
  321. Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
  322. But, both linux and bsd allow that, with an exception:
  323. zero length strings are always identical, so NULLs are never dereferenced.
  324. sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
  325. */
  326. if (len == 0) {
  327. return 0;
  328. }
  329. return sceClibMemcmp(s1, s2, len);
  330. #elif defined(HAVE_MEMCMP)
  331. return memcmp(s1, s2, len);
  332. #else
  333. char *s1p = (char *)s1;
  334. char *s2p = (char *)s2;
  335. while (len--) {
  336. if (*s1p != *s2p) {
  337. return *s1p - *s2p;
  338. }
  339. ++s1p;
  340. ++s2p;
  341. }
  342. return 0;
  343. #endif /* HAVE_MEMCMP */
  344. }
  345. size_t SDL_strlen(const char *string)
  346. {
  347. #ifdef HAVE_STRLEN
  348. return strlen(string);
  349. #else
  350. size_t len = 0;
  351. while (*string++) {
  352. ++len;
  353. }
  354. return len;
  355. #endif /* HAVE_STRLEN */
  356. }
  357. size_t SDL_wcslen(const wchar_t *string)
  358. {
  359. #ifdef HAVE_WCSLEN
  360. return wcslen(string);
  361. #else
  362. size_t len = 0;
  363. while (*string++) {
  364. ++len;
  365. }
  366. return len;
  367. #endif /* HAVE_WCSLEN */
  368. }
  369. size_t SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  370. {
  371. #ifdef HAVE_WCSLCPY
  372. return wcslcpy(dst, src, maxlen);
  373. #else
  374. size_t srclen = SDL_wcslen(src);
  375. if (maxlen > 0) {
  376. size_t len = SDL_min(srclen, maxlen - 1);
  377. SDL_memcpy(dst, src, len * sizeof(wchar_t));
  378. dst[len] = '\0';
  379. }
  380. return srclen;
  381. #endif /* HAVE_WCSLCPY */
  382. }
  383. size_t SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  384. {
  385. #ifdef HAVE_WCSLCAT
  386. return wcslcat(dst, src, maxlen);
  387. #else
  388. size_t dstlen = SDL_wcslen(dst);
  389. size_t srclen = SDL_wcslen(src);
  390. if (dstlen < maxlen) {
  391. SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
  392. }
  393. return dstlen + srclen;
  394. #endif /* HAVE_WCSLCAT */
  395. }
  396. wchar_t *SDL_wcsdup(const wchar_t *string)
  397. {
  398. size_t len = ((SDL_wcslen(string) + 1) * sizeof(wchar_t));
  399. wchar_t *newstr = (wchar_t *)SDL_malloc(len);
  400. if (newstr) {
  401. SDL_memcpy(newstr, string, len);
  402. }
  403. return newstr;
  404. }
  405. wchar_t *SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
  406. {
  407. #ifdef HAVE_WCSSTR
  408. return SDL_const_cast(wchar_t *, wcsstr(haystack, needle));
  409. #else
  410. size_t length = SDL_wcslen(needle);
  411. while (*haystack) {
  412. if (SDL_wcsncmp(haystack, needle, length) == 0) {
  413. return (wchar_t *)haystack;
  414. }
  415. ++haystack;
  416. }
  417. return NULL;
  418. #endif /* HAVE_WCSSTR */
  419. }
  420. int SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
  421. {
  422. #ifdef HAVE_WCSCMP
  423. return wcscmp(str1, str2);
  424. #else
  425. while (*str1 && *str2) {
  426. if (*str1 != *str2) {
  427. break;
  428. }
  429. ++str1;
  430. ++str2;
  431. }
  432. return *str1 - *str2;
  433. #endif /* HAVE_WCSCMP */
  434. }
  435. int SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
  436. {
  437. #ifdef HAVE_WCSNCMP
  438. return wcsncmp(str1, str2, maxlen);
  439. #else
  440. while (*str1 && *str2 && maxlen) {
  441. if (*str1 != *str2) {
  442. break;
  443. }
  444. ++str1;
  445. ++str2;
  446. --maxlen;
  447. }
  448. if (!maxlen) {
  449. return 0;
  450. }
  451. return *str1 - *str2;
  452. #endif /* HAVE_WCSNCMP */
  453. }
  454. int SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2)
  455. {
  456. #ifdef HAVE_WCSCASECMP
  457. return wcscasecmp(str1, str2);
  458. #elif defined(HAVE__WCSICMP)
  459. return _wcsicmp(str1, str2);
  460. #else
  461. wchar_t a = 0;
  462. wchar_t b = 0;
  463. while (*str1 && *str2) {
  464. /* FIXME: This doesn't actually support wide characters */
  465. if (*str1 >= 0x80 || *str2 >= 0x80) {
  466. a = *str1;
  467. b = *str2;
  468. } else {
  469. a = (wchar_t)SDL_toupper((unsigned char)*str1);
  470. b = (wchar_t)SDL_toupper((unsigned char)*str2);
  471. }
  472. if (a != b) {
  473. break;
  474. }
  475. ++str1;
  476. ++str2;
  477. }
  478. /* FIXME: This doesn't actually support wide characters */
  479. if (*str1 >= 0x80 || *str2 >= 0x80) {
  480. a = *str1;
  481. b = *str2;
  482. } else {
  483. a = (wchar_t)SDL_toupper((unsigned char)*str1);
  484. b = (wchar_t)SDL_toupper((unsigned char)*str2);
  485. }
  486. return (int)((unsigned int)a - (unsigned int)b);
  487. #endif /* HAVE__WCSICMP */
  488. }
  489. int SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
  490. {
  491. #ifdef HAVE_WCSNCASECMP
  492. return wcsncasecmp(str1, str2, maxlen);
  493. #elif defined(HAVE__WCSNICMP)
  494. return _wcsnicmp(str1, str2, maxlen);
  495. #else
  496. wchar_t a = 0;
  497. wchar_t b = 0;
  498. while (*str1 && *str2 && maxlen) {
  499. /* FIXME: This doesn't actually support wide characters */
  500. if (*str1 >= 0x80 || *str2 >= 0x80) {
  501. a = *str1;
  502. b = *str2;
  503. } else {
  504. a = (wchar_t)SDL_toupper((unsigned char)*str1);
  505. b = (wchar_t)SDL_toupper((unsigned char)*str2);
  506. }
  507. if (a != b) {
  508. break;
  509. }
  510. ++str1;
  511. ++str2;
  512. --maxlen;
  513. }
  514. if (maxlen == 0) {
  515. return 0;
  516. } else {
  517. /* FIXME: This doesn't actually support wide characters */
  518. if (*str1 >= 0x80 || *str2 >= 0x80) {
  519. a = *str1;
  520. b = *str2;
  521. } else {
  522. a = (wchar_t)SDL_toupper((unsigned char)*str1);
  523. b = (wchar_t)SDL_toupper((unsigned char)*str2);
  524. }
  525. return (int)((unsigned int)a - (unsigned int)b);
  526. }
  527. #endif /* HAVE__WCSNICMP */
  528. }
  529. long SDL_wcstol(const wchar_t *string, wchar_t **endp, int base)
  530. {
  531. #ifdef HAVE_WCSTOL
  532. return wcstol(string, endp, base);
  533. #else
  534. size_t len;
  535. long value = 0;
  536. if (!base) {
  537. if ((SDL_wcslen(string) > 2) && (SDL_wcsncmp(string, L"0x", 2) == 0)) {
  538. base = 16;
  539. } else {
  540. base = 10;
  541. }
  542. }
  543. len = SDL_ScanLongW(string, 0, base, &value);
  544. if (endp) {
  545. *endp = (wchar_t *)string + len;
  546. }
  547. return value;
  548. #endif /* HAVE_WCSTOL */
  549. }
  550. size_t SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  551. {
  552. #ifdef HAVE_STRLCPY
  553. return strlcpy(dst, src, maxlen);
  554. #else
  555. size_t srclen = SDL_strlen(src);
  556. if (maxlen > 0) {
  557. size_t len = SDL_min(srclen, maxlen - 1);
  558. SDL_memcpy(dst, src, len);
  559. dst[len] = '\0';
  560. }
  561. return srclen;
  562. #endif /* HAVE_STRLCPY */
  563. }
  564. size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
  565. {
  566. size_t src_bytes = SDL_strlen(src);
  567. size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
  568. size_t i = 0;
  569. size_t trailing_bytes = 0;
  570. if (bytes) {
  571. unsigned char c = (unsigned char)src[bytes - 1];
  572. if (UTF8_IsLeadByte(c)) {
  573. --bytes;
  574. } else if (UTF8_IsTrailingByte(c)) {
  575. for (i = bytes - 1; i != 0; --i) {
  576. c = (unsigned char)src[i];
  577. trailing_bytes = UTF8_GetTrailingBytes(c);
  578. if (trailing_bytes) {
  579. if ((bytes - i) != (trailing_bytes + 1)) {
  580. bytes = i;
  581. }
  582. break;
  583. }
  584. }
  585. }
  586. SDL_memcpy(dst, src, bytes);
  587. }
  588. dst[bytes] = '\0';
  589. return bytes;
  590. }
  591. size_t SDL_utf8strlen(const char *str)
  592. {
  593. size_t retval = 0;
  594. const char *p = str;
  595. unsigned char ch;
  596. while ((ch = *(p++)) != 0) {
  597. /* if top two bits are 1 and 0, it's a continuation byte. */
  598. if ((ch & 0xc0) != 0x80) {
  599. retval++;
  600. }
  601. }
  602. return retval;
  603. }
  604. size_t SDL_utf8strnlen(const char *str, size_t bytes)
  605. {
  606. size_t retval = 0;
  607. const char *p = str;
  608. unsigned char ch;
  609. while ((ch = *(p++)) != 0 && bytes-- > 0) {
  610. /* if top two bits are 1 and 0, it's a continuation byte. */
  611. if ((ch & 0xc0) != 0x80) {
  612. retval++;
  613. }
  614. }
  615. return retval;
  616. }
  617. size_t SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  618. {
  619. #ifdef HAVE_STRLCAT
  620. return strlcat(dst, src, maxlen);
  621. #else
  622. size_t dstlen = SDL_strlen(dst);
  623. size_t srclen = SDL_strlen(src);
  624. if (dstlen < maxlen) {
  625. SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
  626. }
  627. return dstlen + srclen;
  628. #endif /* HAVE_STRLCAT */
  629. }
  630. char *SDL_strdup(const char *string)
  631. {
  632. size_t len = SDL_strlen(string) + 1;
  633. char *newstr = (char *)SDL_malloc(len);
  634. if (newstr) {
  635. SDL_memcpy(newstr, string, len);
  636. }
  637. return newstr;
  638. }
  639. char *SDL_strndup(const char *string, size_t maxlen)
  640. {
  641. size_t len = SDL_min(SDL_strlen(string), maxlen) + 1;
  642. char *newstr = (char *)SDL_malloc(len);
  643. if (newstr) {
  644. SDL_memcpy(newstr, string, len);
  645. newstr[len - 1] = '\0';
  646. }
  647. return newstr;
  648. }
  649. char *SDL_strrev(char *string)
  650. {
  651. #ifdef HAVE__STRREV
  652. return _strrev(string);
  653. #else
  654. size_t len = SDL_strlen(string);
  655. char *a = &string[0];
  656. char *b = &string[len - 1];
  657. len /= 2;
  658. while (len--) {
  659. char c = *a; /* NOLINT(clang-analyzer-core.uninitialized.Assign) */
  660. *a++ = *b;
  661. *b-- = c;
  662. }
  663. return string;
  664. #endif /* HAVE__STRREV */
  665. }
  666. char *SDL_strupr(char *string)
  667. {
  668. #ifdef HAVE__STRUPR
  669. return _strupr(string);
  670. #else
  671. char *bufp = string;
  672. while (*bufp) {
  673. *bufp = (char)SDL_toupper((unsigned char)*bufp);
  674. ++bufp;
  675. }
  676. return string;
  677. #endif /* HAVE__STRUPR */
  678. }
  679. char *SDL_strlwr(char *string)
  680. {
  681. #ifdef HAVE__STRLWR
  682. return _strlwr(string);
  683. #else
  684. char *bufp = string;
  685. while (*bufp) {
  686. *bufp = (char)SDL_tolower((unsigned char)*bufp);
  687. ++bufp;
  688. }
  689. return string;
  690. #endif /* HAVE__STRLWR */
  691. }
  692. char *SDL_strchr(const char *string, int c)
  693. {
  694. #ifdef HAVE_STRCHR
  695. return SDL_const_cast(char *, strchr(string, c));
  696. #elif defined(HAVE_INDEX)
  697. return SDL_const_cast(char *, index(string, c));
  698. #else
  699. while (*string) {
  700. if (*string == c) {
  701. return (char *)string;
  702. }
  703. ++string;
  704. }
  705. if (c == '\0') {
  706. return (char *)string;
  707. }
  708. return NULL;
  709. #endif /* HAVE_STRCHR */
  710. }
  711. char *SDL_strrchr(const char *string, int c)
  712. {
  713. #ifdef HAVE_STRRCHR
  714. return SDL_const_cast(char *, strrchr(string, c));
  715. #elif defined(HAVE_RINDEX)
  716. return SDL_const_cast(char *, rindex(string, c));
  717. #else
  718. const char *bufp = string + SDL_strlen(string);
  719. while (bufp >= string) {
  720. if (*bufp == c) {
  721. return (char *)bufp;
  722. }
  723. --bufp;
  724. }
  725. return NULL;
  726. #endif /* HAVE_STRRCHR */
  727. }
  728. char *SDL_strstr(const char *haystack, const char *needle)
  729. {
  730. #ifdef HAVE_STRSTR
  731. return SDL_const_cast(char *, strstr(haystack, needle));
  732. #else
  733. size_t length = SDL_strlen(needle);
  734. while (*haystack) {
  735. if (SDL_strncmp(haystack, needle, length) == 0) {
  736. return (char *)haystack;
  737. }
  738. ++haystack;
  739. }
  740. return NULL;
  741. #endif /* HAVE_STRSTR */
  742. }
  743. char *SDL_strcasestr(const char *haystack, const char *needle)
  744. {
  745. #ifdef HAVE_STRCASESTR
  746. return SDL_const_cast(char *, strcasestr(haystack, needle));
  747. #else
  748. size_t length = SDL_strlen(needle);
  749. while (*haystack) {
  750. if (SDL_strncasecmp(haystack, needle, length) == 0) {
  751. return (char *)haystack;
  752. }
  753. ++haystack;
  754. }
  755. return NULL;
  756. #endif /* HAVE_STRCASESTR */
  757. }
  758. #if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
  759. !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
  760. static const char ntoa_table[] = {
  761. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  762. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  763. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  764. 'U', 'V', 'W', 'X', 'Y', 'Z'
  765. };
  766. #endif /* ntoa() conversion table */
  767. char *SDL_itoa(int value, char *string, int radix)
  768. {
  769. #ifdef HAVE_ITOA
  770. return itoa(value, string, radix);
  771. #else
  772. return SDL_ltoa((long)value, string, radix);
  773. #endif /* HAVE_ITOA */
  774. }
  775. char *SDL_uitoa(unsigned int value, char *string, int radix)
  776. {
  777. #ifdef HAVE__UITOA
  778. return _uitoa(value, string, radix);
  779. #else
  780. return SDL_ultoa((unsigned long)value, string, radix);
  781. #endif /* HAVE__UITOA */
  782. }
  783. char *SDL_ltoa(long value, char *string, int radix)
  784. {
  785. #ifdef HAVE__LTOA
  786. return _ltoa(value, string, radix);
  787. #else
  788. char *bufp = string;
  789. if (value < 0) {
  790. *bufp++ = '-';
  791. SDL_ultoa(-value, bufp, radix);
  792. } else {
  793. SDL_ultoa(value, bufp, radix);
  794. }
  795. return string;
  796. #endif /* HAVE__LTOA */
  797. }
  798. char *SDL_ultoa(unsigned long value, char *string, int radix)
  799. {
  800. #ifdef HAVE__ULTOA
  801. return _ultoa(value, string, radix);
  802. #else
  803. char *bufp = string;
  804. if (value) {
  805. while (value > 0) {
  806. *bufp++ = ntoa_table[value % radix];
  807. value /= radix;
  808. }
  809. } else {
  810. *bufp++ = '0';
  811. }
  812. *bufp = '\0';
  813. /* The numbers went into the string backwards. :) */
  814. SDL_strrev(string);
  815. return string;
  816. #endif /* HAVE__ULTOA */
  817. }
  818. char *SDL_lltoa(Sint64 value, char *string, int radix)
  819. {
  820. #ifdef HAVE__I64TOA
  821. return _i64toa(value, string, radix);
  822. #else
  823. char *bufp = string;
  824. if (value < 0) {
  825. *bufp++ = '-';
  826. SDL_ulltoa(-value, bufp, radix);
  827. } else {
  828. SDL_ulltoa(value, bufp, radix);
  829. }
  830. return string;
  831. #endif /* HAVE__I64TOA */
  832. }
  833. char *SDL_ulltoa(Uint64 value, char *string, int radix)
  834. {
  835. #ifdef HAVE__UI64TOA
  836. return _ui64toa(value, string, radix);
  837. #else
  838. char *bufp = string;
  839. if (value) {
  840. while (value > 0) {
  841. *bufp++ = ntoa_table[value % radix];
  842. value /= radix;
  843. }
  844. } else {
  845. *bufp++ = '0';
  846. }
  847. *bufp = '\0';
  848. /* The numbers went into the string backwards. :) */
  849. SDL_strrev(string);
  850. return string;
  851. #endif /* HAVE__UI64TOA */
  852. }
  853. int SDL_atoi(const char *string)
  854. {
  855. #ifdef HAVE_ATOI
  856. return atoi(string);
  857. #else
  858. return SDL_strtol(string, NULL, 10);
  859. #endif /* HAVE_ATOI */
  860. }
  861. double SDL_atof(const char *string)
  862. {
  863. #ifdef HAVE_ATOF
  864. return atof(string);
  865. #else
  866. return SDL_strtod(string, NULL);
  867. #endif /* HAVE_ATOF */
  868. }
  869. long SDL_strtol(const char *string, char **endp, int base)
  870. {
  871. #ifdef HAVE_STRTOL
  872. return strtol(string, endp, base);
  873. #else
  874. size_t len;
  875. long value = 0;
  876. if (!base) {
  877. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  878. base = 16;
  879. } else {
  880. base = 10;
  881. }
  882. }
  883. len = SDL_ScanLong(string, 0, base, &value);
  884. if (endp) {
  885. *endp = (char *)string + len;
  886. }
  887. return value;
  888. #endif /* HAVE_STRTOL */
  889. }
  890. unsigned long
  891. SDL_strtoul(const char *string, char **endp, int base)
  892. {
  893. #ifdef HAVE_STRTOUL
  894. return strtoul(string, endp, base);
  895. #else
  896. size_t len;
  897. unsigned long value = 0;
  898. if (!base) {
  899. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  900. base = 16;
  901. } else {
  902. base = 10;
  903. }
  904. }
  905. len = SDL_ScanUnsignedLong(string, 0, base, &value);
  906. if (endp) {
  907. *endp = (char *)string + len;
  908. }
  909. return value;
  910. #endif /* HAVE_STRTOUL */
  911. }
  912. Sint64 SDL_strtoll(const char *string, char **endp, int base)
  913. {
  914. #ifdef HAVE_STRTOLL
  915. return strtoll(string, endp, base);
  916. #else
  917. size_t len;
  918. Sint64 value = 0;
  919. if (!base) {
  920. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  921. base = 16;
  922. } else {
  923. base = 10;
  924. }
  925. }
  926. len = SDL_ScanLongLong(string, 0, base, &value);
  927. if (endp) {
  928. *endp = (char *)string + len;
  929. }
  930. return value;
  931. #endif /* HAVE_STRTOLL */
  932. }
  933. Uint64 SDL_strtoull(const char *string, char **endp, int base)
  934. {
  935. #ifdef HAVE_STRTOULL
  936. return strtoull(string, endp, base);
  937. #else
  938. size_t len;
  939. Uint64 value = 0;
  940. if (!base) {
  941. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  942. base = 16;
  943. } else {
  944. base = 10;
  945. }
  946. }
  947. len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
  948. if (endp) {
  949. *endp = (char *)string + len;
  950. }
  951. return value;
  952. #endif /* HAVE_STRTOULL */
  953. }
  954. double
  955. SDL_strtod(const char *string, char **endp)
  956. {
  957. #ifdef HAVE_STRTOD
  958. return strtod(string, endp);
  959. #else
  960. size_t len;
  961. double value = 0.0;
  962. len = SDL_ScanFloat(string, &value);
  963. if (endp) {
  964. *endp = (char *)string + len;
  965. }
  966. return value;
  967. #endif /* HAVE_STRTOD */
  968. }
  969. int SDL_strcmp(const char *str1, const char *str2)
  970. {
  971. #ifdef HAVE_STRCMP
  972. return strcmp(str1, str2);
  973. #else
  974. int result;
  975. while (1) {
  976. result = ((unsigned char)*str1 - (unsigned char)*str2);
  977. if (result != 0 || (*str1 == '\0' /* && *str2 == '\0'*/)) {
  978. break;
  979. }
  980. ++str1;
  981. ++str2;
  982. }
  983. return result;
  984. #endif /* HAVE_STRCMP */
  985. }
  986. int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
  987. {
  988. #ifdef HAVE_STRNCMP
  989. return strncmp(str1, str2, maxlen);
  990. #else
  991. int result = 0;
  992. while (maxlen) {
  993. result = (int)(unsigned char)*str1 - (unsigned char)*str2;
  994. if (result != 0 || *str1 == '\0' /* && *str2 == '\0'*/) {
  995. break;
  996. }
  997. ++str1;
  998. ++str2;
  999. --maxlen;
  1000. }
  1001. return result;
  1002. #endif /* HAVE_STRNCMP */
  1003. }
  1004. int SDL_strcasecmp(const char *str1, const char *str2)
  1005. {
  1006. #ifdef HAVE_STRCASECMP
  1007. return strcasecmp(str1, str2);
  1008. #elif defined(HAVE__STRICMP)
  1009. return _stricmp(str1, str2);
  1010. #else
  1011. int a, b, result;
  1012. while (1) {
  1013. a = SDL_toupper((unsigned char)*str1);
  1014. b = SDL_toupper((unsigned char)*str2);
  1015. result = a - b;
  1016. if (result != 0 || a == 0 /*&& b == 0*/) {
  1017. break;
  1018. }
  1019. ++str1;
  1020. ++str2;
  1021. }
  1022. return result;
  1023. #endif /* HAVE_STRCASECMP */
  1024. }
  1025. int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
  1026. {
  1027. #ifdef HAVE_STRNCASECMP
  1028. return strncasecmp(str1, str2, maxlen);
  1029. #elif defined(HAVE__STRNICMP)
  1030. return _strnicmp(str1, str2, maxlen);
  1031. #else
  1032. int a, b, result = 0;
  1033. while (maxlen) {
  1034. a = SDL_tolower((unsigned char)*str1);
  1035. b = SDL_tolower((unsigned char)*str2);
  1036. result = a - b;
  1037. if (result != 0 || a == 0 /*&& b == 0*/) {
  1038. break;
  1039. }
  1040. ++str1;
  1041. ++str2;
  1042. --maxlen;
  1043. }
  1044. return result;
  1045. #endif /* HAVE_STRNCASECMP */
  1046. }
  1047. int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  1048. {
  1049. int rc;
  1050. va_list ap;
  1051. va_start(ap, fmt);
  1052. rc = SDL_vsscanf(text, fmt, ap);
  1053. va_end(ap);
  1054. return rc;
  1055. }
  1056. #ifdef HAVE_VSSCANF
  1057. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1058. {
  1059. return vsscanf(text, fmt, ap);
  1060. }
  1061. #else
  1062. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1063. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1064. {
  1065. int retval = 0;
  1066. if (text == NULL || !*text) {
  1067. return -1;
  1068. }
  1069. while (*fmt) {
  1070. if (*fmt == ' ') {
  1071. while (SDL_isspace((unsigned char)*text)) {
  1072. ++text;
  1073. }
  1074. ++fmt;
  1075. continue;
  1076. }
  1077. if (*fmt == '%') {
  1078. SDL_bool done = SDL_FALSE;
  1079. long count = 0;
  1080. int radix = 10;
  1081. enum
  1082. {
  1083. DO_SHORT,
  1084. DO_INT,
  1085. DO_LONG,
  1086. DO_LONGLONG,
  1087. DO_SIZE_T
  1088. } inttype = DO_INT;
  1089. size_t advance;
  1090. SDL_bool suppress = SDL_FALSE;
  1091. ++fmt;
  1092. if (*fmt == '%') {
  1093. if (*text == '%') {
  1094. ++text;
  1095. ++fmt;
  1096. continue;
  1097. }
  1098. break;
  1099. }
  1100. if (*fmt == '*') {
  1101. suppress = SDL_TRUE;
  1102. ++fmt;
  1103. }
  1104. fmt += SDL_ScanLong(fmt, 0, 10, &count);
  1105. if (*fmt == 'c') {
  1106. if (!count) {
  1107. count = 1;
  1108. }
  1109. if (suppress) {
  1110. while (count--) {
  1111. ++text;
  1112. }
  1113. } else {
  1114. char *valuep = va_arg(ap, char *);
  1115. while (count--) {
  1116. *valuep++ = *text++;
  1117. }
  1118. ++retval;
  1119. }
  1120. continue;
  1121. }
  1122. while (SDL_isspace((unsigned char)*text)) {
  1123. ++text;
  1124. }
  1125. /* FIXME: implement more of the format specifiers */
  1126. while (!done) {
  1127. switch (*fmt) {
  1128. case '*':
  1129. suppress = SDL_TRUE;
  1130. break;
  1131. case 'h':
  1132. if (inttype > DO_SHORT) {
  1133. ++inttype;
  1134. }
  1135. break;
  1136. case 'l':
  1137. if (inttype < DO_LONGLONG) {
  1138. ++inttype;
  1139. }
  1140. break;
  1141. case 'I':
  1142. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1143. fmt += 2;
  1144. inttype = DO_LONGLONG;
  1145. }
  1146. break;
  1147. case 'z':
  1148. inttype = DO_SIZE_T;
  1149. break;
  1150. case 'i':
  1151. {
  1152. int index = 0;
  1153. if (text[index] == '-') {
  1154. ++index;
  1155. }
  1156. if (text[index] == '0') {
  1157. if (SDL_tolower((unsigned char)text[index + 1]) == 'x') {
  1158. radix = 16;
  1159. } else {
  1160. radix = 8;
  1161. }
  1162. }
  1163. }
  1164. SDL_FALLTHROUGH;
  1165. case 'd':
  1166. if (inttype == DO_LONGLONG) {
  1167. Sint64 value = 0;
  1168. advance = SDL_ScanLongLong(text, count, radix, &value);
  1169. text += advance;
  1170. if (advance && !suppress) {
  1171. Sint64 *valuep = va_arg(ap, Sint64 *);
  1172. *valuep = value;
  1173. ++retval;
  1174. }
  1175. } else if (inttype == DO_SIZE_T) {
  1176. Sint64 value = 0;
  1177. advance = SDL_ScanLongLong(text, count, radix, &value);
  1178. text += advance;
  1179. if (advance && !suppress) {
  1180. size_t *valuep = va_arg(ap, size_t *);
  1181. *valuep = (size_t)value;
  1182. ++retval;
  1183. }
  1184. } else {
  1185. long value = 0;
  1186. advance = SDL_ScanLong(text, count, radix, &value);
  1187. text += advance;
  1188. if (advance && !suppress) {
  1189. switch (inttype) {
  1190. case DO_SHORT:
  1191. {
  1192. short *valuep = va_arg(ap, short *);
  1193. *valuep = (short)value;
  1194. } break;
  1195. case DO_INT:
  1196. {
  1197. int *valuep = va_arg(ap, int *);
  1198. *valuep = (int)value;
  1199. } break;
  1200. case DO_LONG:
  1201. {
  1202. long *valuep = va_arg(ap, long *);
  1203. *valuep = value;
  1204. } break;
  1205. case DO_LONGLONG:
  1206. case DO_SIZE_T:
  1207. /* Handled above */
  1208. break;
  1209. }
  1210. ++retval;
  1211. }
  1212. }
  1213. done = SDL_TRUE;
  1214. break;
  1215. case 'o':
  1216. if (radix == 10) {
  1217. radix = 8;
  1218. }
  1219. SDL_FALLTHROUGH;
  1220. case 'x':
  1221. case 'X':
  1222. if (radix == 10) {
  1223. radix = 16;
  1224. }
  1225. SDL_FALLTHROUGH;
  1226. case 'u':
  1227. if (inttype == DO_LONGLONG) {
  1228. Uint64 value = 0;
  1229. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1230. text += advance;
  1231. if (advance && !suppress) {
  1232. Uint64 *valuep = va_arg(ap, Uint64 *);
  1233. *valuep = value;
  1234. ++retval;
  1235. }
  1236. } else if (inttype == DO_SIZE_T) {
  1237. Uint64 value = 0;
  1238. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1239. text += advance;
  1240. if (advance && !suppress) {
  1241. size_t *valuep = va_arg(ap, size_t *);
  1242. *valuep = (size_t)value;
  1243. ++retval;
  1244. }
  1245. } else {
  1246. unsigned long value = 0;
  1247. advance = SDL_ScanUnsignedLong(text, count, radix, &value);
  1248. text += advance;
  1249. if (advance && !suppress) {
  1250. switch (inttype) {
  1251. case DO_SHORT:
  1252. {
  1253. short *valuep = va_arg(ap, short *);
  1254. *valuep = (short)value;
  1255. } break;
  1256. case DO_INT:
  1257. {
  1258. int *valuep = va_arg(ap, int *);
  1259. *valuep = (int)value;
  1260. } break;
  1261. case DO_LONG:
  1262. {
  1263. long *valuep = va_arg(ap, long *);
  1264. *valuep = value;
  1265. } break;
  1266. case DO_LONGLONG:
  1267. case DO_SIZE_T:
  1268. /* Handled above */
  1269. break;
  1270. }
  1271. ++retval;
  1272. }
  1273. }
  1274. done = SDL_TRUE;
  1275. break;
  1276. case 'p':
  1277. {
  1278. uintptr_t value = 0;
  1279. advance = SDL_ScanUintPtrT(text, 16, &value);
  1280. text += advance;
  1281. if (advance && !suppress) {
  1282. void **valuep = va_arg(ap, void **);
  1283. *valuep = (void *)value;
  1284. ++retval;
  1285. }
  1286. }
  1287. done = SDL_TRUE;
  1288. break;
  1289. case 'f':
  1290. {
  1291. double value = 0.0;
  1292. advance = SDL_ScanFloat(text, &value);
  1293. text += advance;
  1294. if (advance && !suppress) {
  1295. float *valuep = va_arg(ap, float *);
  1296. *valuep = (float)value;
  1297. ++retval;
  1298. }
  1299. }
  1300. done = SDL_TRUE;
  1301. break;
  1302. case 's':
  1303. if (suppress) {
  1304. while (!SDL_isspace((unsigned char)*text)) {
  1305. ++text;
  1306. if (count) {
  1307. if (--count == 0) {
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. } else {
  1313. char *valuep = va_arg(ap, char *);
  1314. while (!SDL_isspace((unsigned char)*text)) {
  1315. *valuep++ = *text++;
  1316. if (count) {
  1317. if (--count == 0) {
  1318. break;
  1319. }
  1320. }
  1321. }
  1322. *valuep = '\0';
  1323. ++retval;
  1324. }
  1325. done = SDL_TRUE;
  1326. break;
  1327. default:
  1328. done = SDL_TRUE;
  1329. break;
  1330. }
  1331. ++fmt;
  1332. }
  1333. continue;
  1334. }
  1335. if (*text == *fmt) {
  1336. ++text;
  1337. ++fmt;
  1338. continue;
  1339. }
  1340. /* Text didn't match format specifier */
  1341. break;
  1342. }
  1343. return retval;
  1344. }
  1345. #endif /* HAVE_VSSCANF */
  1346. int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1347. {
  1348. va_list ap;
  1349. int retval;
  1350. va_start(ap, fmt);
  1351. retval = SDL_vsnprintf(text, maxlen, fmt, ap);
  1352. va_end(ap);
  1353. return retval;
  1354. }
  1355. int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
  1356. {
  1357. va_list ap;
  1358. int retval;
  1359. va_start(ap, fmt);
  1360. retval = SDL_vswprintf(text, maxlen, fmt, ap);
  1361. va_end(ap);
  1362. return retval;
  1363. }
  1364. #if defined(HAVE_LIBC) && defined(__WATCOMC__)
  1365. /* _vsnprintf() doesn't ensure nul termination */
  1366. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1367. {
  1368. int retval;
  1369. if (!fmt) {
  1370. fmt = "";
  1371. }
  1372. retval = _vsnprintf(text, maxlen, fmt, ap);
  1373. if (maxlen > 0) {
  1374. text[maxlen - 1] = '\0';
  1375. }
  1376. if (retval < 0) {
  1377. retval = (int)maxlen;
  1378. }
  1379. return retval;
  1380. }
  1381. #elif defined(HAVE_VSNPRINTF)
  1382. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1383. {
  1384. if (!fmt) {
  1385. fmt = "";
  1386. }
  1387. return vsnprintf(text, maxlen, fmt, ap);
  1388. }
  1389. #else
  1390. #define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
  1391. /* FIXME: implement more of the format specifiers */
  1392. typedef enum
  1393. {
  1394. SDL_CASE_NOCHANGE,
  1395. SDL_CASE_LOWER,
  1396. SDL_CASE_UPPER
  1397. } SDL_letter_case;
  1398. typedef struct
  1399. {
  1400. SDL_bool left_justify; /* for now: ignored. */
  1401. SDL_bool force_sign;
  1402. SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */
  1403. SDL_bool pad_zeroes;
  1404. SDL_letter_case force_case;
  1405. int width;
  1406. int radix;
  1407. int precision;
  1408. } SDL_FormatInfo;
  1409. static size_t SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
  1410. {
  1411. size_t length = 0;
  1412. size_t slen, sz;
  1413. if (string == NULL) {
  1414. string = "(null)";
  1415. }
  1416. sz = SDL_strlen(string);
  1417. if (info && info->width > 0 && (size_t)info->width > sz) {
  1418. const char fill = info->pad_zeroes ? '0' : ' ';
  1419. size_t width = info->width - sz;
  1420. size_t filllen;
  1421. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1422. width += sz - (size_t)info->precision;
  1423. }
  1424. filllen = SDL_min(width, maxlen);
  1425. SDL_memset(text, fill, filllen);
  1426. text += filllen;
  1427. maxlen -= filllen;
  1428. length += width;
  1429. }
  1430. SDL_strlcpy(text, string, maxlen);
  1431. length += sz;
  1432. if (info) {
  1433. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1434. slen = (size_t)info->precision;
  1435. if (slen < maxlen) {
  1436. text[slen] = '\0';
  1437. }
  1438. length -= (sz - slen);
  1439. }
  1440. if (maxlen > 1) {
  1441. if (info->force_case == SDL_CASE_LOWER) {
  1442. SDL_strlwr(text);
  1443. } else if (info->force_case == SDL_CASE_UPPER) {
  1444. SDL_strupr(text);
  1445. }
  1446. }
  1447. }
  1448. return length;
  1449. }
  1450. static size_t SDL_PrintStringW(char *text, size_t maxlen, SDL_FormatInfo *info, const wchar_t *wide_string)
  1451. {
  1452. size_t length = 0;
  1453. if (wide_string) {
  1454. char *string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)(wide_string), (SDL_wcslen(wide_string) + 1) * sizeof(*wide_string));
  1455. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, string);
  1456. SDL_free(string);
  1457. } else {
  1458. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, NULL);
  1459. }
  1460. return length;
  1461. }
  1462. static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
  1463. { /* left-pad num with zeroes. */
  1464. size_t sz, pad, have_sign;
  1465. if (info == NULL) {
  1466. return;
  1467. }
  1468. have_sign = 0;
  1469. if (*num == '-' || *num == '+') {
  1470. have_sign = 1;
  1471. ++num;
  1472. --maxlen;
  1473. }
  1474. sz = SDL_strlen(num);
  1475. if (info->precision > 0 && sz < (size_t)info->precision) {
  1476. pad = (size_t)info->precision - sz;
  1477. if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
  1478. SDL_memmove(num + pad, num, sz + 1);
  1479. SDL_memset(num, '0', pad);
  1480. }
  1481. }
  1482. info->precision = -1; /* so that SDL_PrintString() doesn't make a mess. */
  1483. if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
  1484. /* handle here: spaces are added before the sign
  1485. but zeroes must be placed _after_ the sign. */
  1486. /* sz hasn't changed: we ignore pad_zeroes if a precision is given. */
  1487. pad = (size_t)info->width - sz - have_sign;
  1488. if (pad + sz + 1 <= maxlen) {
  1489. SDL_memmove(num + pad, num, sz + 1);
  1490. SDL_memset(num, '0', pad);
  1491. }
  1492. info->width = 0; /* so that SDL_PrintString() doesn't make a mess. */
  1493. }
  1494. }
  1495. static size_t SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
  1496. {
  1497. char num[130], *p = num;
  1498. if (info->force_sign && value >= 0L) {
  1499. *p++ = '+';
  1500. }
  1501. SDL_ltoa(value, p, info ? info->radix : 10);
  1502. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1503. return SDL_PrintString(text, maxlen, info, num);
  1504. }
  1505. static size_t SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
  1506. {
  1507. char num[130];
  1508. SDL_ultoa(value, num, info ? info->radix : 10);
  1509. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1510. return SDL_PrintString(text, maxlen, info, num);
  1511. }
  1512. static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
  1513. {
  1514. char num[130], *p = num;
  1515. if (info->force_sign && value >= (Sint64)0) {
  1516. *p++ = '+';
  1517. }
  1518. SDL_lltoa(value, p, info ? info->radix : 10);
  1519. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1520. return SDL_PrintString(text, maxlen, info, num);
  1521. }
  1522. static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
  1523. {
  1524. char num[130];
  1525. SDL_ulltoa(value, num, info ? info->radix : 10);
  1526. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1527. return SDL_PrintString(text, maxlen, info, num);
  1528. }
  1529. static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg, SDL_bool g)
  1530. {
  1531. char num[327];
  1532. size_t length = 0;
  1533. size_t integer_length;
  1534. int precision = info->precision;
  1535. /* This isn't especially accurate, but hey, it's easy. :) */
  1536. Uint64 value;
  1537. if (arg < 0) {
  1538. num[length++] = '-';
  1539. arg = -arg;
  1540. } else if (info->force_sign) {
  1541. num[length++] = '+';
  1542. }
  1543. value = (Uint64)arg;
  1544. integer_length = SDL_PrintUnsignedLongLong(&num[length], sizeof(num) - length, NULL, value);
  1545. length += integer_length;
  1546. arg -= value;
  1547. if (precision < 0) {
  1548. precision = 6;
  1549. }
  1550. if (g) {
  1551. /* The precision includes the integer portion */
  1552. precision -= SDL_min((int)integer_length, precision);
  1553. }
  1554. if (info->force_type || precision > 0) {
  1555. const char decimal_separator = '.';
  1556. double integer_value;
  1557. SDL_assert(length < sizeof(num));
  1558. num[length++] = decimal_separator;
  1559. while (precision > 1) {
  1560. arg *= 10.0;
  1561. arg = SDL_modf(arg, &integer_value);
  1562. SDL_assert(length < sizeof(num));
  1563. num[length++] = '0' + (char)integer_value;
  1564. --precision;
  1565. }
  1566. if (precision == 1) {
  1567. arg *= 10.0;
  1568. integer_value = SDL_round(arg);
  1569. if (integer_value == 10.0) {
  1570. /* Carry the one... */
  1571. size_t i;
  1572. for (i = length; i--; ) {
  1573. if (num[i] == decimal_separator) {
  1574. continue;
  1575. }
  1576. if (num[i] == '9') {
  1577. num[i] = '0';
  1578. } else {
  1579. ++num[i];
  1580. break;
  1581. }
  1582. }
  1583. if (i == 0 || num[i] == '-' || num[i] == '+') {
  1584. SDL_memmove(&num[i+1], &num[i], length - i);
  1585. num[i] = '1';
  1586. ++length;
  1587. }
  1588. SDL_assert(length < sizeof(num));
  1589. num[length++] = '0';
  1590. } else {
  1591. SDL_assert(length < sizeof(num));
  1592. num[length++] = '0' + (char)integer_value;
  1593. }
  1594. }
  1595. if (g) {
  1596. /* Trim trailing zeroes and decimal separator */
  1597. size_t i;
  1598. for (i = length - 1; num[i] != decimal_separator; --i) {
  1599. if (num[i] == '0') {
  1600. --length;
  1601. } else {
  1602. break;
  1603. }
  1604. }
  1605. if (num[i] == decimal_separator) {
  1606. --length;
  1607. }
  1608. }
  1609. }
  1610. num[length] = '\0';
  1611. info->precision = -1;
  1612. length = SDL_PrintString(text, maxlen, info, num);
  1613. if (info->width > 0 && (size_t)info->width > length) {
  1614. const char fill = info->pad_zeroes ? '0' : ' ';
  1615. size_t width = info->width - length;
  1616. size_t filllen, movelen;
  1617. filllen = SDL_min(width, maxlen);
  1618. movelen = SDL_min(length, (maxlen - filllen));
  1619. SDL_memmove(&text[filllen], text, movelen);
  1620. SDL_memset(text, fill, filllen);
  1621. length += width;
  1622. }
  1623. return length;
  1624. }
  1625. static size_t SDL_PrintPointer(char *text, size_t maxlen, SDL_FormatInfo *info, const void *value)
  1626. {
  1627. char num[130];
  1628. size_t length;
  1629. if (!value) {
  1630. return SDL_PrintString(text, maxlen, info, NULL);
  1631. }
  1632. SDL_ulltoa((unsigned long long)(uintptr_t)value, num, 16);
  1633. length = SDL_PrintString(text, maxlen, info, "0x");
  1634. return length + SDL_PrintString(TEXT_AND_LEN_ARGS, info, num);
  1635. }
  1636. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1637. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1638. {
  1639. size_t length = 0;
  1640. if (!text) {
  1641. maxlen = 0;
  1642. }
  1643. if (!fmt) {
  1644. fmt = "";
  1645. }
  1646. while (*fmt) {
  1647. if (*fmt == '%') {
  1648. SDL_bool done = SDL_FALSE;
  1649. SDL_bool check_flag;
  1650. SDL_FormatInfo info;
  1651. enum
  1652. {
  1653. DO_INT,
  1654. DO_LONG,
  1655. DO_LONGLONG,
  1656. DO_SIZE_T
  1657. } inttype = DO_INT;
  1658. SDL_zero(info);
  1659. info.radix = 10;
  1660. info.precision = -1;
  1661. check_flag = SDL_TRUE;
  1662. while (check_flag) {
  1663. ++fmt;
  1664. switch (*fmt) {
  1665. case '-':
  1666. info.left_justify = SDL_TRUE;
  1667. break;
  1668. case '+':
  1669. info.force_sign = SDL_TRUE;
  1670. break;
  1671. case '#':
  1672. info.force_type = SDL_TRUE;
  1673. break;
  1674. case '0':
  1675. info.pad_zeroes = SDL_TRUE;
  1676. break;
  1677. default:
  1678. check_flag = SDL_FALSE;
  1679. break;
  1680. }
  1681. }
  1682. if (*fmt >= '0' && *fmt <= '9') {
  1683. info.width = SDL_strtol(fmt, (char **)&fmt, 0);
  1684. } else if (*fmt == '*') {
  1685. ++fmt;
  1686. info.width = va_arg(ap, int);
  1687. }
  1688. if (*fmt == '.') {
  1689. ++fmt;
  1690. if (*fmt >= '0' && *fmt <= '9') {
  1691. info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
  1692. } else if (*fmt == '*') {
  1693. ++fmt;
  1694. info.precision = va_arg(ap, int);
  1695. } else {
  1696. info.precision = 0;
  1697. }
  1698. if (info.precision < 0) {
  1699. info.precision = 0;
  1700. }
  1701. }
  1702. while (!done) {
  1703. switch (*fmt) {
  1704. case '%':
  1705. if (length < maxlen) {
  1706. text[length] = '%';
  1707. }
  1708. ++length;
  1709. done = SDL_TRUE;
  1710. break;
  1711. case 'c':
  1712. /* char is promoted to int when passed through (...) */
  1713. if (length < maxlen) {
  1714. text[length] = (char)va_arg(ap, int);
  1715. }
  1716. ++length;
  1717. done = SDL_TRUE;
  1718. break;
  1719. case 'h':
  1720. /* short is promoted to int when passed through (...) */
  1721. break;
  1722. case 'l':
  1723. if (inttype < DO_LONGLONG) {
  1724. ++inttype;
  1725. }
  1726. break;
  1727. case 'I':
  1728. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1729. fmt += 2;
  1730. inttype = DO_LONGLONG;
  1731. }
  1732. break;
  1733. case 'z':
  1734. inttype = DO_SIZE_T;
  1735. break;
  1736. case 'i':
  1737. case 'd':
  1738. if (info.precision >= 0) {
  1739. info.pad_zeroes = SDL_FALSE;
  1740. }
  1741. switch (inttype) {
  1742. case DO_INT:
  1743. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1744. (long)va_arg(ap, int));
  1745. break;
  1746. case DO_LONG:
  1747. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1748. va_arg(ap, long));
  1749. break;
  1750. case DO_LONGLONG:
  1751. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1752. va_arg(ap, Sint64));
  1753. break;
  1754. case DO_SIZE_T:
  1755. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1756. va_arg(ap, size_t));
  1757. break;
  1758. }
  1759. done = SDL_TRUE;
  1760. break;
  1761. case 'p':
  1762. info.force_case = SDL_CASE_LOWER;
  1763. length += SDL_PrintPointer(TEXT_AND_LEN_ARGS, &info, va_arg(ap, void *));
  1764. done = SDL_TRUE;
  1765. break;
  1766. case 'x':
  1767. info.force_case = SDL_CASE_LOWER;
  1768. SDL_FALLTHROUGH;
  1769. case 'X':
  1770. if (info.force_case == SDL_CASE_NOCHANGE) {
  1771. info.force_case = SDL_CASE_UPPER;
  1772. }
  1773. if (info.radix == 10) {
  1774. info.radix = 16;
  1775. }
  1776. SDL_FALLTHROUGH;
  1777. case 'o':
  1778. if (info.radix == 10) {
  1779. info.radix = 8;
  1780. }
  1781. SDL_FALLTHROUGH;
  1782. case 'u':
  1783. info.force_sign = SDL_FALSE;
  1784. if (info.precision >= 0) {
  1785. info.pad_zeroes = SDL_FALSE;
  1786. }
  1787. switch (inttype) {
  1788. case DO_INT:
  1789. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1790. (unsigned long)
  1791. va_arg(ap, unsigned int));
  1792. break;
  1793. case DO_LONG:
  1794. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1795. va_arg(ap, unsigned long));
  1796. break;
  1797. case DO_LONGLONG:
  1798. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1799. va_arg(ap, Uint64));
  1800. break;
  1801. case DO_SIZE_T:
  1802. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1803. va_arg(ap, size_t));
  1804. break;
  1805. }
  1806. done = SDL_TRUE;
  1807. break;
  1808. case 'f':
  1809. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), SDL_FALSE);
  1810. done = SDL_TRUE;
  1811. break;
  1812. case 'g':
  1813. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), SDL_TRUE);
  1814. done = SDL_TRUE;
  1815. break;
  1816. case 'S':
  1817. info.pad_zeroes = SDL_FALSE;
  1818. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  1819. done = SDL_TRUE;
  1820. break;
  1821. case 's':
  1822. info.pad_zeroes = SDL_FALSE;
  1823. if (inttype > DO_INT) {
  1824. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  1825. } else {
  1826. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
  1827. }
  1828. done = SDL_TRUE;
  1829. break;
  1830. default:
  1831. done = SDL_TRUE;
  1832. break;
  1833. }
  1834. ++fmt;
  1835. }
  1836. } else {
  1837. if (length < maxlen) {
  1838. text[length] = *fmt;
  1839. }
  1840. ++fmt;
  1841. ++length;
  1842. }
  1843. }
  1844. if (length < maxlen) {
  1845. text[length] = '\0';
  1846. } else if (maxlen > 0) {
  1847. text[maxlen - 1] = '\0';
  1848. }
  1849. return (int)length;
  1850. }
  1851. #undef TEXT_AND_LEN_ARGS
  1852. #endif /* HAVE_VSNPRINTF */
  1853. int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, va_list ap)
  1854. {
  1855. char *text_utf8 = NULL, *fmt_utf8 = NULL;
  1856. int retval;
  1857. if (fmt) {
  1858. fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
  1859. if (!fmt_utf8) {
  1860. return -1;
  1861. }
  1862. }
  1863. if (!maxlen) {
  1864. /* We still need to generate the text to find the final text length */
  1865. maxlen = 1024;
  1866. }
  1867. text_utf8 = (char *)SDL_malloc(maxlen * 4);
  1868. if (!text_utf8) {
  1869. SDL_free(fmt_utf8);
  1870. return -1;
  1871. }
  1872. retval = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
  1873. if (retval >= 0) {
  1874. wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1);
  1875. if (text_wchar) {
  1876. if (text) {
  1877. SDL_wcslcpy(text, text_wchar, maxlen);
  1878. }
  1879. retval = (int)SDL_wcslen(text_wchar);
  1880. SDL_free(text_wchar);
  1881. } else {
  1882. retval = -1;
  1883. }
  1884. }
  1885. SDL_free(text_utf8);
  1886. SDL_free(fmt_utf8);
  1887. return retval;
  1888. }
  1889. int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1890. {
  1891. va_list ap;
  1892. int retval;
  1893. va_start(ap, fmt);
  1894. retval = SDL_vasprintf(strp, fmt, ap);
  1895. va_end(ap);
  1896. return retval;
  1897. }
  1898. int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
  1899. {
  1900. int retval;
  1901. int size = 100; /* Guess we need no more than 100 bytes */
  1902. char *p, *np;
  1903. va_list aq;
  1904. *strp = NULL;
  1905. p = (char *)SDL_malloc(size);
  1906. if (p == NULL) {
  1907. return -1;
  1908. }
  1909. while (1) {
  1910. /* Try to print in the allocated space */
  1911. va_copy(aq, ap);
  1912. retval = SDL_vsnprintf(p, size, fmt, aq);
  1913. va_end(aq);
  1914. /* Check error code */
  1915. if (retval < 0) {
  1916. return retval;
  1917. }
  1918. /* If that worked, return the string */
  1919. if (retval < size) {
  1920. *strp = p;
  1921. return retval;
  1922. }
  1923. /* Else try again with more space */
  1924. size = retval + 1; /* Precisely what is needed */
  1925. np = (char *)SDL_realloc(p, size);
  1926. if (np == NULL) {
  1927. SDL_free(p);
  1928. return -1;
  1929. } else {
  1930. p = np;
  1931. }
  1932. }
  1933. }