-
Notifications
You must be signed in to change notification settings - Fork 398
/
format.hpp
333 lines (287 loc) · 6.94 KB
/
format.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
#ifndef OPENVPN_COMMON_FORMAT_H
#define OPENVPN_COMMON_FORMAT_H
#include <cstddef> // for std::nullptr_t
#include <string>
#include <sstream>
#include <ostream>
#include <type_traits>
#include <utility>
#include <openvpn/common/platform.hpp>
#include <openvpn/common/to_string.hpp>
namespace openvpn {
// Concatenate arguments into a string:
// print(args...) -- concatenate
// prints(args...) -- concatenate but delimit args with space
// printd(char delim, args...) -- concatenate but delimit args with delim
namespace print_detail {
template <typename T>
inline void print(std::ostream &os, char delim, const T &first)
{
os << first;
}
template <typename T, typename... Args>
inline void print(std::ostream &os, char delim, const T &first, Args... args)
{
os << first;
if (delim)
os << delim;
print(os, delim, args...);
}
} // namespace print_detail
template <typename... Args>
inline std::string printd(char delim, Args... args)
{
std::ostringstream os;
print_detail::print(os, delim, args...);
return os.str();
}
template <typename... Args>
inline std::string print(Args... args)
{
return printd(0, args...);
}
template <typename... Args>
inline std::string prints(Args... args)
{
return printd(' ', args...);
}
// String formatting similar to sprintf.
// %s formats any argument regardless of type.
// %r formats any argument regardless of type and single-quotes it.
// %R formats any argument regardless of type and double-quotes it.
// %% formats '%'
// printfmt(<format_string>, args...)
namespace print_formatted_detail {
template <typename T>
class Output
{
};
template <>
class Output<std::string>
{
public:
Output(const size_t reserve)
{
if (reserve)
str_.reserve(reserve);
}
// numeric types
template <typename T,
typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
void append(T value)
{
str_ += openvpn::to_string(value);
}
// non-numeric types not specialized below
template <typename T,
typename std::enable_if<!std::is_arithmetic<T>::value, int>::type = 0>
void append(const T &value)
{
std::ostringstream os;
os << value;
str_ += os.str();
}
// specialization for std::string
void append(const std::string &value)
{
str_ += value;
}
// specialization for const char *
void append(const char *value)
{
if (value)
str_ += value;
}
// specialization for char *
void append(char *value)
{
if (value)
str_ += value;
}
// specialization for char
void append(const char c)
{
str_ += c;
}
// specialization for bool
void append(const bool value)
{
str_ += value ? "true" : "false";
}
// specialization for nullptr
void append(std::nullptr_t)
{
str_ += "nullptr";
}
std::string str()
{
return std::move(str_);
}
private:
std::string str_;
};
template <>
class Output<std::ostringstream>
{
public:
Output(const size_t reserve)
{
// fixme -- figure out how to reserve space in std::ostringstream
}
// general types
template <typename T>
void append(const T &value)
{
os_ << value;
}
// specialization for const char *
void append(const char *value)
{
if (value)
os_ << value;
}
// specialization for char *
void append(char *value)
{
if (value)
os_ << value;
}
// specialization for bool
void append(const bool value)
{
if (value)
os_ << "true";
else
os_ << "false";
}
// specialization for nullptr
void append(std::nullptr_t)
{
os_ << "nullptr";
}
std::string str()
{
return os_.str();
}
private:
std::ostringstream os_;
};
} // namespace print_formatted_detail
template <typename OUTPUT>
class PrintFormatted
{
public:
PrintFormatted(const std::string &fmt_arg, const size_t reserve)
: fmt(fmt_arg),
fi(fmt.begin()),
out(reserve),
pct(false)
{
}
void process()
{
process_finish();
}
template <typename T>
void process(const T &last)
{
process_arg(last);
process_finish();
}
template <typename T, typename... Args>
void process(const T &first, Args... args)
{
process_arg(first);
process(args...);
}
std::string str()
{
return out.str();
}
private:
PrintFormatted(const PrintFormatted &) = delete;
PrintFormatted &operator=(const PrintFormatted &) = delete;
template <typename T>
bool process_arg(const T &arg)
{
while (fi != fmt.end())
{
const char c = *fi++;
if (pct)
{
pct = false;
const int quote = quote_delim(c);
if (quote >= 0)
{
if (quote)
out.append((char)quote);
out.append(arg);
if (quote)
out.append((char)quote);
return true;
}
else
out.append(c);
}
else
{
if (c == '%')
pct = true;
else
out.append(c);
}
}
return false;
}
void process_finish()
{
// '?' printed for %s operators that don't match an argument
while (process_arg("?"))
;
}
static int quote_delim(const char fmt)
{
switch (fmt)
{
case 's':
return 0;
case 'r':
return '\'';
case 'R':
return '\"';
default:
return -1;
}
}
const std::string &fmt;
std::string::const_iterator fi;
print_formatted_detail::Output<OUTPUT> out;
bool pct;
};
template <typename... Args>
inline std::string printfmt(const std::string &fmt, Args... args)
{
#ifdef OPENVPN_PLATFORM_ANDROID
PrintFormatted<std::ostringstream> pf(fmt, 256);
#else
PrintFormatted<std::string> pf(fmt, 256);
#endif
pf.process(args...);
return pf.str();
}
// log with formatting
#define OPENVPN_FMT(...) OPENVPN_LOG_STRING(printfmt(__VA_ARGS__))
// throw a formatted exception
#define OPENVPN_THROW_FMT(EXC, ...) throw EXC(printfmt(__VA_ARGS__))
} // namespace openvpn
#endif