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
|
/**
* @file convert.h
* @brief Declaration of miscellaneous convertion functions.
* @author Andreas Aardal Hanssen
* @date 2002-2005
*/
#ifndef convert_h_included
#define convert_h_included
#include "address.h"
#include "depot.h"
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <stdio.h>
#include <sys/stat.h>
namespace Binc {
inline std::string toString(int i_in)
{
char intbuf[16];
snprintf(intbuf, sizeof(intbuf), "%d", i_in);
return std::string(intbuf);
}
inline std::string toString(unsigned int i_in)
{
char intbuf[16];
snprintf(intbuf, sizeof(intbuf), "%u", i_in);
return std::string(intbuf);
}
inline std::string toString(unsigned long i_in)
{
char longbuf[40];
snprintf(longbuf, sizeof(longbuf), "%lu", i_in);
return std::string(longbuf);
}
inline std::string toString(const char *i_in)
{
return std::string(i_in);
}
inline int atoi(const std::string &s_in)
{
return ::atoi(s_in.c_str());
}
inline std::string toHex(const std::string &s)
{
const char hexchars[] = "0123456789abcdef";
std::string tmp;
for (auto i : s) {
unsigned char c = static_cast<unsigned char>(i);
tmp += hexchars[((c & 0xf0) >> 4)];
tmp += hexchars[c & 0x0f];
}
return tmp;
}
inline std::string fromHex(const std::string &s)
{
// const
char hexchars[] = "0123456789abcdef";
std::string tmp;
for (auto i = s.cbegin(); i != s.cend() && i + 1 != s.cend(); i += 2) {
int n;
unsigned char c = *i;
unsigned char d = *(i + 1);
char *t;
if ((t = strchr(hexchars, c)) == nullptr) return "out of range";
n = (t - hexchars) << 4;
if ((t = strchr(hexchars, d)) == nullptr) return "out of range";
n += (t - hexchars);
if (n >= 0 && n <= 255)
tmp += (char)n;
else
return "out of range";
}
return tmp;
}
inline std::string toImapString(const std::string &s_in)
{
for (const auto i : s_in) {
auto c = static_cast<unsigned char>(i);
if (c <= 31 || c >= 127 || c == '\"' || c == '\\')
return "{" + toString(s_in.length()) + "}\r\n" + s_in;
}
return "\"" + s_in + "\"";
}
inline void uppercase(std::string &input)
{
for (auto &i : input)
i = toupper(i);
}
inline void lowercase(std::string &input)
{
for (char &i : input)
i = tolower(i);
}
inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n")
{
int n = s_in.length();
while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos)
s_in.resize(n-- - 1);
}
inline void trim(std::string &s_in, const std::string &chars = " \t\r\n")
{
while (s_in != "" && chars.find(s_in[0]) != std::string::npos)
s_in = s_in.substr(1);
chomp(s_in, chars);
}
inline void trim(std::string_view &s_in, std::string_view chars = " \t\r\n")
{
size_t n = 0;
while (n < s_in.length() && chars.find(s_in[n]) != std::string_view::npos) {
n++;
}
s_in = s_in.substr(n);
if (s_in.empty()) return;
n = s_in.length();
while (n > 0 && chars.find(s_in[--n]) != std::string_view::npos) {
}
s_in = s_in.substr(0, n + 1);
}
inline const std::string unfold(const std::string &a, bool removecomment = true)
{
std::string tmp;
bool incomment = false;
bool inquotes = false;
for (const char i : a) {
unsigned char c = (unsigned char)i;
if (!inquotes && removecomment) {
if (c == '(') {
incomment = true;
tmp += " ";
} else if (c == ')') {
incomment = false;
} else if (c != 0x0a && c != 0x0d) {
tmp += i;
}
} else if (c != 0x0a && c != 0x0d) {
tmp += i;
}
if (!incomment) {
if (i == '\"') inquotes = !inquotes;
}
}
trim(tmp);
return tmp;
}
inline void split(const std::string &s_in,
const std::string &delim,
std::vector<std::string> &dest,
bool skipempty = true)
{
std::string token;
for (const char i : s_in) {
if (delim.find(i) != std::string::npos) {
if (!skipempty || token != "") dest.push_back(token);
token = "";
} else {
token += i;
}
}
if (token != "") dest.push_back(token);
}
inline void splitAddr(const std::string &s_in,
std::vector<std::string> &dest,
bool skipempty = true)
{
static const std::string delim = ",";
std::string token;
bool inquote = false;
for (const char i : s_in) {
if (inquote && i == '\"')
inquote = false;
else if (!inquote && i == '\"')
inquote = true;
if (!inquote && delim.find(i) != std::string::npos) {
if (!skipempty || token != "") dest.push_back(token);
token = "";
} else {
token += i;
}
}
if (token != "") dest.push_back(token);
}
inline std::string toCanonMailbox(const std::string &s_in)
{
if (s_in.find("..") != std::string::npos) return "";
if (s_in.length() >= 5) {
std::string a = s_in.substr(0, 5);
uppercase(a);
return a == "INBOX" ? a + (s_in.length() > 5 ? s_in.substr(5) : "") : s_in;
}
return s_in;
}
inline std::string toRegex(const std::string &s_in, char delimiter)
{
std::string regex = "^";
for (const char i : s_in) {
if (i == '.' || i == '[' || i == ']' || i == '{' || i == '}' || i == '(' || i == ')'
|| i == '^' || i == '$' || i == '?' || i == '+' || i == '\\')
{
regex += "\\";
regex += i;
} else if (i == '*') {
regex += ".*?";
} else if (i == '%') {
regex += "(\\";
regex += delimiter;
regex += "){0,1}";
regex += "[^\\";
regex += delimiter;
regex += "]*?";
} else {
regex += i;
}
}
if (regex[regex.length() - 1] == '?')
regex[regex.length() - 1] = '$';
else
regex += "$";
return regex;
}
class BincStream {
private:
std::string nstr;
public:
BincStream &operator<<(std::ostream &(*)(std::ostream &));
BincStream &operator<<(const std::string &t);
BincStream &operator<<(unsigned long t);
BincStream &operator<<(unsigned int t);
BincStream &operator<<(int t);
BincStream &operator<<(char t);
std::string popString(unsigned int size);
char popChar();
void unpopChar(char c);
void unpopStr(const std::string &s);
const std::string &str() const;
unsigned int getSize() const;
void clear();
BincStream();
~BincStream();
};
}
#endif
|