1 martin 1.29 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.30 //
|
3 martin 1.29 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.30 //
|
10 martin 1.29 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.30 //
|
17 martin 1.29 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.30 //
|
20 martin 1.29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.30 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.30 //
|
28 martin 1.29 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.11 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_Formatter_h
33 #define Pegasus_Formatter_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/String.h>
|
37 kumpf 1.14 #include <Pegasus/Common/Linkage.h>
|
38 mike 1.22 #include <Pegasus/Common/StrLit.h>
|
39 chuck 1.17
|
40 mike 1.11 PEGASUS_NAMESPACE_BEGIN
41
|
42 kumpf 1.25 /**
|
43 mike 1.11 Formatter is a class to build formatted strings from
44 strings that contain variable defintions. The
45 variable definitions in the strings are of the form
46 $<int>
|
47 kumpf 1.27
|
48 mike 1.11 where <int> is a single digit integer (0 - 9).
|
49 kumpf 1.31
|
50 mike 1.11 The variable subsituted may be String, Boolean Integer, Unsigned Integer
|
51 kumpf 1.27 or real.
52
|
53 mike 1.11 The format subsitution may be escaped by preceding the
54 $ with a \
|
55 kumpf 1.27
|
56 mike 1.11 usage:
57 Formatter::format (FormatString, variable0,.., variable9)
|
58 kumpf 1.27
|
59 mike 1.11 Example:
60 <pre>
61 int total = 4;
62 int count = 2;
63 String name = "Output"
64 String output = Formatter::format(
|
65 kumpf 1.27 "total $0 average $1 on $2",
|
66 kumpf 1.26 total,
67 total/count,
68 name);
69
|
70 mike 1.11 produces the string
|
71 kumpf 1.26
|
72 mike 1.11 "total 4 average 2 on Output"
|
73 kumpf 1.27
|
74 mike 1.11 </pre>
75 */
76 class PEGASUS_COMMON_LINKAGE Formatter
77 {
78 public:
79
80 class Arg
81 {
82 public:
83
|
84 kumpf 1.27 enum Type { VOIDT, STRING, CSTRLIT, BOOLEAN, INTEGER,
|
85 kumpf 1.26 UINTEGER, LINTEGER, ULINTEGER, REAL };
86
87 Arg() : _type(VOIDT)
88 {
89 }
90
91 Arg(const String& x) : _string(x), _type(STRING)
92 {
93 }
94
95 Arg(const char* x) : _string(x), _type(STRING)
96 {
97 }
98
99 Arg(const StrLit& x) : _cstrlit(&x), _type(CSTRLIT)
100 {
101 }
102
103 Arg(Boolean x) : _boolean(x), _type(BOOLEAN)
104 {
105 }
106 kumpf 1.26
107 Arg(Sint32 x) : _integer(x), _type(INTEGER)
108 {
109 }
110
111 Arg(Uint32 x) : _uinteger(x), _type(UINTEGER)
112 {
113 }
114
115 Arg(Sint64 x) : _lInteger(x), _type(LINTEGER)
116 {
117 }
118
119 Arg(Uint64 x) : _lUInteger(x), _type(ULINTEGER)
120 {
121 }
122
123 Arg(Real64 x) : _real(x), _type(REAL)
124 {
125 }
126
127 kumpf 1.26 String toString() const;
128
129 void appendToString(String& out) const;
|
130 mike 1.11
|
131 kumpf 1.28 friend class MessageLoaderICU;
|
132 mike 1.11
133 private:
134
|
135 kumpf 1.26 String _string;
|
136 mike 1.11
|
137 kumpf 1.26 union
138 {
139 Sint32 _integer;
140 Uint32 _uinteger;
141 Real64 _real;
142 int _boolean;
143 Sint64 _lInteger;
144 Uint64 _lUInteger;
145 const StrLit* _cstrlit;
146 };
|
147 mike 1.11
|
148 kumpf 1.26 Type _type;
|
149 mike 1.11 };
|
150 karl 1.20
|
151 kumpf 1.26 /** Format function for the formatter
|
152 mike 1.11 */
153 static String format(
|
154 kumpf 1.26 const String& formatString,
155 const Arg& arg0 = Formatter::DEFAULT_ARG,
156 const Arg& arg1 = Formatter::DEFAULT_ARG,
157 const Arg& arg2 = Formatter::DEFAULT_ARG,
158 const Arg& arg3 = Formatter::DEFAULT_ARG,
159 const Arg& arg4 = Formatter::DEFAULT_ARG,
160 const Arg& arg5 = Formatter::DEFAULT_ARG,
161 const Arg& arg6 = Formatter::DEFAULT_ARG,
162 const Arg& arg7 = Formatter::DEFAULT_ARG,
163 const Arg& arg8 = Formatter::DEFAULT_ARG,
164 const Arg& arg9 = Formatter::DEFAULT_ARG);
|
165 karl 1.20
166 static const Formatter::Arg DEFAULT_ARG;
|
167 mike 1.11 };
168
169 PEGASUS_NAMESPACE_END
|
170 chuck 1.17
|
171 mike 1.11 #endif /* Pegasus_Formatter_h */
|