-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
99 lines (89 loc) · 2.1 KB
/
Copy pathutils.cpp
File metadata and controls
99 lines (89 loc) · 2.1 KB
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
#include "Webserv.hpp"
#include <sys/time.h>
double now() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1e6;
}
void removeSpacesAround(std::string &str) {
size_t start = str.find_first_not_of(" \t\r\n");
if (start == std::string::npos) {
str = "";
return ;
}
size_t end = str.find_last_not_of(" \t\r\n");
str = str.substr(start, end - start + 1);
}
bool isSpacesInside(std::string &str) {
if (str.find_first_of(" \t\r\n") != std::string::npos)
return true;
return false;
}
void addSpaces(std::string &line, char c) {
size_t pos = line.find(c);
std::string charC(1, c);
while (pos != std::string::npos)
{
line.replace(pos, 1, (" " + charC + " "));
pos += 3;
pos = line.find(c, pos);
}
}
std::list<std::string> tokenizer(std::string &buffer) {
std::list<std::string> tokens;
std::string token;
std::stringstream bufferStream(buffer);
while (bufferStream >> token)
tokens.push_back(token);
return tokens;
}
std::string lexer(std::string &file) {
std::string buffer;
std::string line;
std::ifstream filestream(file.c_str());
if (!filestream.is_open())
throw std::runtime_error("Could not open file: " + file);
while (getline(filestream, line))
{
size_t commentPos = line.find('#');
if (commentPos != line.std::string::npos)
line = line.substr(0, commentPos);
buffer += line + ' ';
}
addSpaces(buffer, '{');
addSpaces(buffer, '}');
addSpaces(buffer, ';');
return buffer;
}
void ipIsValid(std::string ip){
addSpaces(ip, '.');
std::stringstream ss(ip);
int i = 0;
std::string dot;
while (i++ < 4)
{
int bytes = -1;
ss >> bytes;
if (i < 4)
ss >> dot;
if (bytes < 0 || bytes > 255 || dot != ".")
{
std::cerr << bytes << std::endl;
throw std::runtime_error("Config: invalid IP address: " + ip);
}
}
if (ss.fail() || !ss.eof())
throw std::runtime_error("Config: failed to parse IP address: " + ip);
}
std::string sizetostr(int num) {
std::stringstream ss;
ss << num;
return ss.str();
}
long ft_strhexatoul(std::string str) {
std::stringstream ss;
unsigned long res;
ss << std::hex << str;
ss >> res;
return res;
}