EASprintf.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EAStdC/internal/Config.h>
  5. #include <EAStdC/internal/SprintfCore.h>
  6. #include <EAStdC/EAString.h>
  7. #include <EAStdC/EACType.h>
  8. #include <EAAssert/eaassert.h>
  9. EA_DISABLE_ALL_VC_WARNINGS()
  10. #include <math.h>
  11. #include <float.h>
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. EA_RESTORE_ALL_VC_WARNINGS()
  16. EA_DISABLE_VC_WARNING(4127) // conditional expression is constant.
  17. namespace EA
  18. {
  19. namespace StdC
  20. {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // char8_t
  23. ///////////////////////////////////////////////////////////////////////////////
  24. EASTDC_API int Vcprintf(WriteFunction8 pWriteFunction8, void* EA_RESTRICT pContext, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  25. {
  26. return SprintfLocal::VprintfCore(pWriteFunction8, pContext, pFormat, arguments);
  27. }
  28. EASTDC_API int Vfprintf(FILE* EA_RESTRICT pFile, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  29. {
  30. #if EASTDC_PRINTF_DEBUG_ENABLED
  31. if((pFile == stdout) || (pFile == stderr))
  32. {
  33. SprintfLocal::PlatformLogWriterContext8 context;
  34. return SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  35. }
  36. #endif
  37. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter8, pFile, pFormat, arguments);
  38. }
  39. EASTDC_API int Vprintf(const char8_t* EA_RESTRICT pFormat, va_list arguments)
  40. {
  41. #if EASTDC_PRINTF_DEBUG_ENABLED
  42. SprintfLocal::PlatformLogWriterContext8 context;
  43. return SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  44. #else
  45. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter8, stdout, pFormat, arguments);
  46. #endif
  47. }
  48. EASTDC_API int Vsprintf(char8_t* EA_RESTRICT pDestination, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  49. {
  50. return Vsnprintf(pDestination, (size_t)-1, pFormat, arguments);
  51. }
  52. EASTDC_API int Vsnprintf(char8_t* EA_RESTRICT pDestination, size_t n, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  53. {
  54. SprintfLocal::SnprintfContext8 sc(pDestination, 0, pDestination ? n : 0);
  55. const int nRequiredLength = SprintfLocal::VprintfCore(SprintfLocal::StringWriter8, &sc, pFormat, arguments);
  56. #if EASPRINTF_SNPRINTF_C99_RETURN
  57. if(pDestination && (nRequiredLength >= 0))
  58. {
  59. if((size_t)nRequiredLength < n) // If there was enough space...
  60. pDestination[nRequiredLength] = 0;
  61. else if(n > 0)
  62. pDestination[n - 1] = 0;
  63. } // Else an encoding error has occurred and we can do nothing.
  64. return nRequiredLength;
  65. #else
  66. if((size_t)nRequiredLength < n)
  67. {
  68. if(pDestination)
  69. pDestination[nRequiredLength] = 0;
  70. return nRequiredLength;
  71. }
  72. else if((n > 0) && pDestination)
  73. pDestination[n - 1] = 0;
  74. else if(n == 0)
  75. return nRequiredLength;
  76. return -1;
  77. #endif
  78. }
  79. EASTDC_API int Vscprintf(const char8_t* EA_RESTRICT pFormat, va_list arguments)
  80. {
  81. // vscprintf returns the number of chars that are needed for a printf operation.
  82. return Vsnprintf(NULL, 0, pFormat, arguments);
  83. }
  84. EASTDC_API int Vdprintf(const char8_t* EA_RESTRICT pFormat, va_list arguments)
  85. {
  86. SprintfLocal::PlatformLogWriterContext8 context;
  87. return SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  88. }
  89. EASTDC_API int Cprintf(WriteFunction8 pWriteFunction, void* EA_RESTRICT pContext, const char8_t* EA_RESTRICT pFormat, ...)
  90. {
  91. va_list arguments;
  92. va_start(arguments, pFormat);
  93. int result = SprintfLocal::VprintfCore(pWriteFunction, pContext, pFormat, arguments);
  94. va_end(arguments);
  95. return result;
  96. }
  97. EASTDC_API int Fprintf(FILE* EA_RESTRICT pFile, const char8_t* EA_RESTRICT pFormat, ...)
  98. {
  99. int result;
  100. va_list arguments;
  101. va_start(arguments, pFormat);
  102. #if EASTDC_PRINTF_DEBUG_ENABLED
  103. if((pFile == stdout) || (pFile == stderr))
  104. {
  105. SprintfLocal::PlatformLogWriterContext8 context;
  106. result = SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  107. }
  108. else
  109. #endif
  110. result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter8, pFile, pFormat, arguments);
  111. va_end(arguments);
  112. return result;
  113. }
  114. EASTDC_API int Printf(const char8_t* EA_RESTRICT pFormat, ...)
  115. {
  116. va_list arguments;
  117. va_start(arguments, pFormat);
  118. #if EASTDC_PRINTF_DEBUG_ENABLED
  119. SprintfLocal::PlatformLogWriterContext8 context;
  120. int result = SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  121. #else
  122. int result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter8, stdout, pFormat, arguments);
  123. #endif
  124. va_end(arguments);
  125. return result;
  126. }
  127. EASTDC_API int Sprintf(char8_t* EA_RESTRICT pDestination, const char8_t* EA_RESTRICT pFormat, ...)
  128. {
  129. va_list arguments;
  130. va_start(arguments, pFormat);
  131. int result = Vsnprintf(pDestination, (size_t)SprintfLocal::kNoPrecision, pFormat, arguments);
  132. va_end(arguments);
  133. return result;
  134. }
  135. EASTDC_API int Snprintf(char8_t* EA_RESTRICT pDestination, size_t n, const char8_t* EA_RESTRICT pFormat, ...)
  136. {
  137. va_list arguments;
  138. va_start(arguments, pFormat);
  139. int result = Vsnprintf(pDestination, n, pFormat, arguments);
  140. va_end(arguments);
  141. return result;
  142. }
  143. EASTDC_API int Dprintf(const char8_t* EA_RESTRICT pFormat, ...)
  144. {
  145. va_list arguments;
  146. va_start(arguments, pFormat);
  147. SprintfLocal::PlatformLogWriterContext8 context;
  148. int result = SprintfLocal::VprintfCore(SprintfLocal::PlatformLogWriter8, &context, pFormat, arguments);
  149. va_end(arguments);
  150. return result;
  151. }
  152. ///////////////////////////////////////////////////////////////////////////////
  153. // char16_t
  154. ///////////////////////////////////////////////////////////////////////////////
  155. EASTDC_API int Vcprintf(WriteFunction16 pWriteFunction16, void* EA_RESTRICT pContext, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  156. {
  157. return SprintfLocal::VprintfCore(pWriteFunction16, pContext, pFormat, arguments);
  158. }
  159. EASTDC_API int Vfprintf(FILE* EA_RESTRICT pFile, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  160. {
  161. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter16, pFile, pFormat, arguments);
  162. }
  163. EASTDC_API int Vprintf(const char16_t* EA_RESTRICT pFormat, va_list arguments)
  164. {
  165. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter16, stdout, pFormat, arguments);
  166. }
  167. EASTDC_API int Vsprintf(char16_t* EA_RESTRICT pDestination, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  168. {
  169. return Vsnprintf(pDestination, (size_t)-1, pFormat, arguments);
  170. }
  171. EASTDC_API int Vsnprintf(char16_t* EA_RESTRICT pDestination, size_t n, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  172. {
  173. SprintfLocal::SnprintfContext16 sc(pDestination, 0, pDestination ? n : 0);
  174. const int nRequiredLength = SprintfLocal::VprintfCore(SprintfLocal::StringWriter16, &sc, pFormat, arguments);
  175. #if EASPRINTF_SNPRINTF_C99_RETURN
  176. if(pDestination && (nRequiredLength >= 0))
  177. {
  178. if((size_t)nRequiredLength < n) // If there was enough space...
  179. pDestination[nRequiredLength] = 0;
  180. else if(n > 0)
  181. pDestination[n - 1] = 0;
  182. } // Else an encoding error has occurred and we can do nothing.
  183. return nRequiredLength;
  184. #else
  185. if((size_t)nRequiredLength < n)
  186. {
  187. if(pDestination)
  188. pDestination[nRequiredLength] = 0;
  189. return nRequiredLength;
  190. }
  191. else if((n > 0) && pDestination)
  192. pDestination[n - 1] = 0;
  193. else if(n == 0)
  194. return nRequiredLength;
  195. return -1;
  196. #endif
  197. }
  198. EASTDC_API int Vscprintf(const char16_t* EA_RESTRICT pFormat, va_list arguments)
  199. {
  200. // vscprintf returns the number of chars that are needed for a printf operation.
  201. return Vsnprintf(NULL, 0, pFormat, arguments);
  202. }
  203. EASTDC_API int Cprintf(WriteFunction16 pWriteFunction, void* EA_RESTRICT pContext, const char16_t* EA_RESTRICT pFormat, ...)
  204. {
  205. va_list arguments;
  206. va_start(arguments, pFormat);
  207. int result = SprintfLocal::VprintfCore(pWriteFunction, pContext, pFormat, arguments);
  208. va_end(arguments);
  209. return result;
  210. }
  211. EASTDC_API int Fprintf(FILE* EA_RESTRICT pFile, const char16_t* EA_RESTRICT pFormat, ...)
  212. {
  213. va_list arguments;
  214. va_start(arguments, pFormat);
  215. int result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter16, pFile, pFormat, arguments);
  216. va_end(arguments);
  217. return result;
  218. }
  219. EASTDC_API int Printf(const char16_t* EA_RESTRICT pFormat, ...)
  220. {
  221. va_list arguments;
  222. va_start(arguments, pFormat);
  223. int result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter16, stdout, pFormat, arguments);
  224. va_end(arguments);
  225. return result;
  226. }
  227. EASTDC_API int Sprintf(char16_t* EA_RESTRICT pDestination, const char16_t* EA_RESTRICT pFormat, ...)
  228. {
  229. va_list arguments;
  230. va_start(arguments, pFormat);
  231. int result = Vsnprintf(pDestination, (size_t)SprintfLocal::kNoPrecision, pFormat, arguments);
  232. va_end(arguments);
  233. return result;
  234. }
  235. EASTDC_API int Snprintf(char16_t* EA_RESTRICT pDestination, size_t n, const char16_t* EA_RESTRICT pFormat, ...)
  236. {
  237. va_list arguments;
  238. va_start(arguments, pFormat);
  239. int result = Vsnprintf(pDestination, n, pFormat, arguments);
  240. va_end(arguments);
  241. return result;
  242. }
  243. ///////////////////////////////////////////////////////////////////////////////
  244. // char32_t
  245. ///////////////////////////////////////////////////////////////////////////////
  246. EASTDC_API int Vcprintf(WriteFunction32 pWriteFunction32, void* EA_RESTRICT pContext, const char32_t* EA_RESTRICT pFormat, va_list arguments)
  247. {
  248. return SprintfLocal::VprintfCore(pWriteFunction32, pContext, pFormat, arguments);
  249. }
  250. EASTDC_API int Vfprintf(FILE* EA_RESTRICT pFile, const char32_t* EA_RESTRICT pFormat, va_list arguments)
  251. {
  252. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter32, pFile, pFormat, arguments);
  253. }
  254. EASTDC_API int Vprintf(const char32_t* EA_RESTRICT pFormat, va_list arguments)
  255. {
  256. return SprintfLocal::VprintfCore(SprintfLocal::FILEWriter32, stdout, pFormat, arguments);
  257. }
  258. EASTDC_API int Vsprintf(char32_t* EA_RESTRICT pDestination, const char32_t* EA_RESTRICT pFormat, va_list arguments)
  259. {
  260. return Vsnprintf(pDestination, (size_t)-1, pFormat, arguments);
  261. }
  262. EASTDC_API int Vsnprintf(char32_t* EA_RESTRICT pDestination, size_t n, const char32_t* EA_RESTRICT pFormat, va_list arguments)
  263. {
  264. SprintfLocal::SnprintfContext32 sc(pDestination, 0, pDestination ? n : 0);
  265. const int nRequiredLength = SprintfLocal::VprintfCore(SprintfLocal::StringWriter32, &sc, pFormat, arguments);
  266. #if EASPRINTF_SNPRINTF_C99_RETURN
  267. if(pDestination && (nRequiredLength >= 0))
  268. {
  269. if((size_t)nRequiredLength < n) // If there was enough space...
  270. pDestination[nRequiredLength] = 0;
  271. else if(n > 0)
  272. pDestination[n - 1] = 0;
  273. } // Else an encoding error has occurred and we can do nothing.
  274. return nRequiredLength;
  275. #else
  276. if((size_t)nRequiredLength < n)
  277. {
  278. if(pDestination)
  279. pDestination[nRequiredLength] = 0;
  280. return nRequiredLength;
  281. }
  282. else if((n > 0) && pDestination)
  283. pDestination[n - 1] = 0;
  284. else if(n == 0)
  285. return nRequiredLength;
  286. return -1;
  287. #endif
  288. }
  289. EASTDC_API int Vscprintf(const char32_t* EA_RESTRICT pFormat, va_list arguments)
  290. {
  291. // vscprintf returns the number of chars that are needed for a printf operation.
  292. return Vsnprintf(NULL, 0, pFormat, arguments);
  293. }
  294. EASTDC_API int Cprintf(WriteFunction32 pWriteFunction, void* EA_RESTRICT pContext, const char32_t* EA_RESTRICT pFormat, ...)
  295. {
  296. va_list arguments;
  297. va_start(arguments, pFormat);
  298. int result = SprintfLocal::VprintfCore(pWriteFunction, pContext, pFormat, arguments);
  299. va_end(arguments);
  300. return result;
  301. }
  302. EASTDC_API int Fprintf(FILE* EA_RESTRICT pFile, const char32_t* EA_RESTRICT pFormat, ...)
  303. {
  304. va_list arguments;
  305. va_start(arguments, pFormat);
  306. int result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter32, pFile, pFormat, arguments);
  307. va_end(arguments);
  308. return result;
  309. }
  310. EASTDC_API int Printf(const char32_t* EA_RESTRICT pFormat, ...)
  311. {
  312. va_list arguments;
  313. va_start(arguments, pFormat);
  314. int result = SprintfLocal::VprintfCore(SprintfLocal::FILEWriter32, stdout, pFormat, arguments);
  315. va_end(arguments);
  316. return result;
  317. }
  318. EASTDC_API int Sprintf(char32_t* EA_RESTRICT pDestination, const char32_t* EA_RESTRICT pFormat, ...)
  319. {
  320. va_list arguments;
  321. va_start(arguments, pFormat);
  322. int result = Vsnprintf(pDestination, (size_t)SprintfLocal::kNoPrecision, pFormat, arguments);
  323. va_end(arguments);
  324. return result;
  325. }
  326. EASTDC_API int Snprintf(char32_t* EA_RESTRICT pDestination, size_t n, const char32_t* EA_RESTRICT pFormat, ...)
  327. {
  328. va_list arguments;
  329. va_start(arguments, pFormat);
  330. int result = Vsnprintf(pDestination, n, pFormat, arguments);
  331. va_end(arguments);
  332. return result;
  333. }
  334. ///////////////////////////////////////////////////////////////////////////
  335. // Deprecated functionality
  336. ///////////////////////////////////////////////////////////////////////////
  337. struct Bridge8
  338. {
  339. WriteFunction8Old mpOldWriteFunction;
  340. void* mpUserContext;
  341. };
  342. static int WriteFunctionBridge8(const char8_t* EA_RESTRICT pData, size_t nCount, void* EA_RESTRICT pContext8, WriteFunctionState /*wfs*/)
  343. {
  344. Bridge8* pBridge8 = (Bridge8*)pContext8;
  345. return pBridge8->mpOldWriteFunction(pData, nCount, pBridge8->mpUserContext);
  346. }
  347. struct Bridge16
  348. {
  349. WriteFunction16Old mpOldWriteFunction;
  350. void* mpUserContext;
  351. };
  352. static int WriteFunctionBridge16(const char16_t* EA_RESTRICT pData, size_t nCount, void* EA_RESTRICT pContext16, WriteFunctionState /*wfs*/)
  353. {
  354. Bridge16* pBridge16 = (Bridge16*)pContext16;
  355. return pBridge16->mpOldWriteFunction(pData, nCount, pBridge16->mpUserContext);
  356. }
  357. EASTDC_API int Cprintf(WriteFunction8Old pWriteFunction, void* EA_RESTRICT pContext, const char8_t* EA_RESTRICT pFormat, ...)
  358. {
  359. va_list arguments;
  360. va_start(arguments, pFormat);
  361. int result = Vcprintf(pWriteFunction, pContext, pFormat, arguments);
  362. va_end(arguments);
  363. return result;
  364. }
  365. EASTDC_API int Cprintf(WriteFunction16Old pWriteFunction, void* EA_RESTRICT pContext, const char16_t* EA_RESTRICT pFormat, ...)
  366. {
  367. va_list arguments;
  368. va_start(arguments, pFormat);
  369. int result = Vcprintf(pWriteFunction, pContext, pFormat, arguments);
  370. va_end(arguments);
  371. return result;
  372. }
  373. EASTDC_API int Vcprintf(WriteFunction8Old pWriteFunction8, void* EA_RESTRICT pContext, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  374. {
  375. Bridge8 bridge8 = { pWriteFunction8, pContext };
  376. return SprintfLocal::VprintfCore(WriteFunctionBridge8, &bridge8, pFormat, arguments);
  377. }
  378. EASTDC_API int Vcprintf(WriteFunction16Old pWriteFunction16, void* EA_RESTRICT pContext, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  379. {
  380. Bridge16 bridge16 = { pWriteFunction16, pContext };
  381. return SprintfLocal::VprintfCore(WriteFunctionBridge16, &bridge16, pFormat, arguments);
  382. }
  383. #if EASTDC_VSNPRINTF8_ENABLED
  384. EASTDC_API int Vsnprintf8(char8_t* EA_RESTRICT pDestination, size_t n, const char8_t* EA_RESTRICT pFormat, va_list arguments)
  385. {
  386. return Vsnprintf(pDestination, n, pFormat, arguments);
  387. }
  388. EASTDC_API int Vsnprintf16(char16_t* EA_RESTRICT pDestination, size_t n, const char16_t* EA_RESTRICT pFormat, va_list arguments)
  389. {
  390. return Vsnprintf(pDestination, n, pFormat, arguments);
  391. }
  392. EASTDC_API int Vsnprintf32(char32_t* EA_RESTRICT pDestination, size_t n, const char32_t* EA_RESTRICT pFormat, va_list arguments)
  393. {
  394. return Vsnprintf(pDestination, n, pFormat, arguments);
  395. }
  396. #if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
  397. EASTDC_API int VsnprintfW(wchar_t* EA_RESTRICT pDestination, size_t n, const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  398. {
  399. return Vsnprintf(pDestination, n, pFormat, arguments);
  400. }
  401. #endif
  402. #endif
  403. #if defined(EA_WCHAR_UNIQUE) && EA_WCHAR_UNIQUE
  404. EASTDC_API int Vcprintf(WriteFunctionW pWriteFunctionW, void* EA_RESTRICT pContext, const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  405. {
  406. #if (EA_WCHAR_SIZE == 2)
  407. return Vcprintf(reinterpret_cast<WriteFunction16>(pWriteFunctionW), pContext, reinterpret_cast<const char16_t *>(pFormat), arguments);
  408. #else
  409. return Vcprintf(reinterpret_cast<WriteFunction32>(pWriteFunctionW), pContext, reinterpret_cast<const char32_t *>(pFormat), arguments);
  410. #endif
  411. }
  412. EASTDC_API int Vfprintf(FILE* EA_RESTRICT pFile, const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  413. {
  414. #if (EA_WCHAR_SIZE == 2)
  415. return Vfprintf(pFile, reinterpret_cast<const char16_t *>(pFormat), arguments);
  416. #else
  417. return Vfprintf(pFile, reinterpret_cast<const char32_t *>(pFormat), arguments);
  418. #endif
  419. }
  420. EASTDC_API int Vprintf(const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  421. {
  422. #if (EA_WCHAR_SIZE == 2)
  423. return Vprintf(reinterpret_cast<const char16_t *>(pFormat), arguments);
  424. #else
  425. return Vprintf(reinterpret_cast<const char32_t *>(pFormat), arguments);
  426. #endif
  427. }
  428. EASTDC_API int Vsprintf(wchar_t* EA_RESTRICT pDestination, const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  429. {
  430. #if (EA_WCHAR_SIZE == 2)
  431. return Vsprintf(reinterpret_cast<char16_t *>(pDestination), reinterpret_cast<const char16_t *>(pFormat), arguments);
  432. #else
  433. return Vsprintf(reinterpret_cast<char32_t *>(pDestination), reinterpret_cast<const char32_t *>(pFormat), arguments);
  434. #endif
  435. }
  436. EASTDC_API int Vsnprintf(wchar_t* EA_RESTRICT pDestination, size_t n, const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  437. {
  438. #if (EA_WCHAR_SIZE == 2)
  439. return Vsnprintf(reinterpret_cast<char16_t *>(pDestination), n, reinterpret_cast<const char16_t *>(pFormat), arguments);
  440. #else
  441. return Vsnprintf(reinterpret_cast<char32_t *>(pDestination), n, reinterpret_cast<const char32_t *>(pFormat), arguments);
  442. #endif
  443. }
  444. EASTDC_API int Vscprintf(const wchar_t* EA_RESTRICT pFormat, va_list arguments)
  445. {
  446. #if (EA_WCHAR_SIZE == 2)
  447. return Vscprintf(reinterpret_cast<const char16_t *>(pFormat), arguments);
  448. #else
  449. return Vscprintf(reinterpret_cast<const char32_t *>(pFormat), arguments);
  450. #endif
  451. }
  452. EASTDC_API int Cprintf(WriteFunctionW pWriteFunction, void* EA_RESTRICT pContext, const wchar_t* EA_RESTRICT pFormat, ...)
  453. {
  454. va_list arguments;
  455. va_start(arguments, pFormat);
  456. int result = Vcprintf(pWriteFunction, pContext, pFormat, arguments);
  457. va_end(arguments);
  458. return result;
  459. }
  460. EASTDC_API int Fprintf(FILE* EA_RESTRICT pFile, const wchar_t* EA_RESTRICT pFormat, ...)
  461. {
  462. va_list arguments;
  463. va_start(arguments, pFormat);
  464. int result = Vfprintf(pFile, pFormat, arguments);
  465. va_end(arguments);
  466. return result;
  467. }
  468. EASTDC_API int Printf(const wchar_t* EA_RESTRICT pFormat, ...)
  469. {
  470. va_list arguments;
  471. va_start(arguments, pFormat);
  472. int result = Vprintf(pFormat, arguments);
  473. va_end(arguments);
  474. return result;
  475. }
  476. EASTDC_API int Sprintf(wchar_t* EA_RESTRICT pDestination, const wchar_t* EA_RESTRICT pFormat, ...)
  477. {
  478. va_list arguments;
  479. va_start(arguments, pFormat);
  480. int result = Vsprintf(pDestination, pFormat, arguments);
  481. va_end(arguments);
  482. return result;
  483. }
  484. EASTDC_API int Snprintf(wchar_t* EA_RESTRICT pDestination, size_t n, const wchar_t* EA_RESTRICT pFormat, ...)
  485. {
  486. va_list arguments;
  487. va_start(arguments, pFormat);
  488. int result = Vsnprintf(pDestination, n, pFormat, arguments);
  489. va_end(arguments);
  490. return result;
  491. }
  492. #endif
  493. } // namespace StdC
  494. } // namespace EA
  495. EA_RESTORE_VC_WARNING()