inttypes.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // ISO C9x compliant inttypes.h for Microsoft Visual Studio
  2. // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
  3. //
  4. // Copyright (c) 2006-2013 Alexander Chemeris
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // 1. Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // 2. Redistributions in binary form must reproduce the above copyright
  13. // notice, this list of conditions and the following disclaimer in the
  14. // documentation and/or other materials provided with the distribution.
  15. //
  16. // 3. Neither the name of the product nor the names of its contributors may
  17. // be used to endorse or promote products derived from this software
  18. // without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  23. // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  26. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // The above software in this distribution may have been modified by
  33. // THL A29 Limited ("Tencent Modifications").
  34. // All Tencent Modifications are Copyright (C) 2015 THL A29 Limited.
  35. #ifndef _MSC_VER // [
  36. #error "Use this header only with Microsoft Visual C++ compilers!"
  37. #endif // _MSC_VER ]
  38. #ifndef _MSC_INTTYPES_H_ // [
  39. #define _MSC_INTTYPES_H_
  40. #if _MSC_VER > 1000
  41. #pragma once
  42. #endif
  43. #include "stdint.h"
  44. // miloyip: VC supports inttypes.h since VC2013
  45. #if _MSC_VER >= 1800
  46. #include <inttypes.h>
  47. #else
  48. // 7.8 Format conversion of integer types
  49. typedef struct {
  50. intmax_t quot;
  51. intmax_t rem;
  52. } imaxdiv_t;
  53. // 7.8.1 Macros for format specifiers
  54. #if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198
  55. // The fprintf macros for signed integers are:
  56. #define PRId8 "d"
  57. #define PRIi8 "i"
  58. #define PRIdLEAST8 "d"
  59. #define PRIiLEAST8 "i"
  60. #define PRIdFAST8 "d"
  61. #define PRIiFAST8 "i"
  62. #define PRId16 "hd"
  63. #define PRIi16 "hi"
  64. #define PRIdLEAST16 "hd"
  65. #define PRIiLEAST16 "hi"
  66. #define PRIdFAST16 "hd"
  67. #define PRIiFAST16 "hi"
  68. #define PRId32 "I32d"
  69. #define PRIi32 "I32i"
  70. #define PRIdLEAST32 "I32d"
  71. #define PRIiLEAST32 "I32i"
  72. #define PRIdFAST32 "I32d"
  73. #define PRIiFAST32 "I32i"
  74. #define PRId64 "I64d"
  75. #define PRIi64 "I64i"
  76. #define PRIdLEAST64 "I64d"
  77. #define PRIiLEAST64 "I64i"
  78. #define PRIdFAST64 "I64d"
  79. #define PRIiFAST64 "I64i"
  80. #define PRIdMAX "I64d"
  81. #define PRIiMAX "I64i"
  82. #define PRIdPTR "Id"
  83. #define PRIiPTR "Ii"
  84. // The fprintf macros for unsigned integers are:
  85. #define PRIo8 "o"
  86. #define PRIu8 "u"
  87. #define PRIx8 "x"
  88. #define PRIX8 "X"
  89. #define PRIoLEAST8 "o"
  90. #define PRIuLEAST8 "u"
  91. #define PRIxLEAST8 "x"
  92. #define PRIXLEAST8 "X"
  93. #define PRIoFAST8 "o"
  94. #define PRIuFAST8 "u"
  95. #define PRIxFAST8 "x"
  96. #define PRIXFAST8 "X"
  97. #define PRIo16 "ho"
  98. #define PRIu16 "hu"
  99. #define PRIx16 "hx"
  100. #define PRIX16 "hX"
  101. #define PRIoLEAST16 "ho"
  102. #define PRIuLEAST16 "hu"
  103. #define PRIxLEAST16 "hx"
  104. #define PRIXLEAST16 "hX"
  105. #define PRIoFAST16 "ho"
  106. #define PRIuFAST16 "hu"
  107. #define PRIxFAST16 "hx"
  108. #define PRIXFAST16 "hX"
  109. #define PRIo32 "I32o"
  110. #define PRIu32 "I32u"
  111. #define PRIx32 "I32x"
  112. #define PRIX32 "I32X"
  113. #define PRIoLEAST32 "I32o"
  114. #define PRIuLEAST32 "I32u"
  115. #define PRIxLEAST32 "I32x"
  116. #define PRIXLEAST32 "I32X"
  117. #define PRIoFAST32 "I32o"
  118. #define PRIuFAST32 "I32u"
  119. #define PRIxFAST32 "I32x"
  120. #define PRIXFAST32 "I32X"
  121. #define PRIo64 "I64o"
  122. #define PRIu64 "I64u"
  123. #define PRIx64 "I64x"
  124. #define PRIX64 "I64X"
  125. #define PRIoLEAST64 "I64o"
  126. #define PRIuLEAST64 "I64u"
  127. #define PRIxLEAST64 "I64x"
  128. #define PRIXLEAST64 "I64X"
  129. #define PRIoFAST64 "I64o"
  130. #define PRIuFAST64 "I64u"
  131. #define PRIxFAST64 "I64x"
  132. #define PRIXFAST64 "I64X"
  133. #define PRIoMAX "I64o"
  134. #define PRIuMAX "I64u"
  135. #define PRIxMAX "I64x"
  136. #define PRIXMAX "I64X"
  137. #define PRIoPTR "Io"
  138. #define PRIuPTR "Iu"
  139. #define PRIxPTR "Ix"
  140. #define PRIXPTR "IX"
  141. // The fscanf macros for signed integers are:
  142. #define SCNd8 "d"
  143. #define SCNi8 "i"
  144. #define SCNdLEAST8 "d"
  145. #define SCNiLEAST8 "i"
  146. #define SCNdFAST8 "d"
  147. #define SCNiFAST8 "i"
  148. #define SCNd16 "hd"
  149. #define SCNi16 "hi"
  150. #define SCNdLEAST16 "hd"
  151. #define SCNiLEAST16 "hi"
  152. #define SCNdFAST16 "hd"
  153. #define SCNiFAST16 "hi"
  154. #define SCNd32 "ld"
  155. #define SCNi32 "li"
  156. #define SCNdLEAST32 "ld"
  157. #define SCNiLEAST32 "li"
  158. #define SCNdFAST32 "ld"
  159. #define SCNiFAST32 "li"
  160. #define SCNd64 "I64d"
  161. #define SCNi64 "I64i"
  162. #define SCNdLEAST64 "I64d"
  163. #define SCNiLEAST64 "I64i"
  164. #define SCNdFAST64 "I64d"
  165. #define SCNiFAST64 "I64i"
  166. #define SCNdMAX "I64d"
  167. #define SCNiMAX "I64i"
  168. #ifdef _WIN64 // [
  169. # define SCNdPTR "I64d"
  170. # define SCNiPTR "I64i"
  171. #else // _WIN64 ][
  172. # define SCNdPTR "ld"
  173. # define SCNiPTR "li"
  174. #endif // _WIN64 ]
  175. // The fscanf macros for unsigned integers are:
  176. #define SCNo8 "o"
  177. #define SCNu8 "u"
  178. #define SCNx8 "x"
  179. #define SCNX8 "X"
  180. #define SCNoLEAST8 "o"
  181. #define SCNuLEAST8 "u"
  182. #define SCNxLEAST8 "x"
  183. #define SCNXLEAST8 "X"
  184. #define SCNoFAST8 "o"
  185. #define SCNuFAST8 "u"
  186. #define SCNxFAST8 "x"
  187. #define SCNXFAST8 "X"
  188. #define SCNo16 "ho"
  189. #define SCNu16 "hu"
  190. #define SCNx16 "hx"
  191. #define SCNX16 "hX"
  192. #define SCNoLEAST16 "ho"
  193. #define SCNuLEAST16 "hu"
  194. #define SCNxLEAST16 "hx"
  195. #define SCNXLEAST16 "hX"
  196. #define SCNoFAST16 "ho"
  197. #define SCNuFAST16 "hu"
  198. #define SCNxFAST16 "hx"
  199. #define SCNXFAST16 "hX"
  200. #define SCNo32 "lo"
  201. #define SCNu32 "lu"
  202. #define SCNx32 "lx"
  203. #define SCNX32 "lX"
  204. #define SCNoLEAST32 "lo"
  205. #define SCNuLEAST32 "lu"
  206. #define SCNxLEAST32 "lx"
  207. #define SCNXLEAST32 "lX"
  208. #define SCNoFAST32 "lo"
  209. #define SCNuFAST32 "lu"
  210. #define SCNxFAST32 "lx"
  211. #define SCNXFAST32 "lX"
  212. #define SCNo64 "I64o"
  213. #define SCNu64 "I64u"
  214. #define SCNx64 "I64x"
  215. #define SCNX64 "I64X"
  216. #define SCNoLEAST64 "I64o"
  217. #define SCNuLEAST64 "I64u"
  218. #define SCNxLEAST64 "I64x"
  219. #define SCNXLEAST64 "I64X"
  220. #define SCNoFAST64 "I64o"
  221. #define SCNuFAST64 "I64u"
  222. #define SCNxFAST64 "I64x"
  223. #define SCNXFAST64 "I64X"
  224. #define SCNoMAX "I64o"
  225. #define SCNuMAX "I64u"
  226. #define SCNxMAX "I64x"
  227. #define SCNXMAX "I64X"
  228. #ifdef _WIN64 // [
  229. # define SCNoPTR "I64o"
  230. # define SCNuPTR "I64u"
  231. # define SCNxPTR "I64x"
  232. # define SCNXPTR "I64X"
  233. #else // _WIN64 ][
  234. # define SCNoPTR "lo"
  235. # define SCNuPTR "lu"
  236. # define SCNxPTR "lx"
  237. # define SCNXPTR "lX"
  238. #endif // _WIN64 ]
  239. #endif // __STDC_FORMAT_MACROS ]
  240. // 7.8.2 Functions for greatest-width integer types
  241. // 7.8.2.1 The imaxabs function
  242. #define imaxabs _abs64
  243. // 7.8.2.2 The imaxdiv function
  244. // This is modified version of div() function from Microsoft's div.c found
  245. // in %MSVC.NET%\crt\src\div.c
  246. #ifdef STATIC_IMAXDIV // [
  247. static
  248. #else // STATIC_IMAXDIV ][
  249. _inline
  250. #endif // STATIC_IMAXDIV ]
  251. imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom)
  252. {
  253. imaxdiv_t result;
  254. result.quot = numer / denom;
  255. result.rem = numer % denom;
  256. if (numer < 0 && result.rem > 0) {
  257. // did division wrong; must fix up
  258. ++result.quot;
  259. result.rem -= denom;
  260. }
  261. return result;
  262. }
  263. // 7.8.2.3 The strtoimax and strtoumax functions
  264. #define strtoimax _strtoi64
  265. #define strtoumax _strtoui64
  266. // 7.8.2.4 The wcstoimax and wcstoumax functions
  267. #define wcstoimax _wcstoi64
  268. #define wcstoumax _wcstoui64
  269. #endif // _MSC_VER >= 1800
  270. #endif // _MSC_INTTYPES_H_ ]