1 | // |
---|
2 | // NumberFormatter.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/NumberFormatter.cpp#4 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: NumberFormatter |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/NumberFormatter.h" |
---|
38 | #include <cstdio> |
---|
39 | #include <cctype> |
---|
40 | |
---|
41 | |
---|
42 | #if defined(_MSC_VER) |
---|
43 | #define I64_FMT "I64" |
---|
44 | #elif defined(__APPLE__) |
---|
45 | #define I64_FMT "q" |
---|
46 | #else |
---|
47 | #define I64_FMT "ll" |
---|
48 | #endif |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | |
---|
53 | |
---|
54 | void NumberFormatter::append(std::string& str, int value) |
---|
55 | { |
---|
56 | char buffer[64]; |
---|
57 | std::sprintf(buffer, "%d", value); |
---|
58 | str.append(buffer); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void NumberFormatter::append(std::string& str, int value, int width) |
---|
63 | { |
---|
64 | poco_assert (width > 0 && width < 64); |
---|
65 | |
---|
66 | char buffer[64]; |
---|
67 | std::sprintf(buffer, "%*d", width, value); |
---|
68 | str.append(buffer); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void NumberFormatter::append0(std::string& str, int value, int width) |
---|
73 | { |
---|
74 | poco_assert (width > 0 && width < 64); |
---|
75 | |
---|
76 | char buffer[64]; |
---|
77 | std::sprintf(buffer, "%0*d", width, value); |
---|
78 | str.append(buffer); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void NumberFormatter::appendHex(std::string& str, int value) |
---|
83 | { |
---|
84 | char buffer[64]; |
---|
85 | std::sprintf(buffer, "%X", value); |
---|
86 | str.append(buffer); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void NumberFormatter::appendHex(std::string& str, int value, int width) |
---|
91 | { |
---|
92 | poco_assert (width > 0 && width < 64); |
---|
93 | |
---|
94 | char buffer[64]; |
---|
95 | std::sprintf(buffer, "%0*X", width, value); |
---|
96 | str.append(buffer); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void NumberFormatter::append(std::string& str, unsigned value) |
---|
101 | { |
---|
102 | char buffer[64]; |
---|
103 | std::sprintf(buffer, "%u", value); |
---|
104 | str.append(buffer); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void NumberFormatter::append(std::string& str, unsigned value, int width) |
---|
109 | { |
---|
110 | poco_assert (width > 0 && width < 64); |
---|
111 | |
---|
112 | char buffer[64]; |
---|
113 | std::sprintf(buffer, "%*u", width, value); |
---|
114 | str.append(buffer); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | void NumberFormatter::append0(std::string& str, unsigned int value, int width) |
---|
119 | { |
---|
120 | poco_assert (width > 0 && width < 64); |
---|
121 | |
---|
122 | char buffer[64]; |
---|
123 | std::sprintf(buffer, "%0*u", width, value); |
---|
124 | str.append(buffer); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void NumberFormatter::appendHex(std::string& str, unsigned value) |
---|
129 | { |
---|
130 | char buffer[64]; |
---|
131 | std::sprintf(buffer, "%X", value); |
---|
132 | str.append(buffer); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void NumberFormatter::appendHex(std::string& str, unsigned value, int width) |
---|
137 | { |
---|
138 | poco_assert (width > 0 && width < 64); |
---|
139 | |
---|
140 | char buffer[64]; |
---|
141 | std::sprintf(buffer, "%0*X", width, value); |
---|
142 | str.append(buffer); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void NumberFormatter::append(std::string& str, long value) |
---|
147 | { |
---|
148 | char buffer[64]; |
---|
149 | std::sprintf(buffer, "%ld", value); |
---|
150 | str.append(buffer); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | void NumberFormatter::append(std::string& str, long value, int width) |
---|
155 | { |
---|
156 | poco_assert (width > 0 && width < 64); |
---|
157 | |
---|
158 | char buffer[64]; |
---|
159 | std::sprintf(buffer, "%*ld", width, value); |
---|
160 | str.append(buffer); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | void NumberFormatter::append0(std::string& str, long value, int width) |
---|
165 | { |
---|
166 | poco_assert (width > 0 && width < 64); |
---|
167 | |
---|
168 | char buffer[64]; |
---|
169 | std::sprintf(buffer, "%0*ld", width, value); |
---|
170 | str.append(buffer); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | void NumberFormatter::appendHex(std::string& str, long value) |
---|
175 | { |
---|
176 | char buffer[64]; |
---|
177 | std::sprintf(buffer, "%lX", value); |
---|
178 | str.append(buffer); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | void NumberFormatter::appendHex(std::string& str, long value, int width) |
---|
183 | { |
---|
184 | poco_assert (width > 0 && width < 64); |
---|
185 | |
---|
186 | char buffer[64]; |
---|
187 | std::sprintf(buffer, "%0*lX", width, value); |
---|
188 | str.append(buffer); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | void NumberFormatter::append(std::string& str, unsigned long value) |
---|
193 | { |
---|
194 | char buffer[64]; |
---|
195 | std::sprintf(buffer, "%lu", value); |
---|
196 | str.append(buffer); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | void NumberFormatter::append(std::string& str, unsigned long value, int width) |
---|
201 | { |
---|
202 | poco_assert (width > 0 && width < 64); |
---|
203 | |
---|
204 | char buffer[64]; |
---|
205 | std::sprintf(buffer, "%*lu", width, value); |
---|
206 | str.append(buffer); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | void NumberFormatter::append0(std::string& str, unsigned long value, int width) |
---|
211 | { |
---|
212 | poco_assert (width > 0 && width < 64); |
---|
213 | |
---|
214 | char buffer[64]; |
---|
215 | std::sprintf(buffer, "%0*lu", width, value); |
---|
216 | str.append(buffer); |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | void NumberFormatter::appendHex(std::string& str, unsigned long value) |
---|
221 | { |
---|
222 | char buffer[64]; |
---|
223 | std::sprintf(buffer, "%lX", value); |
---|
224 | str.append(buffer); |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | void NumberFormatter::appendHex(std::string& str, unsigned long value, int width) |
---|
229 | { |
---|
230 | poco_assert (width > 0 && width < 64); |
---|
231 | |
---|
232 | char buffer[64]; |
---|
233 | std::sprintf(buffer, "%0*lX", width, value); |
---|
234 | str.append(buffer); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | #if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) |
---|
239 | |
---|
240 | |
---|
241 | void NumberFormatter::append(std::string& str, Int64 value) |
---|
242 | { |
---|
243 | char buffer[64]; |
---|
244 | std::sprintf(buffer, "%"I64_FMT"d", value); |
---|
245 | str.append(buffer); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | void NumberFormatter::append(std::string& str, Int64 value, int width) |
---|
250 | { |
---|
251 | poco_assert (width > 0 && width < 64); |
---|
252 | |
---|
253 | char buffer[64]; |
---|
254 | std::sprintf(buffer, "%*"I64_FMT"d", width, value); |
---|
255 | str.append(buffer); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | void NumberFormatter::append0(std::string& str, Int64 value, int width) |
---|
260 | { |
---|
261 | poco_assert (width > 0 && width < 64); |
---|
262 | |
---|
263 | char buffer[64]; |
---|
264 | std::sprintf(buffer, "%0*"I64_FMT"d", width, value); |
---|
265 | str.append(buffer); |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | void NumberFormatter::appendHex(std::string& str, Int64 value) |
---|
270 | { |
---|
271 | char buffer[64]; |
---|
272 | std::sprintf(buffer, "%"I64_FMT"X", value); |
---|
273 | str.append(buffer); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void NumberFormatter::appendHex(std::string& str, Int64 value, int width) |
---|
278 | { |
---|
279 | poco_assert (width > 0 && width < 64); |
---|
280 | |
---|
281 | char buffer[64]; |
---|
282 | std::sprintf(buffer, "%0*"I64_FMT"X", width, value); |
---|
283 | str.append(buffer); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | void NumberFormatter::append(std::string& str, UInt64 value) |
---|
288 | { |
---|
289 | char buffer[64]; |
---|
290 | std::sprintf(buffer, "%"I64_FMT"u", value); |
---|
291 | str.append(buffer); |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | void NumberFormatter::append(std::string& str, UInt64 value, int width) |
---|
296 | { |
---|
297 | poco_assert (width > 0 && width < 64); |
---|
298 | |
---|
299 | char buffer[64]; |
---|
300 | std::sprintf(buffer, "%*"I64_FMT"u", width, value); |
---|
301 | str.append(buffer); |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | void NumberFormatter::append0(std::string& str, UInt64 value, int width) |
---|
306 | { |
---|
307 | poco_assert (width > 0 && width < 64); |
---|
308 | |
---|
309 | char buffer[64]; |
---|
310 | std::sprintf(buffer, "%0*"I64_FMT"u", width, value); |
---|
311 | str.append(buffer); |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | void NumberFormatter::appendHex(std::string& str, UInt64 value) |
---|
316 | { |
---|
317 | char buffer[64]; |
---|
318 | std::sprintf(buffer, "%"I64_FMT"X", value); |
---|
319 | str.append(buffer); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | void NumberFormatter::appendHex(std::string& str, UInt64 value, int width) |
---|
324 | { |
---|
325 | poco_assert (width > 0 && width < 64); |
---|
326 | |
---|
327 | char buffer[64]; |
---|
328 | std::sprintf(buffer, "%0*"I64_FMT"X", width, value); |
---|
329 | str.append(buffer); |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | #endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT) |
---|
334 | |
---|
335 | |
---|
336 | void NumberFormatter::append(std::string& str, float value) |
---|
337 | { |
---|
338 | char buffer[64]; |
---|
339 | std::sprintf(buffer, "%.*g", 8, (double) value); |
---|
340 | str.append(buffer); |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | void NumberFormatter::append(std::string& str, double value) |
---|
345 | { |
---|
346 | char buffer[64]; |
---|
347 | std::sprintf(buffer, "%.*g", 16, value); |
---|
348 | str.append(buffer); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | void NumberFormatter::append(std::string& str, double value, int precision) |
---|
353 | { |
---|
354 | poco_assert (precision >= 0 && precision < 32); |
---|
355 | |
---|
356 | char buffer[64]; |
---|
357 | std::sprintf(buffer, "%.*f", precision, value); |
---|
358 | str.append(buffer); |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | void NumberFormatter::append(std::string& str, double value, int width, int precision) |
---|
363 | { |
---|
364 | poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width); |
---|
365 | |
---|
366 | char buffer[64]; |
---|
367 | std::sprintf(buffer, "%*.*f", width, precision, value); |
---|
368 | str.append(buffer); |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | void NumberFormatter::append(std::string& str, const void* ptr) |
---|
373 | { |
---|
374 | char buffer[24]; |
---|
375 | #if defined(POCO_PTR_IS_64_BIT) |
---|
376 | std::sprintf(buffer, "%016"I64_FMT"X", (UIntPtr) ptr); |
---|
377 | #else |
---|
378 | std::sprintf(buffer, "%08lX", (UIntPtr) ptr); |
---|
379 | #endif |
---|
380 | str.append(buffer); |
---|
381 | } |
---|
382 | |
---|
383 | |
---|
384 | } // namespace Poco |
---|