/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/dprintf.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/dprintf.hpp Source File
Ratpac-two
dprintf.hpp
1 #ifndef DPRINTF_HPP
2 #define DPRINTF_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  Provides an sprintf-like function acting on STL strings rather than char* so
10  that there is no possibility of overflowing the string buffer. The 'd' stands
11  for "dynamic" in that the string is a dynamic string whereas a char* buffer
12  would be static (in size that is, not static in C terms).
13 
14 int dprintf (std::string&, const char* format, ...);
15 
16  Like sprintf(), formats the message by appending to the std::string (using the +=
17  operator) according to the formatting codes in the format string. The return int
18  is the number of characters generated by this call, i.e. the increase in the
19  length of the std::string.
20 
21 int vdprintf (std::string& formatted, const char* format, va_list args);
22 
23  As above, but using a pre-initialised va_args argument list. Useful for nesting
24  printf calls within variable argument functions.
25 
26 std::string dformat (const char* format, ...);
27 
28  Similar to dprintf() above, except the result is formatted into a new std::string
29  which is returned by the function. Very useful for inline calls within a textio
30  expression.
31  e.g. fout << "Total: " << sformat("%6i",t) << newline;
32 
33 std::string vdformat (const char* format, va_list);
34 
35  As above, but using a pre-initialised va_args argument list. Useful for nesting
36  sformat calls within variable argument functions.
37 
38 The result supports the following "C" format codes:
39 
40  % [ flags ] [ field ] [ . precision ] [ modifier ] [ conversion ]
41 
42  flags:
43  - - left justified
44  + - print sign for +ve numbers
45  ' ' - leading space where + sign would be
46  0 - leading zeros to width of field
47  # - alternate format
48 
49  field:
50  a numeric argument specifying the field width - default = 0
51  * means take the next va_arg as the field width - if negative then left justify
52 
53  precision:
54  a numeric argument the meaning of which depends on the conversion -
55  - %s - max characters from a string - default = strlen()
56  - %e, %f - decimal places to be displayed - default = 6
57  - %g - significant digits to be displayed - default = 6
58  - all integer conversions - minimum digits to display - default = 0
59  * means take the next va_arg as the field width - if negative then left justify
60 
61  modifier:
62  h - short or unsigned short
63  l - long or unsigned long
64  L - long double
65 
66  conversions:
67  d, i - short/int/long as decimal
68  u - short/int/long as unsigned decimal
69  o - short/int/long as unsigned octal - # adds leading 0
70  x, X - short/int/long as unsigned hexadecimal - # adds leading 0x
71  c - char
72  s - char*
73  f - double/long double as fixed point
74  e, E - double/long double as floating point
75  g, G - double/long double as fixed point/floating point depending on value
76  p - void* as unsigned hexadecimal
77  % - literal %
78  n - int* as recipient of length of formatted string so far
79 
80 ------------------------------------------------------------------------------*/
81 #include <stdarg.h>
82 
83 #include <cstring>
84 #include <string>
85 
86 #include "os_fixes.hpp"
87 int dprintf(std::string& formatted, const char* format, ...);
88 int vdprintf(std::string& formatted, const char* format, va_list args);
89 
90 std::string dformat(const char* format, ...);
91 std::string vdformat(const char* format, va_list);
92 
93 #endif