-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
70 lines (56 loc) · 1.32 KB
/
utils.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
#include "utils.h"
#include <vector>
#include <string>
void log(const std::string &message)
{
std::cout << message << std::endl;
}
void erroredExit(const std::string &errMessage)
{
log("ERROR: " + errMessage);
exit(1);
}
std::vector<std::string> splitString(std::string delimiter, std::string s)
{
std::vector<std::string> res;
size_t pos = 0;
while ((pos = s.find(delimiter)) != std::string::npos)
{
std::string token = s.substr(0, pos);
res.push_back(token);
s.erase(0, pos + delimiter.length());
}
if (s.size() != 0)
{
s.erase(s.size() - 1);
res.push_back(s);
}
return res;
}
std::vector<std::string> getReqTokens(std::string str)
{
std::vector<std::string> lines = splitString("\n", str);
std::vector<std::string> tokens;
for (std::string s : lines)
{
std::vector<std::string> split = splitString(" ", s);
tokens.insert(tokens.end(), split.begin(), split.end());
}
return tokens;
}
std::string getReqBody(std::vector<std::string> tokens)
{
int i = 0;
for (int j = 0; j < tokens.size(); j++)
{
if (tokens[j] == "")
{
if (j + 1 < tokens.size())
i = j + 1;
break;
}
}
if (i == 0)
return "";
return tokens[i];
}