SDL_string.c 52 KB

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