-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
278 lines (248 loc) · 7.17 KB
/
Copy pathutils.cpp
File metadata and controls
278 lines (248 loc) · 7.17 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* ************************************************************************** */
/* */
/* :::::::: */
/* utils.cpp :+: :+: */
/* +:+ */
/* By: bdekonin <bdekonin@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/08/23 11:40:33 by bdekonin #+# #+# */
/* Updated: 2022/11/06 12:10:28 by rkieboom ######## odam.nl */
/* */
/* ************************************************************************** */
# include "inc/Utils.hpp"
# include <cstring>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int get_root_options(const char *path)
{
struct stat st;
if (lstat(path, &st) != 0)
return -1;
else if (st.st_mode & S_IFDIR)
return 0;
else
return 1;
}
/// @brief Function that converrts a string to a integer. Throws an error if the string is not a integer.
/// @param str String that needs to be converted to a integer.
/// @return Returns the integer that is converted from the string.
int ft_stoi(const std::string &str)
{
std::stringstream ss(str);
int num;
if((ss >> num).fail())
throw std::invalid_argument("stoi: " + str);
return num;
}
/// @brief Function that creates a string with x amount of spaces. Used for creating and spacing auto index files.
static std::string get_x_spaces(int x)
{
if (x < 0)
return " ";
std::string spaces(x, ' ');
return (spaces);
}
/// @brief Function that creates one line for the autoindex generator
/// @param path Path of the file
/// @param name Name of the file
/// @param ts Timestamp of the file
/// @param bytes Bytes of the file
/// @param is_file Boolean that checks if the file is a file or a directory
/// @return Returns the string that is created for the autoindex generator
std::string create_autoindex_line(const std::string &path, const char *name, time_t ts, const unsigned long bytes, bool is_file)
{
size_t spaces = 0;
std::string line = "<a href=\"PATH\">NAME</a> SPACE DATE BYTES";
std::string date = "";
char buff[100];
strftime(buff, 100, "%d-%b-%Y %H:%M:%S", localtime(&ts));
spaces = 90 - (path.size() + 2);
if (is_file == false)
{
spaces -= 1;
line.replace(line.find("PATH"), 4, path + "/");
}
else
line.replace(line.find("PATH"), 4, path);
line.replace(line.find("SPACE"), 5, get_x_spaces(spaces));
if (is_file == false)
line.replace(line.find("NAME"), 4, std::string(name) + "/");
else
line.replace(line.find("NAME"), 4, name);
line.replace(line.find("DATE"), 4, buff);
line.replace(line.find("BYTES"), 5, std::to_string(bytes));
return (line);
}
/// @brief Converts string to upper
/// @param str String that needs to be converted to upper
void to_upper(std::string &str)
{
for (size_t i = 0; i < str.size(); i++)
str[i] = toupper(str[i]);
}
/// @brief Converts string to lower
/// @param str String that needs to be converted to lower
void to_lower(std::string &str)
{
for (size_t i = 0; i < str.size(); i++)
str[i] = tolower(str[i]);
}
/// @brief Function that counts characters in a string
/// @param str String that needs to be counted
/// @param c Character that needs to be counted
/// @return Returns the amount of characters in the string
size_t count(std::string const &str, char c)
{
size_t count = 0;
for (size_t i = 0; i < str.length(); i++)
if (str[i] == c)
count++;
return (count);
}
size_t count(std::string const str, char c)
{
size_t count = 0;
for (size_t i = 0; i < str.length(); i++)
if (str[i] == c)
count++;
return (count);
}
/// @brief Function that matches the '{' with the '}' in a string
/// @param str String that needs to be checked
/// @param curlyBraceOpen Position of the '{'
/// @return Returns the position of the '}'
size_t getCurlyBraceMatch(std::string const &str, size_t curlyBraceOpen)
{
size_t pos = curlyBraceOpen;
int BraceSubString = 1;
while (pos < str.length() && BraceSubString != 0)
{
pos = str.find_first_of("{}", pos + 1);
if (str[pos] == '{')
BraceSubString++;
else if (str[pos] == '}')
BraceSubString--;
}
return pos;
}
/// @brief Function that matches the '{' with the '}' in a string
/// @param v Vector that needs to be checked
/// @param curlyBraceOpen Position of the '{'
/// @return Returns the position of the '}'
size_t getCurlyBraceMatch(const std::vector<std::string> &v, size_t curlyBraceOpen)
{
size_t pos = curlyBraceOpen;
int BraceSubString = 1;
// v[pos].find('{') == std::string::npos
while (pos < v.size() && BraceSubString != 0)
{
if (v[pos].find('{') != std::string::npos)
BraceSubString++;
else if (v[pos].find('}') != std::string::npos)
BraceSubString--;
pos++;
}
return pos;
}
/// @brief Similar to the function ft_split. This function splits a string on a character and returns a vector with the split strings.
/// @param str String that needs to be split
/// @param delims Characters that needs to be split on
/// @param out Vector that will be filled with the split strings
void split(std::string const &str, const char* delims, std::vector<std::string> &out)
{
size_t posBegin = 0;
size_t posEnd;
size_t posDelim;
size_t braceOpen;
size_t braceClose = 0;
size_t subLength;
while (posBegin < str.length())
{
braceOpen = str.find('{', posBegin);
if (braceOpen != std::string::npos)
braceClose = getCurlyBraceMatch(str, braceOpen);
posDelim = str.find_first_of(delims, posBegin);
// if (braceOpen < posDelim)
// posEnd = braceClose + 1;
// else
posEnd = std::min(posDelim, str.length());
subLength = posEnd - posBegin;
if (subLength)
{
std::string sub = str.substr(posBegin, subLength);
{
size_t begin = sub.find_first_not_of(whitespaces);
if (begin != std::string::npos)
sub = sub.substr(begin);
}
if (sub.find_first_not_of(whitespaces) != std::string::npos)
out.push_back(sub);
}
(void)braceClose;
posBegin = posEnd + 1;
}
}
static int trim_left(std::string &s1, const char *set)
{
int i;
int j;
i = 0;
while (s1[i])
{
j = 0;
while (s1[i] != set[j] && set[j])
j++;
if (!set[j])
break ;
i++;
}
return (i);
}
static int trim_right(std::string &s1, const char *set)
{
int i;
int j;
i = s1.size() - 1;
while (i >= 0)
{
j = 0;
while (s1[i] != set[j] && set[j])
j++;
if (!set[j])
break ;
i--;
}
return (i);
}
std::string ft_strtrim(std::string &s1, const char *set)
{
std::string str;
int left;
int right;
if (s1.size() == 0)
return (std::string(""));
if (!set)
return (std::string(s1));
left = trim_left(s1, set);
right = trim_right(s1, set);
if (right - left + 1 < 0)
return (std::string(""));
str = s1.substr(left, right - left + 1);
return (str);
}
char *ft_strnstr(const char *s1, const char *s2, size_t len)
{
size_t l2;
l2 = strlen(s2);
if (!l2)
return (char *)s1;
while (len >= l2) {
len--;
if (!memcmp(s1, s2, l2))
return (char *)s1;
s1++;
}
return NULL;
}