source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/NumberFormatter.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 19.7 KB
Line 
1//
2// NumberFormatter.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/NumberFormatter.h#3 $
5//
6// Library: Foundation
7// Package: Core
8// Module:  NumberFormatter
9//
10// Definition of the NumberFormatter class.
11//
12// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38
39#ifndef Foundation_NumberFormatter_INCLUDED
40#define Foundation_NumberFormatter_INCLUDED
41
42
43#include "Poco/Foundation.h"
44
45
46namespace Poco {
47
48
49class Foundation_API NumberFormatter
50        /// The NumberFormatter class provides static methods
51        /// for formatting numeric values into strings.
52        ///
53        /// There are two kind of static member functions:
54        ///    * format* functions return a std::string containing
55        ///      the formatted value.
56        ///    * append* functions append the formatted value to
57        ///      an existing string.
58        ///
59        /// Internally, std::sprintf() is used to do the actual
60        /// formatting.
61{
62public:
63        static std::string format(int value);
64                /// Formats an integer value in decimal notation.
65
66        static std::string format(int value, int width);
67                /// Formats an integer value in decimal notation,
68                /// right justified in a field having at least
69                /// the specified width.
70
71        static std::string format0(int value, int width);
72                /// Formats an integer value in decimal notation,
73                /// right justified and zero-padded in a field
74                /// having at least the specified width.
75
76        static std::string formatHex(int value);
77                /// Formats an int value in hexadecimal notation.
78                /// The value is treated as unsigned.
79
80        static std::string formatHex(int value, int width);
81                /// Formats a int value in hexadecimal notation,
82                /// right justified and zero-padded in
83                /// a field having at least the specified width.
84                /// The value is treated as unsigned.
85
86        static std::string format(unsigned value);
87                /// Formats an unsigned int value in decimal notation.
88
89        static std::string format(unsigned value, int width);
90                /// Formats an unsigned long int in decimal notation,
91                /// right justified in a field having at least the
92                /// specified width.
93
94        static std::string format0(unsigned int value, int width);
95                /// Formats an unsigned int value in decimal notation,
96                /// right justified and zero-padded in a field having at
97                /// least the specified width.
98
99        static std::string formatHex(unsigned value);
100                /// Formats an unsigned int value in hexadecimal notation.
101
102        static std::string formatHex(unsigned value, int width);
103                /// Formats a int value in hexadecimal notation,
104                /// right justified and zero-padded in
105                /// a field having at least the specified width.
106
107        static std::string format(long value);
108                /// Formats a long value in decimal notation.
109
110        static std::string format(long value, int width);
111                /// Formats a long value in decimal notation,
112                /// right justified in a field having at least the
113                /// specified width.
114
115        static std::string format0(long value, int width);
116                /// Formats a long value in decimal notation,
117                /// right justified and zero-padded in a field
118                /// having at least the specified width.
119
120        static std::string formatHex(long value);
121                /// Formats an unsigned long value in hexadecimal notation.
122                /// The value is treated as unsigned.
123
124        static std::string formatHex(long value, int width);
125                /// Formats an unsigned long value in hexadecimal notation,
126                /// right justified and zero-padded in a field having at least the
127                /// specified width.
128                /// The value is treated as unsigned.
129
130        static std::string format(unsigned long value);
131                /// Formats an unsigned long value in decimal notation.
132
133        static std::string format(unsigned long value, int width);
134                /// Formats an unsigned long value in decimal notation,
135                /// right justified in a field having at least the specified
136                /// width.
137
138        static std::string format0(unsigned long value, int width);
139                /// Formats an unsigned long value in decimal notation,
140                /// right justified and zero-padded
141                /// in a field having at least the specified width.
142
143        static std::string formatHex(unsigned long value);
144                /// Formats an unsigned long value in hexadecimal notation.
145
146        static std::string formatHex(unsigned long value, int width);
147                /// Formats an unsigned long value in hexadecimal notation,
148                /// right justified and zero-padded in a field having at least the
149                /// specified width.
150
151#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
152
153        static std::string format(Int64 value);
154                /// Formats a 64-bit integer value in decimal notation.
155
156        static std::string format(Int64 value, int width);
157                /// Formats a 64-bit integer value in decimal notation,
158                /// right justified in a field having at least the specified width.
159
160        static std::string format0(Int64 value, int width);
161                /// Formats a 64-bit integer value in decimal notation,
162                /// right justified and zero-padded in a field having at least
163                /// the specified width.
164
165        static std::string formatHex(Int64 value);
166                /// Formats a 64-bit integer value in hexadecimal notation.
167                /// The value is treated as unsigned.
168
169        static std::string formatHex(Int64 value, int width);
170                /// Formats a 64-bit integer value in hexadecimal notation,
171                /// right justified and zero-padded in a field having at least
172                /// the specified width.
173                /// The value is treated as unsigned.
174
175        static std::string format(UInt64 value);
176                /// Formats an unsigned 64-bit integer value in decimal notation.
177
178        static std::string format(UInt64 value, int width);
179                /// Formats an unsigned 64-bit integer value in decimal notation,
180                /// right justified in a field having at least the specified width.
181
182        static std::string format0(UInt64 value, int width);
183                /// Formats an unsigned 64-bit integer value in decimal notation,
184                /// right justified and zero-padded in a field having at least the
185                /// specified width.
186
187        static std::string formatHex(UInt64 value);
188                /// Formats a 64-bit integer value in hexadecimal notation.
189
190        static std::string formatHex(UInt64 value, int width);
191                /// Formats a 64-bit integer value in hexadecimal notation,
192                /// right justified and zero-padded in a field having at least
193                /// the specified width.
194
195#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
196
197        static std::string format(float value);
198                /// Formats a float value in decimal floating-point notation,
199                /// according to std::printf's %g format with a precision of 8 fractional digits.
200
201        static std::string format(double value);
202                /// Formats a double value in decimal floating-point notation,
203                /// according to std::printf's %g format with a precision of 16 fractional digits.
204
205        static std::string format(double value, int precision);
206                /// Formats a double value in decimal floating-point notation,
207                /// according to std::printf's %f format with the given precision.
208
209        static std::string format(double value, int width, int precision);
210                /// Formats a double value in decimal floating-point notation,
211                /// right justified in a field of the specified width,
212                /// with the number of fractional digits given in precision.
213
214        static std::string format(const void* ptr);
215                /// Formats a pointer in an eight (32-bit architectures) or
216                /// sixteen (64-bit architectures) characters wide
217                /// field in hexadecimal notation.
218
219        static void append(std::string& str, int value);
220                /// Formats an integer value in decimal notation.
221
222        static void append(std::string& str, int value, int width);
223                /// Formats an integer value in decimal notation,
224                /// right justified in a field having at least
225                /// the specified width.
226
227        static void append0(std::string& str, int value, int width);
228                /// Formats an integer value in decimal notation,
229                /// right justified and zero-padded in a field
230                /// having at least the specified width.
231
232        static void appendHex(std::string& str, int value);
233                /// Formats an int value in hexadecimal notation.
234                /// The value is treated as unsigned.
235
236        static void appendHex(std::string& str, int value, int width);
237                /// Formats a int value in hexadecimal notation,
238                /// right justified and zero-padded in
239                /// a field having at least the specified width.
240                /// The value is treated as unsigned.
241
242        static void append(std::string& str, unsigned value);
243                /// Formats an unsigned int value in decimal notation.
244
245        static void append(std::string& str, unsigned value, int width);
246                /// Formats an unsigned long int in decimal notation,
247                /// right justified in a field having at least the
248                /// specified width.
249
250        static void append0(std::string& str, unsigned int value, int width);
251                /// Formats an unsigned int value in decimal notation,
252                /// right justified and zero-padded in a field having at
253                /// least the specified width.
254
255        static void appendHex(std::string& str, unsigned value);
256                /// Formats an unsigned int value in hexadecimal notation.
257
258        static void appendHex(std::string& str, unsigned value, int width);
259                /// Formats a int value in hexadecimal notation,
260                /// right justified and zero-padded in
261                /// a field having at least the specified width.
262
263        static void append(std::string& str, long value);
264                /// Formats a long value in decimal notation.
265
266        static void append(std::string& str, long value, int width);
267                /// Formats a long value in decimal notation,
268                /// right justified in a field having at least the
269                /// specified width.
270
271        static void append0(std::string& str, long value, int width);
272                /// Formats a long value in decimal notation,
273                /// right justified and zero-padded in a field
274                /// having at least the specified width.
275
276        static void appendHex(std::string& str, long value);
277                /// Formats an unsigned long value in hexadecimal notation.
278                /// The value is treated as unsigned.
279
280        static void appendHex(std::string& str, long value, int width);
281                /// Formats an unsigned long value in hexadecimal notation,
282                /// right justified and zero-padded in a field having at least the
283                /// specified width.
284                /// The value is treated as unsigned.
285
286        static void append(std::string& str, unsigned long value);
287                /// Formats an unsigned long value in decimal notation.
288
289        static void append(std::string& str, unsigned long value, int width);
290                /// Formats an unsigned long value in decimal notation,
291                /// right justified in a field having at least the specified
292                /// width.
293
294        static void append0(std::string& str, unsigned long value, int width);
295                /// Formats an unsigned long value in decimal notation,
296                /// right justified and zero-padded
297                /// in a field having at least the specified width.
298
299        static void appendHex(std::string& str, unsigned long value);
300                /// Formats an unsigned long value in hexadecimal notation.
301
302        static void appendHex(std::string& str, unsigned long value, int width);
303                /// Formats an unsigned long value in hexadecimal notation,
304                /// right justified and zero-padded in a field having at least the
305                /// specified width.
306
307#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
308
309        static void append(std::string& str, Int64 value);
310                /// Formats a 64-bit integer value in decimal notation.
311
312        static void append(std::string& str, Int64 value, int width);
313                /// Formats a 64-bit integer value in decimal notation,
314                /// right justified in a field having at least the specified width.
315
316        static void append0(std::string& str, Int64 value, int width);
317                /// Formats a 64-bit integer value in decimal notation,
318                /// right justified and zero-padded in a field having at least
319                /// the specified width.
320
321        static void appendHex(std::string& str, Int64 value);
322                /// Formats a 64-bit integer value in hexadecimal notation.
323                /// The value is treated as unsigned.
324
325        static void appendHex(std::string& str, Int64 value, int width);
326                /// Formats a 64-bit integer value in hexadecimal notation,
327                /// right justified and zero-padded in a field having at least
328                /// the specified width.
329                /// The value is treated as unsigned.
330
331        static void append(std::string& str, UInt64 value);
332                /// Formats an unsigned 64-bit integer value in decimal notation.
333
334        static void append(std::string& str, UInt64 value, int width);
335                /// Formats an unsigned 64-bit integer value in decimal notation,
336                /// right justified in a field having at least the specified width.
337
338        static void append0(std::string& str, UInt64 value, int width);
339                /// Formats an unsigned 64-bit integer value in decimal notation,
340                /// right justified and zero-padded in a field having at least the
341                /// specified width.
342
343        static void appendHex(std::string& str, UInt64 value);
344                /// Formats a 64-bit integer value in hexadecimal notation.
345
346        static void appendHex(std::string& str, UInt64 value, int width);
347                /// Formats a 64-bit integer value in hexadecimal notation,
348                /// right justified and zero-padded in a field having at least
349                /// the specified width.
350
351#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
352
353        static void append(std::string& str, float value);
354                /// Formats a float value in decimal floating-point notation,
355                /// according to std::printf's %g format with a precision of 8 fractional digits.
356
357        static void append(std::string& str, double value);
358                /// Formats a double value in decimal floating-point notation,
359                /// according to std::printf's %g format with a precision of 16 fractional digits.
360
361        static void append(std::string& str, double value, int precision);
362                /// Formats a double value in decimal floating-point notation,
363                /// according to std::printf's %f format with the given precision.
364
365        static void append(std::string& str, double value, int width, int precision);
366                /// Formats a double value in decimal floating-point notation,
367                /// right justified in a field of the specified width,
368                /// with the number of fractional digits given in precision.
369
370        static void append(std::string& str, const void* ptr);
371                /// Formats a pointer in an eight (32-bit architectures) or
372                /// sixteen (64-bit architectures) characters wide
373                /// field in hexadecimal notation.
374};
375
376
377//
378// inlines
379//
380inline std::string NumberFormatter::format(int value)
381{
382        std::string result;
383        append(result, value);
384        return result;
385}
386
387
388inline std::string NumberFormatter::format(int value, int width)
389{
390        std::string result;
391        append(result, value, width);
392        return result;
393}
394
395
396inline std::string NumberFormatter::format0(int value, int width)
397{
398        std::string result;
399        append0(result, value, width);
400        return result;
401}
402
403
404inline std::string NumberFormatter::formatHex(int value)
405{
406        std::string result;
407        appendHex(result, value);
408        return result;
409}
410
411
412inline std::string NumberFormatter::formatHex(int value, int width)
413{
414        std::string result;
415        appendHex(result, value, width);
416        return result;
417}
418
419
420inline std::string NumberFormatter::format(unsigned value)
421{
422        std::string result;
423        append(result, value);
424        return result;
425}
426
427
428inline std::string NumberFormatter::format(unsigned value, int width)
429{
430        std::string result;
431        append(result, value, width);
432        return result;
433}
434
435
436inline std::string NumberFormatter::format0(unsigned int value, int width)
437{
438        std::string result;
439        append0(result, value, width);
440        return result;
441}
442
443
444inline std::string NumberFormatter::formatHex(unsigned value)
445{
446        std::string result;
447        appendHex(result, value);
448        return result;
449}
450
451
452inline std::string NumberFormatter::formatHex(unsigned value, int width)
453{
454        std::string result;
455        appendHex(result, value, width);
456        return result;
457}
458
459
460inline std::string NumberFormatter::format(long value)
461{
462        std::string result;
463        append(result, value);
464        return result;
465}
466
467
468inline std::string NumberFormatter::format(long value, int width)
469{
470        std::string result;
471        append(result, value, width);
472        return result;
473}
474
475
476inline std::string NumberFormatter::format0(long value, int width)
477{
478        std::string result;
479        append0(result, value, width);
480        return result;
481}
482
483
484inline std::string NumberFormatter::formatHex(long value)
485{
486        std::string result;
487        appendHex(result, value);
488        return result;
489}
490
491
492inline std::string NumberFormatter::formatHex(long value, int width)
493{
494        std::string result;
495        appendHex(result, value, width);
496        return result;
497}
498
499
500inline std::string NumberFormatter::format(unsigned long value)
501{
502        std::string result;
503        append(result, value);
504        return result;
505}
506
507
508inline std::string NumberFormatter::format(unsigned long value, int width)
509{
510        std::string result;
511        append(result, value, width);
512        return result;
513}
514
515
516inline std::string NumberFormatter::format0(unsigned long value, int width)
517{
518        std::string result;
519        append0(result, value, width);
520        return result;
521}
522
523
524inline std::string NumberFormatter::formatHex(unsigned long value)
525{
526        std::string result;
527        appendHex(result, value);
528        return result;
529}
530
531
532inline std::string NumberFormatter::formatHex(unsigned long value, int width)
533{
534        std::string result;
535        appendHex(result, value, width);
536        return result;
537}
538
539
540#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
541
542
543inline std::string NumberFormatter::format(Int64 value)
544{
545        std::string result;
546        append(result, value);
547        return result;
548}
549
550
551inline std::string NumberFormatter::format(Int64 value, int width)
552{
553        std::string result;
554        append(result, value, width);
555        return result;
556}
557
558
559inline std::string NumberFormatter::format0(Int64 value, int width)
560{
561        std::string result;
562        append0(result, value, width);
563        return result;
564}
565
566
567inline std::string NumberFormatter::formatHex(Int64 value)
568{
569        std::string result;
570        appendHex(result, value);
571        return result;
572}
573
574
575inline std::string NumberFormatter::formatHex(Int64 value, int width)
576{
577        std::string result;
578        appendHex(result, value, width);
579        return result;
580}
581
582
583inline std::string NumberFormatter::format(UInt64 value)
584{
585        std::string result;
586        append(result, value);
587        return result;
588}
589
590
591inline std::string NumberFormatter::format(UInt64 value, int width)
592{
593        std::string result;
594        append(result, value, width);
595        return result;
596}
597
598
599inline std::string NumberFormatter::format0(UInt64 value, int width)
600{
601        std::string result;
602        append0(result, value, width);
603        return result;
604}
605
606
607inline std::string NumberFormatter::formatHex(UInt64 value)
608{
609        std::string result;
610        appendHex(result, value);
611        return result;
612}
613
614
615inline std::string NumberFormatter::formatHex(UInt64 value, int width)
616{
617        std::string result;
618        appendHex(result, value, width);
619        return result;
620}
621
622
623#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
624
625
626inline std::string NumberFormatter::format(float value)
627{
628        std::string result;
629        append(result, value);
630        return result;
631}
632
633
634inline std::string NumberFormatter::format(double value)
635{
636        std::string result;
637        append(result, value);
638        return result;
639}
640
641
642inline std::string NumberFormatter::format(double value, int precision)
643{
644        std::string result;
645        append(result, value, precision);
646        return result;
647}
648
649
650inline std::string NumberFormatter::format(double value, int width, int precision)
651{
652        std::string result;
653        append(result, value, width, precision);
654        return result;
655}
656
657
658inline std::string NumberFormatter::format(const void* ptr)
659{
660        std::string result;
661        append(result, ptr);
662        return result;
663}
664
665
666} // namespace Poco
667
668
669#endif // Foundation_NumberFormatter_INCLUDED
Note: See TracBrowser for help on using the repository browser.