-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefinitions.cpp
More file actions
203 lines (188 loc) · 5.41 KB
/
definitions.cpp
File metadata and controls
203 lines (188 loc) · 5.41 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<string>
#include<boost/regex.hpp>
#include <sys/stat.h>
#include <vector>
#include "definitions.h"
#include "main.h"
using namespace std;
void printRegexError(boost::regex_error &e)
{
const char* precedeError = "\n %s";
switch (e.code())
{
case boost::regex_constants::error_collate:
printf(precedeError, "The expression contained an invalid collating element name.");
break;
case boost::regex_constants::error_ctype:
printf(precedeError, "The expression contained an invalid character class name.");
break;
case boost::regex_constants::error_escape:
printf(precedeError, "The expression contained an invalid escaped character, or a trailing escape.");
break;
case boost::regex_constants::error_backref:
printf(precedeError, "The expression contained an invalid back reference.");
break;
case boost::regex_constants::error_brack:
printf(precedeError, "The expression contained mismatched brackets ([ and ]).");
break;
case boost::regex_constants::error_paren:
printf(precedeError, "The expression contained mismatched parentheses (( and )).");
break;
case boost::regex_constants::error_brace:
printf(precedeError, "The expression contained mismatched braces ({ and }).");
break;
case boost::regex_constants::error_badbrace:
printf(precedeError, "The expression contained an invalid range between braces ({ and }).");
break;
case boost::regex_constants::error_range:
printf(precedeError, "The expression contained an invalid character range.");
break;
case boost::regex_constants::error_space:
printf(precedeError, "There was insufficient memory to convert the expression into a finite state machine.");
break;
case boost::regex_constants::error_badrepeat:
printf(precedeError, "The expression contained a repeat specifier (one of *?+{) that was not preceded by a valid regular expression.");
break;
case boost::regex_constants::error_complexity:
printf(precedeError, "The complexity of an attempted match against a regular expression exceeded a pre-set level.");
break;
case boost::regex_constants::error_stack:
printf(precedeError, "There was insufficient memory to determine whether the regular expression could match the specified character sequence.");
break;
default:
printf(precedeError, "Unknown error :(");
break;
}
}
bool replace(string& str, const string& from, const string& to)
{
size_t start_pos = str.find(from);
if (start_pos == string::npos) return false;
str.replace(start_pos, from.length(), to);
return true;
}
bool replaceByPointers(string& str, string* from, string* to)
{
size_t start_pos = str.find(*from);
if (start_pos == string::npos) return false;
str.replace(start_pos, from->length(), *to);
return true;
}
string bin2hex(const string& input)
{
string res;
const char hex[] = "0123456789ABCDEF";
for(auto sc : input)
{
unsigned char c = static_cast<unsigned char>(sc);
res += hex[c >> 4];
res += hex[c & 0xf];
}
return res;
}
string bin2hex(char c)
{
string res;
const char hex[] = "0123456789ABCDEF";
unsigned char c2 = static_cast<unsigned char>(c);
res += hex[c2 >> 4];
res += hex[c2 & 0xf];
return res;
}
string generateParamString(vector<string>* keys, vector<string>* values)
{
string outputString = string("");
for (int i=0;i<keys->size();i++)
{
outputString.append(keys->at(i));
outputString += '=';
outputString.append(values->at(i));
if (i!=keys->size()-1) outputString += '&';
}
return outputString;
}
void parseParamString(string paramString, vector<string>* keys, vector<string>* values)
{
bool inKey = true;
stringstream buff;
string emptyStr = string("");
int l = paramString.length();
for (int i=0;i<l;i++)
{
if (paramString[i]=='&')
{
if (inKey)
{
keys->push_back(buff.str());
values->push_back(emptyStr);
}
else
{
values->push_back(buff.str());
}
inKey = true;
buff.str("");
}
else if (inKey&&(paramString[i]=='='))
{
inKey = false;
keys->push_back(buff.str());
buff.str("");
}
else buff << paramString[i];
}
if (inKey)
{
keys->push_back(buff.str());
values->push_back(emptyStr);
}
else
{
values->push_back(buff.str());
}
buff.str("");
}
bool searchForFile(char* directory, string& requestPath, vector<string>& indexes, string& filePath, string& fileName)
{
bool exists = true;
filePath = string(directory)+requestPath;
int lastSlash = filePath.rfind('/');
string filePathOnly = filePath.substr(0, lastSlash+1);
fileName = filePath.substr(lastSlash+1, requestPath.length()-lastSlash);
int tryingIndex = -1;
bool triedAddingSlash = false;
fileName = filePath.substr(lastSlash+1, requestPath.length()-lastSlash-1);
tryFileAgain:
lastSlash = filePath.rfind('/');
filePathOnly = filePath.substr(0, lastSlash+1);
struct stat buffer;
if (stat(filePath.c_str(), &buffer)!=0) // If the file doesn't exist....
{
if (tryingIndex<indexes.size()-1)
{
tryingIndex++;
fileName = indexes.at(tryingIndex);
filePath = filePathOnly+fileName;
goto tryFileAgain;
}
exists = false;
}
else if (S_ISDIR(buffer.st_mode)) // If it's a directory...
{
if (filePath.back()!='/') filePath += "/";
fileName = "";
filePath += indexes.at(0);
tryingIndex = 0;
goto tryFileAgain;
}
else exists = true;
if ((!triedAddingSlash) && (!exists))
{
triedAddingSlash = true;
filePath += "/";
fileName = "";
tryingIndex = -1;
goto tryFileAgain;
}
return exists;
}