Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
convert.h
Go to the documentation of this file.
1
8#ifndef convert_h_included
9#define convert_h_included
10#include <cstring>
11#include <string>
12#include <vector>
13#include <iomanip>
14#include <iostream>
15
16#include <stdio.h>
17#include <sys/stat.h>
18
19#include "address.h"
20#include "depot.h"
21
22namespace Binc {
23
24 //----------------------------------------------------------------------
25 inline std::string toString(int i_in)
26 {
27 char intbuf[16];
28 snprintf(intbuf, sizeof(intbuf), "%d", i_in);
29 return std::string(intbuf);
30 }
31
32 //----------------------------------------------------------------------
33 inline std::string toString(unsigned int i_in)
34 {
35 char intbuf[16];
36 snprintf(intbuf, sizeof(intbuf), "%u", i_in);
37 return std::string(intbuf);
38 }
39
40 //----------------------------------------------------------------------
41 inline std::string toString(unsigned long i_in)
42 {
43 char longbuf[40];
44 snprintf(longbuf, sizeof(longbuf), "%lu", i_in);
45 return std::string(longbuf);
46 }
47
48 //----------------------------------------------------------------------
49 inline std::string toString(const char *i_in)
50 {
51 return std::string(i_in);
52 }
53
54 //----------------------------------------------------------------------
55 inline int atoi(const std::string &s_in)
56 {
57 return ::atoi(s_in.c_str());
58 }
59
60 //----------------------------------------------------------------------
61 inline std::string toHex(const std::string &s)
62 {
63 const char hexchars[] = "0123456789abcdef";
64 std::string tmp;
65 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
66 unsigned char c = (unsigned char)*i;
67 tmp += hexchars[((c & 0xf0) >> 4)];
68 tmp += hexchars[c & 0x0f];
69 }
70
71 return tmp;
72 }
73
74 //----------------------------------------------------------------------
75 inline std::string fromHex(const std::string &s)
76 {
77 // const
78 char hexchars[] = "0123456789abcdef";
79 std::string tmp;
80 for (std::string::const_iterator i = s.begin();
81 i != s.end() && i + 1 != s.end(); i += 2) {
82 int n;
83 unsigned char c = *i;
84 unsigned char d = *(i + 1);
85
86 char *t;
87 if ((t = strchr(hexchars, c)) == 0) return "out of range";
88 n = (t - hexchars) << 4;
89
90 if ((t = strchr(hexchars, d)) == 0) return "out of range";
91 n += (t - hexchars);
92
93 if (n >= 0 && n <= 255)
94 tmp += (char) n;
95 else
96 return "out of range";
97 }
98
99 return tmp;
100 }
101
102 //----------------------------------------------------------------------
103 inline std::string toImapString(const std::string &s_in)
104 {
105 for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
106 unsigned char c = (unsigned char)*i;
107 if (c <= 31 || c >= 127 || c == '\"' || c == '\\')
108 return "{" + toString(s_in.length()) + "}\r\n" + s_in;
109 }
110
111 return "\"" + s_in + "\"";
112 }
113
114 //----------------------------------------------------------------------
115 inline void uppercase(std::string &input)
116 {
117 for (std::string::iterator i = input.begin(); i != input.end(); ++i)
118 *i = toupper(*i);
119 }
120
121 //----------------------------------------------------------------------
122 inline void lowercase(std::string &input)
123 {
124 for (std::string::iterator i = input.begin(); i != input.end(); ++i)
125 *i = tolower(*i);
126 }
127
128 //----------------------------------------------------------------------
129 inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n")
130 {
131 int n = s_in.length();
132 while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos)
133 s_in.resize(n-- - 1);
134 }
135
136 //----------------------------------------------------------------------
137 inline void trim(std::string &s_in, const std::string &chars = " \t\r\n")
138 {
139 while (s_in != "" && chars.find(s_in[0]) != std::string::npos)
140 s_in = s_in.substr(1);
141 chomp(s_in, chars);
142 }
143
144 //----------------------------------------------------------------------
145 inline const std::string unfold(const std::string &a,
146 bool removecomment = true)
147 {
148 std::string tmp;
149 bool incomment = false;
150 bool inquotes = false;
151 for (std::string::const_iterator i = a.begin(); i != a.end(); ++i) {
152 unsigned char c = (unsigned char)*i;
153 if (!inquotes && removecomment) {
154 if (c == '(') {
155 incomment = true;
156 tmp += " ";
157 } else if (c == ')') {
158 incomment = false;
159 } else if (c != 0x0a && c != 0x0d) {
160 tmp += *i;
161 }
162 } else if (c != 0x0a && c != 0x0d) {
163 tmp += *i;
164 }
165
166 if (!incomment) {
167 if (*i == '\"')
168 inquotes = !inquotes;
169 }
170 }
171
172 trim(tmp);
173 return tmp;
174 }
175
176 //----------------------------------------------------------------------
177 inline void split(const std::string &s_in, const std::string &delim,
178 std::vector<std::string> &dest, bool skipempty = true)
179 {
180 std::string token;
181 for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
182 if (delim.find(*i) != std::string::npos) {
183 if (!skipempty || token != "")
184 dest.push_back(token);
185 token = "";
186 } else
187 token += *i;
188 }
189
190 if (token != "")
191 dest.push_back(token);
192 }
193
194 //----------------------------------------------------------------------
195 inline void splitAddr(const std::string &s_in,
196 std::vector<std::string> &dest, bool skipempty = true)
197 {
198 static const std::string delim = ",";
199 std::string token;
200 bool inquote = false;
201 for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
202 if (inquote && *i == '\"') inquote = false;
203 else if (!inquote && *i == '\"') inquote = true;
204
205 if (!inquote && delim.find(*i) != std::string::npos) {
206 if (!skipempty || token != "") dest.push_back(token);
207 token = "";
208 } else
209 token += *i;
210 }
211 if (token != "")
212 dest.push_back(token);
213 }
214
215 //----------------------------------------------------------------------
216 inline std::string toCanonMailbox(const std::string &s_in)
217 {
218 if (s_in.find("..") != std::string::npos) return "";
219
220 if (s_in.length() >= 5) {
221 std::string a = s_in.substr(0, 5);
222 uppercase(a);
223 return a == "INBOX" ?
224 a + (s_in.length() > 5 ? s_in.substr(5) : "") : s_in;
225 }
226
227 return s_in;
228 }
229
230 //------------------------------------------------------------------------
231 inline std::string toRegex(const std::string &s_in, char delimiter)
232 {
233 std::string regex = "^";
234 for (std::string::const_iterator i = s_in.begin(); i != s_in.end(); ++i) {
235 if (*i == '.' || *i == '[' || *i == ']' || *i == '{' || *i == '}' ||
236 *i == '(' || *i == ')' || *i == '^' || *i == '$' || *i == '?' ||
237 *i == '+' || *i == '\\') {
238 regex += "\\";
239 regex += *i;
240 } else if (*i == '*')
241 regex += ".*?";
242 else if (*i == '%') {
243 regex += "(\\";
244 regex += delimiter;
245 regex += "){0,1}";
246 regex += "[^\\";
247 regex += delimiter;
248 regex += "]*?";
249 } else
250 regex += *i;
251 }
252
253 if (regex[regex.length() - 1] == '?')
254 regex[regex.length() - 1] = '$';
255 else
256 regex += "$";
257
258 return regex;
259 }
260
261 //------------------------------------------------------------------------
263 private:
264 std::string nstr;
265
266 public:
267 //--
268 BincStream &operator << (std::ostream&(*)(std::ostream&));
269 BincStream &operator << (const std::string &t);
270 BincStream &operator << (unsigned long t);
271 BincStream &operator << (unsigned int t);
272 BincStream &operator << (int t);
273 BincStream &operator << (char t);
274
275 //--
276 std::string popString(unsigned int size);
277
278 //--
279 char popChar(void);
280 void unpopChar(char c);
281 void unpopStr(const std::string &s);
282
283 //--
284 const std::string &str(void) const;
285
286 //--
287 unsigned int getSize(void) const;
288
289 //--
290 void clear(void);
291
292 //--
293 BincStream(void);
294 ~BincStream(void);
295 };
296}
297
298#endif
Declaration of the Address class.
void unpopStr(const std::string &s)
Definition: convert.cc:52
const std::string & str(void) const
Definition: convert.cc:58
BincStream(void)
Definition: convert.cc:14
std::string popString(unsigned int size)
Definition: convert.cc:25
char popChar(void)
Definition: convert.cc:35
unsigned int getSize(void) const
Definition: convert.cc:70
void unpopChar(char c)
Definition: convert.cc:46
BincStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: convert.cc:76
~BincStream(void)
Definition: convert.cc:19
void clear(void)
Definition: convert.cc:64
Definition: bincimapd.cc:9
std::string toImapString(const std::string &s_in)
Definition: convert.h:103
std::string toString(int i_in)
Definition: convert.h:25
void splitAddr(const std::string &s_in, std::vector< std::string > &dest, bool skipempty=true)
Definition: convert.h:195
std::string toCanonMailbox(const std::string &s_in)
Definition: convert.h:216
std::string fromHex(const std::string &s)
Definition: convert.h:75
std::string toRegex(const std::string &s_in, char delimiter)
Definition: convert.h:231
const std::string unfold(const std::string &a, bool removecomment=true)
Definition: convert.h:145
void chomp(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:129
int atoi(const std::string &s_in)
Definition: convert.h:55
void split(const std::string &s_in, const std::string &delim, std::vector< std::string > &dest, bool skipempty=true)
Definition: convert.h:177
void uppercase(std::string &input)
Definition: convert.h:115
void lowercase(std::string &input)
Definition: convert.h:122
void trim(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:137
std::string toHex(const std::string &s)
Definition: convert.h:61