1 mike 1.4 //%/////////////////////////////////////////////////////////////////////////////
|
2 mike 1.1 //
3 // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 // DEALINGS IN THE SOFTWARE.
19 //
|
20 mike 1.4 //==============================================================================
|
21 mike 1.1 //
|
22 mike 1.4 // Author: Mike Brasher (mbrasher@bmc.com)
|
23 mike 1.1 //
|
24 mike 1.4 // Modified By:
|
25 karl 1.2 //
|
26 mike 1.4 //%/////////////////////////////////////////////////////////////////////////////
|
27 mike 1.1
28 #ifndef Pegasus_Formatter_h
29 #define Pegasus_Formatter_h
30
31 #include <Pegasus/Common/Config.h>
32 #include <Pegasus/Common/String.h>
33
34 PEGASUS_NAMESPACE_BEGIN
35
36 /**
|
37 karl 1.2 Formatter is a class to build formatted strings from
38 strings that contain variable defintions. The
39 variable definitions in the strings are of the form
40 $<int>
41
42 where <int> is a single digit integer (0 - 9).
43
|
44 karl 1.6 The variable subsituted may be String, Boolean Integer, Unsigned Integer
|
45 karl 1.5 or real.
|
46 karl 1.2
47 The format subsitution may be escaped by preceding the
48 $ with a \
49
50 usage:
51 Formatter::format (FormatString, variable0,.., variable9)
52
53 Example:
54 <pre>
55 int total = 4;
56 int count = 2;
57 String name = "Output"
58 String output = Formatter::format(
59 "total $0 average $1 on $2",
60 total,
61 total/count,
62 name);
63 produces the string
64
65 "total 4 average 2 on Output"
66
67 karl 1.2 </pre>
|
68 mike 1.1 */
69 class PEGASUS_COMMON_LINKAGE Formatter
70 {
71 public:
72
73 class Arg
74 {
75 public:
76
|
77 karl 1.6 enum Type { VOID, STRING, BOOLEAN, INTEGER, UINTEGER, LINTEGER,
78 ULINTEGER, REAL };
|
79 mike 1.1
80 Arg() : _type(VOID)
81 {
82 }
83
84 Arg(const String& x) : _string(x), _type(STRING)
85 {
86 }
87
88 Arg(const char* x) : _string(x), _type(STRING)
89 {
90 }
91
|
92 karl 1.5 Arg(Boolean x) : _boolean(x), _type(BOOLEAN)
93 {
94 }
95
|
96 mike 1.1 Arg(Sint32 x) : _integer(x), _type(INTEGER)
97 {
98 }
99
|
100 karl 1.5 Arg(Uint32 x) : _uinteger(x), _type(UINTEGER)
101 {
102 }
103
|
104 karl 1.6 Arg(Sint64 x) : _lInteger(x), _type(LINTEGER)
105 {
106 }
|
107 karl 1.5
|
108 karl 1.6 Arg(Uint64 x) : _lUInteger(x), _type(ULINTEGER)
109 {
110 }
|
111 mike 1.1 Arg(Real64 x) : _real(x), _type(REAL)
112 {
113 }
114
115 String toString() const;
116
117 private:
118
119 String _string;
120
121 union
122 {
123 Sint32 _integer;
|
124 karl 1.5 Uint32 _uinteger;
|
125 mike 1.1 Real64 _real;
|
126 mike 1.7 int _boolean;
|
127 karl 1.6 Sint64 _lInteger;
128 Uint64 _lUInteger;
|
129 mike 1.1 };
|
130 mike 1.3
131 Type _type;
|
132 mike 1.1 };
|
133 karl 1.2 /** Format function for the formatter
134 */
|
135 mike 1.1 static String format(
136 const String& formatString,
137 const Arg& arg0 = Arg(),
138 const Arg& arg1 = Arg(),
139 const Arg& arg2 = Arg(),
140 const Arg& arg3 = Arg(),
141 const Arg& arg4 = Arg(),
142 const Arg& arg5 = Arg(),
143 const Arg& arg6 = Arg(),
144 const Arg& arg7 = Arg(),
145 const Arg& arg8 = Arg(),
146 const Arg& arg9 = Arg());
147 };
148
149 PEGASUS_NAMESPACE_END
150
151 #endif /* Pegasus_Formatter_h */
|