|
a |
|
b/src/bincimapmime/convert.h |
|
|
1 |
/* -*- Mode: c++; -*- */
|
|
|
2 |
/* --------------------------------------------------------------------
|
|
|
3 |
* Filename:
|
|
|
4 |
* src/util/convert.h
|
|
|
5 |
*
|
|
|
6 |
* Description:
|
|
|
7 |
* Declaration of miscellaneous convertion functions.
|
|
|
8 |
* --------------------------------------------------------------------
|
|
|
9 |
* Copyright 2002-2004 Andreas Aardal Hanssen
|
|
|
10 |
*
|
|
|
11 |
* This program is free software; you can redistribute it and/or modify
|
|
|
12 |
* it under the terms of the GNU General Public License as published by
|
|
|
13 |
* the Free Software Foundation; either version 2 of the License, or
|
|
|
14 |
* (at your option) any later version.
|
|
|
15 |
*
|
|
|
16 |
* This program is distributed in the hope that it will be useful,
|
|
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
19 |
* GNU General Public License for more details.
|
|
|
20 |
*
|
|
|
21 |
* You should have received a copy of the GNU General Public License
|
|
|
22 |
* along with this program; if not, write to the Free Software
|
|
|
23 |
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
|
|
|
24 |
* --------------------------------------------------------------------
|
|
|
25 |
*/
|
|
|
26 |
#ifdef HAVE_CONFIG_H
|
|
|
27 |
#include <config.h>
|
|
|
28 |
#endif
|
|
|
29 |
|
|
|
30 |
#ifndef convert_h_included
|
|
|
31 |
#define convert_h_included
|
|
|
32 |
#include <string>
|
|
|
33 |
#include <vector>
|
|
|
34 |
#include <iomanip>
|
|
|
35 |
#include <iostream>
|
|
|
36 |
|
|
|
37 |
#include <stdio.h>
|
|
|
38 |
#include <sys/stat.h>
|
|
|
39 |
|
|
|
40 |
#include "address.h"
|
|
|
41 |
#include "depot.h"
|
|
|
42 |
|
|
|
43 |
namespace Binc {
|
|
|
44 |
|
|
|
45 |
//----------------------------------------------------------------------
|
|
|
46 |
inline std::string toString(int i_in)
|
|
|
47 |
{
|
|
|
48 |
char intbuf[16];
|
|
|
49 |
snprintf(intbuf, sizeof(intbuf), "%d", i_in);
|
|
|
50 |
return std::string(intbuf);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
//----------------------------------------------------------------------
|
|
|
54 |
inline std::string toString(unsigned int i_in)
|
|
|
55 |
{
|
|
|
56 |
char intbuf[16];
|
|
|
57 |
snprintf(intbuf, sizeof(intbuf), "%u", i_in);
|
|
|
58 |
return std::string(intbuf);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
//----------------------------------------------------------------------
|
|
|
62 |
inline std::string toString(unsigned long i_in)
|
|
|
63 |
{
|
|
|
64 |
char longbuf[40];
|
|
|
65 |
snprintf(longbuf, sizeof(longbuf), "%lu", i_in);
|
|
|
66 |
return std::string(longbuf);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
//----------------------------------------------------------------------
|
|
|
70 |
inline std::string toString(const char *i_in)
|
|
|
71 |
{
|
|
|
72 |
return std::string(i_in);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
//----------------------------------------------------------------------
|
|
|
76 |
inline int atoi(const std::string &s_in)
|
|
|
77 |
{
|
|
|
78 |
return ::atoi(s_in.c_str());
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
//----------------------------------------------------------------------
|
|
|
82 |
inline std::string toHex(const std::string &s)
|
|
|
83 |
{
|
|
|
84 |
const char hexchars[] = "0123456789abcdef";
|
|
|
85 |
std::string tmp;
|
|
|
86 |
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
|
|
|
87 |
unsigned char c = (unsigned char)*i;
|
|
|
88 |
tmp += hexchars[((c & 0xf0) >> 4)];
|
|
|
89 |
tmp += hexchars[c & 0x0f];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return tmp;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
//----------------------------------------------------------------------
|
|
|
96 |
inline std::string fromHex(const std::string &s)
|
|
|
97 |
{
|
|
|
98 |
const char hexchars[] = "0123456789abcdef";
|
|
|
99 |
std::string tmp;
|
|
|
100 |
for (std::string::const_iterator i = s.begin();
|
|
|
101 |
i != s.end() && i + 1 != s.end(); i += 2) {
|
|
|
102 |
int n;
|
|
|
103 |
unsigned char c = *i;
|
|
|
104 |
unsigned char d = *(i + 1);
|
|
|
105 |
|
|
|
106 |
char *t;
|
|
|
107 |
if ((t = strchr(hexchars, c)) == 0)
|
|
|
108 |
return "out of range";
|
|
|
109 |
n = (t - hexchars) << 4;
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
if ((t = strchr(hexchars, d)) == 0)
|
|
|
113 |
return "out of range";
|
|
|
114 |
n += (t - hexchars);
|
|
|
115 |
|
|
|
116 |
if (n >= 0 && n <= 255)
|
|
|
117 |
tmp += (char) n;
|
|
|
118 |
else
|
|
|
119 |
return "out of range";
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return tmp;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
//----------------------------------------------------------------------
|
|
|
126 |
inline std::string toImapString(const std::string &s_in)
|
|
|
127 |
{
|
|
|
128 |
for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
|
|
|
129 |
unsigned char c = (unsigned char)*i;
|
|
|
130 |
if (c <= 31 || c >= 127 || c == '\"' || c == '\\')
|
|
|
131 |
return "{" + toString(s_in.length()) + "}\r\n" + s_in;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return "\"" + s_in + "\"";
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
//----------------------------------------------------------------------
|
|
|
138 |
inline void uppercase(std::string &input)
|
|
|
139 |
{
|
|
|
140 |
for (std::string::iterator i = input.begin(); i != input.end(); ++i)
|
|
|
141 |
*i = toupper(*i);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
//----------------------------------------------------------------------
|
|
|
145 |
inline void lowercase(std::string &input)
|
|
|
146 |
{
|
|
|
147 |
for (std::string::iterator i = input.begin(); i != input.end(); ++i)
|
|
|
148 |
*i = tolower(*i);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
//----------------------------------------------------------------------
|
|
|
152 |
inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n")
|
|
|
153 |
{
|
|
|
154 |
int n = s_in.length();
|
|
|
155 |
while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos)
|
|
|
156 |
s_in.resize(n-- - 1);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
//----------------------------------------------------------------------
|
|
|
160 |
inline void trim(std::string &s_in, const std::string &chars = " \t\r\n")
|
|
|
161 |
{
|
|
|
162 |
while (s_in != "" && chars.find(s_in[0]) != std::string::npos)
|
|
|
163 |
s_in = s_in.substr(1);
|
|
|
164 |
chomp(s_in, chars);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
//----------------------------------------------------------------------
|
|
|
168 |
inline const std::string unfold(const std::string &a,
|
|
|
169 |
bool removecomment = true)
|
|
|
170 |
{
|
|
|
171 |
std::string tmp;
|
|
|
172 |
bool incomment = false;
|
|
|
173 |
bool inquotes = false;
|
|
|
174 |
for (std::string::const_iterator i = a.begin(); i != a.end(); ++i) {
|
|
|
175 |
unsigned char c = (unsigned char)*i;
|
|
|
176 |
if (!inquotes && removecomment) {
|
|
|
177 |
if (c == '(') {
|
|
|
178 |
incomment = true;
|
|
|
179 |
tmp += " ";
|
|
|
180 |
} else if (c == ')') {
|
|
|
181 |
incomment = false;
|
|
|
182 |
} else if (c != 0x0a && c != 0x0d) {
|
|
|
183 |
tmp += *i;
|
|
|
184 |
}
|
|
|
185 |
} else if (c != 0x0a && c != 0x0d) {
|
|
|
186 |
tmp += *i;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
if (!incomment) {
|
|
|
190 |
if (*i == '\"')
|
|
|
191 |
inquotes = !inquotes;
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
trim(tmp);
|
|
|
196 |
return tmp;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
//----------------------------------------------------------------------
|
|
|
200 |
inline void split(const std::string &s_in, const std::string &delim,
|
|
|
201 |
std::vector<std::string> &dest, bool skipempty = true)
|
|
|
202 |
{
|
|
|
203 |
std::string token;
|
|
|
204 |
for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
|
|
|
205 |
if (delim.find(*i) != std::string::npos) {
|
|
|
206 |
if (!skipempty || token != "")
|
|
|
207 |
dest.push_back(token);
|
|
|
208 |
token = "";
|
|
|
209 |
} else
|
|
|
210 |
token += *i;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
if (token != "")
|
|
|
214 |
dest.push_back(token);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
//----------------------------------------------------------------------
|
|
|
218 |
inline void splitAddr(const std::string &s_in,
|
|
|
219 |
std::vector<std::string> &dest, bool skipempty = true)
|
|
|
220 |
{
|
|
|
221 |
static const std::string delim = ",";
|
|
|
222 |
std::string token;
|
|
|
223 |
bool inquote = false;
|
|
|
224 |
for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
|
|
|
225 |
if (inquote && *i == '\"') inquote = false;
|
|
|
226 |
else if (!inquote && *i == '\"') inquote = true;
|
|
|
227 |
|
|
|
228 |
if (!inquote && delim.find(*i) != std::string::npos) {
|
|
|
229 |
if (!skipempty || token != "")
|
|
|
230 |
dest.push_back(token);
|
|
|
231 |
token = "";
|
|
|
232 |
} else
|
|
|
233 |
token += *i;
|
|
|
234 |
}
|
|
|
235 |
if (token != "")
|
|
|
236 |
dest.push_back(token);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
//----------------------------------------------------------------------
|
|
|
240 |
inline std::string toCanonMailbox(const std::string &s_in)
|
|
|
241 |
{
|
|
|
242 |
if (s_in.find("..") != std::string::npos) return "";
|
|
|
243 |
|
|
|
244 |
if (s_in.length() >= 5) {
|
|
|
245 |
std::string a = s_in.substr(0, 5);
|
|
|
246 |
uppercase(a);
|
|
|
247 |
return a == "INBOX" ?
|
|
|
248 |
a + (s_in.length() > 5 ? s_in.substr(5) : "") : s_in;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
return s_in;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
//------------------------------------------------------------------------
|
|
|
255 |
inline std::string toRegex(const std::string &s_in, char delimiter)
|
|
|
256 |
{
|
|
|
257 |
std::string regex = "^";
|
|
|
258 |
for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
|
|
|
259 |
if (*i == '.' || *i == '[' || *i == ']' || *i == '{' || *i == '}' ||
|
|
|
260 |
*i == '(' || *i == ')' || *i == '^' || *i == '$' || *i == '?' ||
|
|
|
261 |
*i == '+' || *i == '\\') {
|
|
|
262 |
regex += "\\";
|
|
|
263 |
regex += *i;
|
|
|
264 |
} else if (*i == '*')
|
|
|
265 |
regex += ".*?";
|
|
|
266 |
else if (*i == '%') {
|
|
|
267 |
regex += "[^\\";
|
|
|
268 |
regex += delimiter;
|
|
|
269 |
regex += "]*?";
|
|
|
270 |
} else regex += *i;
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if (regex[regex.length() - 1] == '?')
|
|
|
274 |
regex[regex.length() - 1] = '$';
|
|
|
275 |
else
|
|
|
276 |
regex += "$";
|
|
|
277 |
|
|
|
278 |
return regex;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
//------------------------------------------------------------------------
|
|
|
282 |
class BincStream {
|
|
|
283 |
private:
|
|
|
284 |
std::string nstr;
|
|
|
285 |
|
|
|
286 |
public:
|
|
|
287 |
|
|
|
288 |
//--
|
|
|
289 |
BincStream &operator << (std::ostream&(*)(std::ostream&));
|
|
|
290 |
BincStream &operator << (const std::string &t);
|
|
|
291 |
BincStream &operator << (unsigned int t);
|
|
|
292 |
BincStream &operator << (int t);
|
|
|
293 |
BincStream &operator << (char t);
|
|
|
294 |
|
|
|
295 |
//--
|
|
|
296 |
std::string popString(unsigned int size);
|
|
|
297 |
|
|
|
298 |
//--
|
|
|
299 |
char popChar(void);
|
|
|
300 |
void unpopChar(char c);
|
|
|
301 |
void unpopStr(const std::string &s);
|
|
|
302 |
|
|
|
303 |
//--
|
|
|
304 |
const std::string &str(void) const;
|
|
|
305 |
|
|
|
306 |
//--
|
|
|
307 |
unsigned int getSize(void) const;
|
|
|
308 |
|
|
|
309 |
//--
|
|
|
310 |
void clear(void);
|
|
|
311 |
|
|
|
312 |
//--
|
|
|
313 |
BincStream(void);
|
|
|
314 |
~BincStream(void);
|
|
|
315 |
};
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
#endif
|