-
Notifications
You must be signed in to change notification settings - Fork 10
/
utilities.cpp
246 lines (215 loc) · 5.41 KB
/
utilities.cpp
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
#include "utilities.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
char easyToLowerCase(char in) {
if (in <= 'Z' && in >= 'A')
return in - ('Z' - 'z');
return in;
}
string toLowerCase(string s) {
transform(s.begin(), s.end(), s.begin(), easyToLowerCase);
return s;
}
bool comp::operator()(const string &lhs, const string &rhs) const {
return toLowerCase(lhs) < toLowerCase(rhs);
}
string readFile(const char *filename) {
string s = "";
char buffer[BUFFER_SIZE];
ifstream infile(filename);
infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(0, infile.beg);
if (length > sizeof(buffer))
length = sizeof(buffer);
infile.read(buffer, length);
for (size_t i = 0; i < length; i++) {
s += buffer[i];
}
return s;
}
string readFile(string filename) { return readFile(filename.c_str()); }
vector<string> split(string s, string delimiter, bool trim) {
vector<string> tokens;
if (trim)
s.erase(remove(s.begin(), s.end(), ' '), s.end());
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != string::npos) {
token = s.substr(0, pos);
tokens.push_back(token);
s.erase(0, pos + delimiter.length());
}
tokens.push_back(s);
return tokens;
}
void printVector(vector<string> v) {
for (string s : v)
cout << s << endl;
}
string urlEncode(string const &str) {
char encode_buf[4];
string result;
encode_buf[0] = '%';
result.reserve(str.size());
// character selection for this algorithm is based on the following url:
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
for (size_t pos = 0; pos < str.size(); ++pos) {
switch (str[pos]) {
default:
if (str[pos] >= 32 && str[pos] < 127) {
// character does not need to be escaped
result += str[pos];
break;
}
// else pass through to next case
case '$':
case '&':
case '+':
case ',':
case '/':
case ':':
case ';':
case '=':
case '?':
case '@':
case '"':
case '<':
case '>':
case '#':
case '%':
case '{':
case '}':
case '|':
case '\\':
case '^':
case '~':
case '[':
case ']':
case '`':
// the character needs to be encoded
sprintf(encode_buf + 1, "%02X", str[pos]);
result += encode_buf;
break;
}
};
return result;
}
string urlDecode(string const &str) {
char decode_buf[3];
string result;
result.reserve(str.size());
for (size_t pos = 0; pos < str.size(); ++pos) {
switch (str[pos]) {
case '+':
// convert to space character
result += ' ';
break;
case '%':
// decode hexidecimal value
if (pos + 2 < str.size()) {
decode_buf[0] = str[++pos];
decode_buf[1] = str[++pos];
decode_buf[2] = '\0';
result += static_cast<char>(strtol(decode_buf, nullptr, 16));
} else {
// recover from error by not decoding character
result += '%';
}
break;
default:
// character does not need to be escaped
result += str[pos];
}
}
return result;
}
string getExtension(string filePath) {
size_t pos = filePath.find_last_of(".");
return filePath.substr(pos != string::npos ? pos + 1 : filePath.size());
}
vector<string> tokenize(const string &cnt, char delimiter) {
vector<string> res;
istringstream is(cnt);
string part;
while (getline(is, part, delimiter))
res.push_back(part);
return res;
}
void replaceAll(std::string &str, const std::string &from,
const std::string &to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing
// 'x' with 'yx'
}
}
int findSubStrPosition(std::string &str, std::string const &subStr,
int const &pos) {
size_t found = str.find(subStr, pos);
if (found == string::npos)
return -1;
return found;
}
int writeObjectToFile(const char *object, int size,
std::string const &filePath) {
ofstream file;
file.open(filePath, fstream::out);
if (!file.is_open())
return -1;
file.write(object, size);
file.close();
return sizeof(object);
}
int writeToFile(std::string const &str, std::string const &filePath) {
return writeObjectToFile(str.c_str(), str.length(), filePath);
}
cimap getCimapFromString(std::string str) {
cimap m;
vector<string> tokenized = tokenize(str, '&');
for (auto token : tokenized) {
vector<string> keyValue = tokenize(token, '=');
if (keyValue.size() != 2)
continue;
string key = keyValue[0];
string value = keyValue[1];
m[key] = value;
}
return m;
}
int readMapFromFile(std::string fname, std::map<std::string, std::string> *m) {
int count = 0;
FILE *fp = fopen(fname.c_str(), "r");
if (!fp)
return -errno;
m->clear();
char *buf = 0;
size_t buflen = 0;
while (getline(&buf, &buflen, fp) > 0) {
char *nl = strchr(buf, '\n');
if (nl == NULL)
continue;
*nl = 0;
char *sep = strchr(buf, '=');
if (sep == NULL)
continue;
*sep = 0;
sep++;
std::string s1 = buf;
std::string s2 = sep;
(*m)[s1] = s2;
count++;
}
if (buf)
free(buf);
fclose(fp);
return count;
}