1 | // |
---|
2 | // Format.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Format.cpp#7 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: Format |
---|
9 | // |
---|
10 | // Copyright (c) 2006, 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/Format.h" |
---|
38 | #include "Poco/Exception.h" |
---|
39 | #include <sstream> |
---|
40 | #include <cctype> |
---|
41 | #include <cstddef> |
---|
42 | |
---|
43 | |
---|
44 | namespace Poco { |
---|
45 | |
---|
46 | |
---|
47 | namespace |
---|
48 | { |
---|
49 | void parseFlags(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt) |
---|
50 | { |
---|
51 | bool isFlag = true; |
---|
52 | while (isFlag && itFmt != endFmt) |
---|
53 | { |
---|
54 | switch (*itFmt) |
---|
55 | { |
---|
56 | case '-': str.setf(std::ios::left); ++itFmt; break; |
---|
57 | case '+': str.setf(std::ios::showpos); ++itFmt; break; |
---|
58 | case '0': str.fill('0'); ++itFmt; break; |
---|
59 | case '#': str.setf(std::ios::showpoint | std::ios_base::showbase); ++itFmt; break; |
---|
60 | default: isFlag = false; break; |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | void parseWidth(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt) |
---|
67 | { |
---|
68 | int width = 0; |
---|
69 | while (itFmt != endFmt && std::isdigit(*itFmt)) |
---|
70 | { |
---|
71 | width = 10*width + *itFmt - '0'; |
---|
72 | ++itFmt; |
---|
73 | } |
---|
74 | if (width != 0) str.width(width); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | void parsePrec(std::ostream& str, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt) |
---|
79 | { |
---|
80 | if (itFmt != endFmt && *itFmt == '.') |
---|
81 | { |
---|
82 | ++itFmt; |
---|
83 | int prec = 0; |
---|
84 | while (itFmt != endFmt && std::isdigit(*itFmt)) |
---|
85 | { |
---|
86 | prec = 10*prec + *itFmt - '0'; |
---|
87 | ++itFmt; |
---|
88 | } |
---|
89 | if (prec != 0) str.precision(prec); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | char parseMod(std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt) |
---|
94 | { |
---|
95 | char mod = 0; |
---|
96 | if (itFmt != endFmt) |
---|
97 | { |
---|
98 | switch (*itFmt) |
---|
99 | { |
---|
100 | case 'l': |
---|
101 | case 'h': |
---|
102 | case 'L': |
---|
103 | case '?': mod = *itFmt++; break; |
---|
104 | } |
---|
105 | } |
---|
106 | return mod; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void prepareFormat(std::ostream& str, char type) |
---|
111 | { |
---|
112 | switch (type) |
---|
113 | { |
---|
114 | case 'd': |
---|
115 | case 'i': str << std::dec; break; |
---|
116 | case 'o': str << std::oct; break; |
---|
117 | case 'x': str << std::hex; break; |
---|
118 | case 'X': str << std::hex << std::uppercase; break; |
---|
119 | case 'e': str << std::scientific; break; |
---|
120 | case 'E': str << std::scientific << std::uppercase; break; |
---|
121 | case 'f': str << std::fixed; break; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | void writeAnyInt(std::ostream& str, const Any& any) |
---|
127 | { |
---|
128 | if (any.type() == typeid(char)) |
---|
129 | str << static_cast<int>(AnyCast<char>(any)); |
---|
130 | else if (any.type() == typeid(signed char)) |
---|
131 | str << static_cast<int>(AnyCast<signed char>(any)); |
---|
132 | else if (any.type() == typeid(unsigned char)) |
---|
133 | str << static_cast<unsigned>(AnyCast<unsigned char>(any)); |
---|
134 | else if (any.type() == typeid(short)) |
---|
135 | str << AnyCast<short>(any); |
---|
136 | else if (any.type() == typeid(unsigned short)) |
---|
137 | str << AnyCast<unsigned short>(any); |
---|
138 | else if (any.type() == typeid(int)) |
---|
139 | str << AnyCast<int>(any); |
---|
140 | else if (any.type() == typeid(unsigned int)) |
---|
141 | str << AnyCast<unsigned int>(any); |
---|
142 | else if (any.type() == typeid(long)) |
---|
143 | str << AnyCast<long>(any); |
---|
144 | else if (any.type() == typeid(unsigned long)) |
---|
145 | str << AnyCast<unsigned long>(any); |
---|
146 | else if (any.type() == typeid(Int64)) |
---|
147 | str << AnyCast<Int64>(any); |
---|
148 | else if (any.type() == typeid(UInt64)) |
---|
149 | str << AnyCast<UInt64>(any); |
---|
150 | else if (any.type() == typeid(bool)) |
---|
151 | str << AnyCast<bool>(any); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void formatOne(std::string& result, std::string::const_iterator& itFmt, const std::string::const_iterator& endFmt, std::vector<Any>::const_iterator& itVal) |
---|
156 | { |
---|
157 | std::ostringstream str; |
---|
158 | parseFlags(str, itFmt, endFmt); |
---|
159 | parseWidth(str, itFmt, endFmt); |
---|
160 | parsePrec(str, itFmt, endFmt); |
---|
161 | char mod = parseMod(itFmt, endFmt); |
---|
162 | if (itFmt != endFmt) |
---|
163 | { |
---|
164 | char type = *itFmt++; |
---|
165 | prepareFormat(str, type); |
---|
166 | switch (type) |
---|
167 | { |
---|
168 | case 'b': |
---|
169 | str << AnyCast<bool>(*itVal++); |
---|
170 | break; |
---|
171 | case 'c': |
---|
172 | str << AnyCast<char>(*itVal++); |
---|
173 | break; |
---|
174 | case 'd': |
---|
175 | case 'i': |
---|
176 | switch (mod) |
---|
177 | { |
---|
178 | case 'l': str << AnyCast<long>(*itVal++); break; |
---|
179 | case 'L': str << AnyCast<Int64>(*itVal++); break; |
---|
180 | case 'h': str << AnyCast<short>(*itVal++); break; |
---|
181 | case '?': writeAnyInt(str, *itVal++); break; |
---|
182 | default: str << AnyCast<int>(*itVal++); break; |
---|
183 | } |
---|
184 | break; |
---|
185 | case 'o': |
---|
186 | case 'u': |
---|
187 | case 'x': |
---|
188 | case 'X': |
---|
189 | switch (mod) |
---|
190 | { |
---|
191 | case 'l': str << AnyCast<unsigned long>(*itVal++); break; |
---|
192 | case 'L': str << AnyCast<UInt64>(*itVal++); break; |
---|
193 | case 'h': str << AnyCast<unsigned short>(*itVal++); break; |
---|
194 | case '?': writeAnyInt(str, *itVal++); break; |
---|
195 | default: str << AnyCast<unsigned>(*itVal++); break; |
---|
196 | } |
---|
197 | break; |
---|
198 | case 'e': |
---|
199 | case 'E': |
---|
200 | case 'f': |
---|
201 | switch (mod) |
---|
202 | { |
---|
203 | case 'l': str << AnyCast<long double>(*itVal++); break; |
---|
204 | case 'L': str << AnyCast<long double>(*itVal++); break; |
---|
205 | case 'h': str << AnyCast<float>(*itVal++); break; |
---|
206 | default: str << AnyCast<double>(*itVal++); break; |
---|
207 | } |
---|
208 | break; |
---|
209 | case 's': |
---|
210 | str << RefAnyCast<std::string>(*itVal++); |
---|
211 | break; |
---|
212 | case 'z': |
---|
213 | str << AnyCast<std::size_t>(*itVal++); |
---|
214 | break; |
---|
215 | case 'I': |
---|
216 | case 'D': |
---|
217 | default: |
---|
218 | str << type; |
---|
219 | } |
---|
220 | } |
---|
221 | result.append(str.str()); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | std::string format(const std::string& fmt, const Any& value) |
---|
227 | { |
---|
228 | std::string result; |
---|
229 | format(result, fmt, value); |
---|
230 | return result; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | std::string format(const std::string& fmt, const Any& value1, const Any& value2) |
---|
235 | { |
---|
236 | std::string result; |
---|
237 | format(result, fmt, value1, value2); |
---|
238 | return result; |
---|
239 | } |
---|
240 | |
---|
241 | |
---|
242 | std::string format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) |
---|
243 | { |
---|
244 | std::string result; |
---|
245 | format(result, fmt, value1, value2, value3); |
---|
246 | return result; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | std::string format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) |
---|
251 | { |
---|
252 | std::string result; |
---|
253 | format(result, fmt, value1, value2, value3, value4); |
---|
254 | return result; |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | std::string format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) |
---|
259 | { |
---|
260 | std::string result; |
---|
261 | format(result, fmt, value1, value2, value3, value4, value5); |
---|
262 | return result; |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | std::string format(const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) |
---|
267 | { |
---|
268 | std::string result; |
---|
269 | format(result, fmt, value1, value2, value3, value4, value5, value6); |
---|
270 | return result; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | void format(std::string& result, const std::string& fmt, const Any& value) |
---|
275 | { |
---|
276 | std::vector<Any> args; |
---|
277 | args.push_back(value); |
---|
278 | format(result, fmt, args); |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | void format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2) |
---|
283 | { |
---|
284 | std::vector<Any> args; |
---|
285 | args.push_back(value1); |
---|
286 | args.push_back(value2); |
---|
287 | format(result, fmt, args); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | void format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3) |
---|
292 | { |
---|
293 | std::vector<Any> args; |
---|
294 | args.push_back(value1); |
---|
295 | args.push_back(value2); |
---|
296 | args.push_back(value3); |
---|
297 | format(result, fmt, args); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | void format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4) |
---|
302 | { |
---|
303 | std::vector<Any> args; |
---|
304 | args.push_back(value1); |
---|
305 | args.push_back(value2); |
---|
306 | args.push_back(value3); |
---|
307 | args.push_back(value4); |
---|
308 | format(result, fmt, args); |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | void format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5) |
---|
313 | { |
---|
314 | std::vector<Any> args; |
---|
315 | args.push_back(value1); |
---|
316 | args.push_back(value2); |
---|
317 | args.push_back(value3); |
---|
318 | args.push_back(value4); |
---|
319 | args.push_back(value5); |
---|
320 | format(result, fmt, args); |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | void format(std::string& result, const std::string& fmt, const Any& value1, const Any& value2, const Any& value3, const Any& value4, const Any& value5, const Any& value6) |
---|
325 | { |
---|
326 | std::vector<Any> args; |
---|
327 | args.push_back(value1); |
---|
328 | args.push_back(value2); |
---|
329 | args.push_back(value3); |
---|
330 | args.push_back(value4); |
---|
331 | args.push_back(value5); |
---|
332 | args.push_back(value6); |
---|
333 | format(result, fmt, args); |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | void format(std::string& result, const std::string& fmt, const std::vector<Any>& values) |
---|
338 | { |
---|
339 | std::string::const_iterator itFmt = fmt.begin(); |
---|
340 | std::string::const_iterator endFmt = fmt.end(); |
---|
341 | std::vector<Any>::const_iterator itVal = values.begin(); |
---|
342 | std::vector<Any>::const_iterator endVal = values.end(); |
---|
343 | while (itFmt != endFmt) |
---|
344 | { |
---|
345 | switch (*itFmt) |
---|
346 | { |
---|
347 | case '%': |
---|
348 | ++itFmt; |
---|
349 | if (itFmt != endFmt && itVal != endVal) |
---|
350 | formatOne(result, itFmt, endFmt, itVal); |
---|
351 | else if (itFmt != endFmt) |
---|
352 | result += *itFmt++; |
---|
353 | break; |
---|
354 | default: |
---|
355 | result += *itFmt; |
---|
356 | ++itFmt; |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | } // namespace Poco |
---|