-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
203 lines (165 loc) · 3.92 KB
/
utils.cpp
File metadata and controls
203 lines (165 loc) · 3.92 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
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
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include "webserv.hpp"
using std::string; using std::vector;
using std::ifstream; using std::getline; using std::rand;
using std::isspace;
bool is_directory(const string &route) {
struct stat sb;
if (stat(route.c_str(), &sb) != 0) {
return false;
}
bool ret = (sb.st_mode & S_IFMT) == S_IFDIR;
return ret;
}
bool file_exists(const string &route) {
struct stat sb;
if (stat(route.c_str(), &sb) != 0) {
return false;
}
return (sb.st_mode & S_IFMT) == S_IFREG;
}
const string getRandomString(const unsigned int length) {
string ret;
for (unsigned int i = 0; i < length; i++) {
ret += 'a' + rand() % 26;
}
return ret;
}
bool is_str_ends(const string &s1, const string &suf) {
if (s1.size() < suf.size()) return false;
for (int i = 0; i < (int)suf.size(); i++) {
if (s1[s1.size() - suf.size() + i] != suf[i]) return false;
}
return true;
}
string read_path(const string &route) {
ifstream file(route.c_str());
string buffer;
string ret;
while (getline(file, buffer)) {
ret += buffer;
if (!file.eof()) {
ret += "\n";
}
}
return ret;
}
string trim_str(const string &s) {
if (s.size() == 0) { return ""; }
int from = 0, to = s.size() - 1;
while (from < (int)s.size() && isspace(s[from])) {
from++;
}
if (from == (int)s.size()) { return ""; }
while (to > from && isspace(s[to])) {
to--;
}
return s.substr(from, to - from + 1);
}
vector<string> split_lines(string s, const string &delimiter, bool remain_empty) {
vector<string> ret;
if (s.size() == 0) { return vector<string>(); }
int from = 0;
int newline = -1;
while ((newline = s.find(delimiter, from)) != -1) {
if (from == newline) {
from += delimiter.size();
if (remain_empty) {
ret.push_back("");
}
continue;
}
ret.push_back(s.substr(from, newline - from));
from = newline + delimiter.size();
}
if (from < (int)s.size()) {
ret.push_back(s.substr(from));
}
return ret;
}
string escape_newline(const string &s) {
string ret;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == '\n') {
ret += "\\n";
} else if (s[i] == '\r') {
ret += "\\r";
} else {
ret += s[i];
}
}
return ret;
}
int hexToInt(char c) {
if ('0' <= c && c <= '9') { return c - '0'; }
if ('a' <= c && c <= 'f') { return c - 'a' + 10; }
if ('A' <= c && c <= 'F') { return c - 'A' + 10; }
return -1;
}
string url_decode(const string &url) {
string ret;
for (int i = 0; i < (int)url.size(); i++) {
if (url[i] != '%') {
if (url[i] == '+') {
ret += ' ';
} else {
ret += url[i];
}
continue;
}
if (i + 2 >= (int)url.size()) {
break;
}
char c = hexToInt(url[i + 1]) * 16 + hexToInt(url[i + 2]);
ret += c;
i += 2;
}
return ret;
}
/* Custom Int -> String Converter */
string num_to_string(int n) {
if (n == -2147483647 - 1) return "-2147483648";
if (0 <= n && n < 10) { return string(1, '0' + n); }
string ans;
if (n < 0) {
ans += "-";
n = -n;
}
return ans + num_to_string(n / 10) + static_cast<char>('0' + n % 10);
}
string num_to_hexstring(int n) {
char c;
if (0 <= n && n <= 9) {
c = '0' + n;
} else {
c = 'A' + n - 10;
}
return string(1, c);
}
unsigned int content_find(const vector<unsigned char> &content, const string &target, unsigned int pos) {
for (unsigned int i = pos; i < content.size(); i++) {
unsigned int j = i;
for (j = i; j < content.size() && j - i < target.size(); j++) {
if (content[j] != target[j - i]) break;
}
if (j - i == target.size()) return i;
}
return static_cast<unsigned int>(-1);
}
bool is_extension(const string &extension) {
const string extension_list[] = {
".jpg", ".png", ".pdf", ".txt", ".jpeg"
};
const int extension_size = sizeof(extension_list) / sizeof(extension_list[0]);
for (int i = 0; i < extension_size; i++) {
if (extension_list[i] == extension) return true;
}
return false;
}