1 | /* $Id: strformatnum.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Formatter, Single Numbers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_STRING
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include "internal/string.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
|
---|
40 | signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
|
---|
41 | {
|
---|
42 | fFlags &= ~RTSTR_F_BIT_MASK;
|
---|
43 | fFlags |= RTSTR_F_8BIT;
|
---|
44 |
|
---|
45 | ssize_t cchRet;
|
---|
46 | if (cbBuf >= 64)
|
---|
47 | cchRet = RTStrFormatNumber(pszBuf, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
48 | else
|
---|
49 | {
|
---|
50 | char szTmp[64];
|
---|
51 | cchRet = RTStrFormatNumber(szTmp, u8Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
52 | if ((size_t)cchRet < cbBuf)
|
---|
53 | memcpy(pszBuf, szTmp, cchRet + 1);
|
---|
54 | else
|
---|
55 | {
|
---|
56 | if (cbBuf)
|
---|
57 | {
|
---|
58 | memcpy(pszBuf, szTmp, cbBuf - 1);
|
---|
59 | pszBuf[cbBuf - 1] = '\0';
|
---|
60 | }
|
---|
61 | cchRet = VERR_BUFFER_OVERFLOW;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return cchRet;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
|
---|
69 | signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
|
---|
70 | {
|
---|
71 | fFlags &= ~RTSTR_F_BIT_MASK;
|
---|
72 | fFlags |= RTSTR_F_16BIT;
|
---|
73 |
|
---|
74 | ssize_t cchRet;
|
---|
75 | if (cbBuf >= 64)
|
---|
76 | cchRet = RTStrFormatNumber(pszBuf, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
77 | else
|
---|
78 | {
|
---|
79 | char szTmp[64];
|
---|
80 | cchRet = RTStrFormatNumber(szTmp, u16Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
81 | if ((size_t)cchRet <= cbBuf)
|
---|
82 | memcpy(pszBuf, szTmp, cchRet + 1);
|
---|
83 | else
|
---|
84 | {
|
---|
85 | if (cbBuf)
|
---|
86 | {
|
---|
87 | memcpy(pszBuf, szTmp, cbBuf - 1);
|
---|
88 | pszBuf[cbBuf - 1] = '\0';
|
---|
89 | }
|
---|
90 | cchRet = VERR_BUFFER_OVERFLOW;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return cchRet;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
|
---|
98 | signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
|
---|
99 | {
|
---|
100 | fFlags &= ~RTSTR_F_BIT_MASK;
|
---|
101 | fFlags |= RTSTR_F_32BIT;
|
---|
102 |
|
---|
103 | ssize_t cchRet;
|
---|
104 | if (cbBuf >= 64)
|
---|
105 | cchRet = RTStrFormatNumber(pszBuf, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
106 | else
|
---|
107 | {
|
---|
108 | char szTmp[64];
|
---|
109 | cchRet = RTStrFormatNumber(szTmp, u32Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
110 | if ((size_t)cchRet <= cbBuf)
|
---|
111 | memcpy(pszBuf, szTmp, cchRet + 1);
|
---|
112 | else
|
---|
113 | {
|
---|
114 | if (cbBuf)
|
---|
115 | {
|
---|
116 | memcpy(pszBuf, szTmp, cbBuf - 1);
|
---|
117 | pszBuf[cbBuf - 1] = '\0';
|
---|
118 | }
|
---|
119 | cchRet = VERR_BUFFER_OVERFLOW;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return cchRet;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
|
---|
127 | signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
|
---|
128 | {
|
---|
129 | fFlags &= ~RTSTR_F_BIT_MASK;
|
---|
130 | fFlags |= RTSTR_F_64BIT;
|
---|
131 |
|
---|
132 | ssize_t cchRet;
|
---|
133 | if (cbBuf >= 64)
|
---|
134 | cchRet = RTStrFormatNumber(pszBuf, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
135 | else
|
---|
136 | {
|
---|
137 | char szTmp[64];
|
---|
138 | cchRet = RTStrFormatNumber(szTmp, u64Value, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
139 | if ((size_t)cchRet <= cbBuf)
|
---|
140 | memcpy(pszBuf, szTmp, cchRet + 1);
|
---|
141 | else
|
---|
142 | {
|
---|
143 | if (cbBuf)
|
---|
144 | {
|
---|
145 | memcpy(pszBuf, szTmp, cbBuf - 1);
|
---|
146 | pszBuf[cbBuf - 1] = '\0';
|
---|
147 | }
|
---|
148 | cchRet = VERR_BUFFER_OVERFLOW;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | return cchRet;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128, unsigned int uiBase,
|
---|
156 | signed int cchWidth, signed int cchPrecision, uint32_t fFlags)
|
---|
157 | {
|
---|
158 | NOREF(cchWidth); NOREF(cchPrecision);
|
---|
159 | if (uiBase != 16)
|
---|
160 | fFlags |= RTSTR_F_SPECIAL;
|
---|
161 | fFlags &= ~RTSTR_F_BIT_MASK;
|
---|
162 |
|
---|
163 | char szTmp[64+32];
|
---|
164 | size_t cchFirst = RTStrFormatNumber(szTmp, pu128->s.Hi, 16, 0, 0, fFlags | RTSTR_F_64BIT);
|
---|
165 | size_t cchSecond = RTStrFormatNumber(&szTmp[cchFirst], pu128->s.Lo, 16, 8, 0,
|
---|
166 | (fFlags | RTSTR_F_64BIT | RTSTR_F_ZEROPAD) & ~RTSTR_F_SPECIAL);
|
---|
167 | int rc = RTStrCopy(pszBuf, cbBuf, szTmp);
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | return rc;
|
---|
170 | return cchFirst + cchSecond;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
|
---|
175 | signed int cchPrecision, uint32_t fFlags)
|
---|
176 | {
|
---|
177 | NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
|
---|
178 | char szTmp[160];
|
---|
179 |
|
---|
180 | char *pszTmp = szTmp;
|
---|
181 | if (pr80Value->s.fSign)
|
---|
182 | *pszTmp++ = '-';
|
---|
183 | else
|
---|
184 | *pszTmp++ = '+';
|
---|
185 |
|
---|
186 | if (pr80Value->s.uExponent == 0)
|
---|
187 | {
|
---|
188 | if ( !pr80Value->sj64.u63Fraction
|
---|
189 | && pr80Value->sj64.fInteger)
|
---|
190 | *pszTmp++ = '0';
|
---|
191 | /* else: Denormal, handled way below. */
|
---|
192 | }
|
---|
193 | else if (pr80Value->sj64.uExponent == UINT16_C(0x7fff))
|
---|
194 | {
|
---|
195 | /** @todo Figure out Pseudo inf/nan... */
|
---|
196 | if (pr80Value->sj64.fInteger)
|
---|
197 | *pszTmp++ = 'P';
|
---|
198 | if (pr80Value->sj64.u63Fraction == 0)
|
---|
199 | {
|
---|
200 | *pszTmp++ = 'I';
|
---|
201 | *pszTmp++ = 'n';
|
---|
202 | *pszTmp++ = 'f';
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | *pszTmp++ = 'N';
|
---|
207 | *pszTmp++ = 'a';
|
---|
208 | *pszTmp++ = 'N';
|
---|
209 | }
|
---|
210 | }
|
---|
211 | if (pszTmp != &szTmp[1])
|
---|
212 | *pszTmp = '\0';
|
---|
213 | else
|
---|
214 | {
|
---|
215 | *pszTmp++ = pr80Value->sj64.fInteger ? '1' : '0';
|
---|
216 | *pszTmp++ = 'm';
|
---|
217 | pszTmp += RTStrFormatNumber(pszTmp, pr80Value->sj64.u63Fraction, 16, 2+16, 0,
|
---|
218 | RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD | RTSTR_F_64BIT);
|
---|
219 |
|
---|
220 | *pszTmp++ = 'e';
|
---|
221 | pszTmp += RTStrFormatNumber(pszTmp, (int32_t)pr80Value->sj64.uExponent - 16383, 10, 0, 0,
|
---|
222 | RTSTR_F_ZEROPAD | RTSTR_F_32BIT | RTSTR_F_VALSIGNED);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Copy out the result.
|
---|
227 | */
|
---|
228 | ssize_t cchRet = pszTmp - &szTmp[0];
|
---|
229 | if ((size_t)cchRet <= cbBuf)
|
---|
230 | memcpy(pszBuf, szTmp, cchRet + 1);
|
---|
231 | else
|
---|
232 | {
|
---|
233 | if (cbBuf)
|
---|
234 | {
|
---|
235 | memcpy(pszBuf, szTmp, cbBuf - 1);
|
---|
236 | pszBuf[cbBuf - 1] = '\0';
|
---|
237 | }
|
---|
238 | cchRet = VERR_BUFFER_OVERFLOW;
|
---|
239 | }
|
---|
240 | return cchRet;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
|
---|
245 | signed int cchPrecision, uint32_t fFlags)
|
---|
246 | {
|
---|
247 | RTFLOAT80U2 r80ValueU2;
|
---|
248 | RT_ZERO(r80ValueU2);
|
---|
249 | r80ValueU2.s.fSign = pr80Value->s.fSign;
|
---|
250 | r80ValueU2.s.uExponent = pr80Value->s.uExponent;
|
---|
251 | r80ValueU2.s.u64Mantissa = pr80Value->s.u64Mantissa;
|
---|
252 | return RTStrFormatR80u2(pszBuf, cbBuf, &r80ValueU2, cchWidth, cchPrecision, fFlags);
|
---|
253 | }
|
---|
254 |
|
---|