-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.hpp
272 lines (240 loc) · 8.82 KB
/
util.hpp
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
#pragma once
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <sys/resource.h>
#include <filesystem>
#include <math.h>
#include <ctime>
#include <map>
#include <fstream> // this is for the macros
// Avoid these macros. They work but don't work with a debugger
#define MACRO_ReadFileByLine(fileToOpen,sLine,code_block) { \
std::ifstream file; \
file.open(fileToOpen); \
if(file.is_open()) { \
while(getline(file,sLine)) { \
code_block; \
} \
file.close(); \
} else { \
util::qPrint("Error opening file \"",fileToOpen,"\""); \
} \
}
// Avoid these macros. They work but don't work with a debugger
#define MACRO_WriteToFile(fileToWrite,fileFlags,code_block) { \
std::ofstream file; \
file.open(fileToWrite,fileFlags); \
if(file.is_open()) { \
code_block; \
file.close(); \
} else { \
util::qPrint("Error openning file \"",fileToWrite,"\""); \
} \
}
namespace util {
extern std::map<std::string,std::string> mColors;
// Use instance util::alt instead of this class directly
class alternativeVariables {
public:
bool getAlt();
void setAlt(bool value);
//contructor
alternativeVariables(bool value);
private:
bool altValue;
};
extern alternativeVariables alt;
template<typename T>
T switchOnAlt(T a, T b) {
return (alt.getAlt()) ? a : b;
}
// Use instance util::gate instead of this class directly
class class_gate {
public:
void
open(std::string ID),
close(std::string ID),
toggle(std::string ID),
// Will overwrite if ID already exists
create(std::string ID, bool bStartOpen = true);
bool
exists(std::string ID),
state(std::string ID);
private:
std::map<std::string,bool> mGates;
};
extern class_gate gate;
struct int2d {
int x, y;
int2d(int _x, int _y) : x(_x), y(_y) {};
int2d() : x(0), y(0) {};
int2d(std::pair<int,int> _input) : x(_input.first), y(_input.second) {};
};
void qPrint(bool output);
void cPrint(const std::string sColor, bool output);
//quick print
template <typename T>
void qPrint(T output) {
std::cout << output << "\n";
}
template <typename T, typename... Args>
void qPrint(T output, Args... args){
std::cout << output << " ";
qPrint(args...);
}
template <typename... Args>
void qPrint(bool output,Args... args) {
std::cout << (output ? "True" : "False") << " ";
qPrint(args...);
}
//Color print
template <typename T>
void cPrint(const std::string sColor,T output) {
if(mColors.find(sColor) == mColors.end()) {
std::cout << mColors.at("red") << "Color '" << sColor << "' is not defined. Using qPrint..." << "\033[0m\n";
qPrint(output);
return;
}
std::cout << mColors.at(sColor) << output << "\033[0m\n";
}
template <typename T, typename... Args>
void cPrint(const std::string sColor,T output, Args... args) {
if(mColors.find(sColor) == mColors.end()) {
std::cout << mColors.at("red") << "Color '" << sColor << "' is not defined. Using qPrint..." << "\033[0m\n";
qPrint(output,args...);
return;
}
std::cout << mColors.at(sColor) << output << " \033[0m";
cPrint(sColor,args...);
}
template <typename... Args>
void cPrint(const std::string sColor, bool output, Args... args) {
if(mColors.find(sColor) == mColors.end()) {
std::cout << mColors.at("red") << "Color '" << sColor << "' is not defined. Using qPrint..." << "\033[0m\n";
qPrint(output,args...);
return;
}
std::cout << mColors.at(sColor) << (output ? "True" : "False") << " \033[0m";
cPrint(sColor,args...);
}
//if bReturnIndex = false will return 0 if not found
//if bReturnIndex = true will return -1 if not found
template <typename T>
int searchVector(std::vector<T>& inputVector, T toFind, bool bReturnIndex = false) {
int iSize = inputVector.size();
for(int i = 0; i < iSize; i++) {
if(inputVector[i] == toFind) {
return (i + (!bReturnIndex));
//need to return at least 1 if found at index 0 when bReturnIndex is false
}
}
return (0 - (bReturnIndex));
}
// Will return NULL if not found
template <typename A, typename B>
A findKey(std::map<A,B>& mSource, B toFind) {
for(auto& i : mSource) {
if(i.second == toFind) {
return i.first;
}
}
return NULL;
}
template <typename T>
void removeFirst(std::vector<T>& inputVector, T toRemove) {
for(int i = 0, last = inputVector.size() - 1; i <= last; i++) {
if(inputVector[i] == toRemove) {
inputVector.erase(inputVector.begin() + i);
return;
}
}
qPrint("Value",toRemove,"was not found in vector!");
}
template <typename T>
bool contains(std::unordered_set<T>& inputUSet, T toCheck) {
return (inputUSet.find(toCheck) != inputUSet.end());
}
template <typename T>
bool contains(std::string& sSource, T toFind) {
return (sSource.find(toFind) != sSource.npos);
}
template <typename T>
bool contains(std::vector<T>& sSource, T toFind) {
for(auto& i : sSource) {
if(i == toFind) return true;
}
return false;
}
template <typename A, typename B>
bool containsValue(std::map<A,B>& mSource, B toFind) {
for(auto& i : mSource) {
if(i.second == toFind) {
return true;
}
}
return false;
}
template <typename T>
void appendVectors(std::vector<T>& appendTarget,std::vector<T>& toBeAppended) {
appendTarget.insert(appendTarget.end(),toBeAppended.begin(),toBeAppended.end());
}
// Will increment a maped value that exists
// or will add key and initialize to 0 + incrementAmount
template <typename T>
int& mapIncrement(std::map<T,int>& sourceMap, T incrementKey, int incrementAmount = 1) {
if(sourceMap.find(incrementKey) == sourceMap.end()) {
sourceMap[incrementKey] = 0 + incrementAmount;
return sourceMap.at(incrementKey);
} else {
sourceMap[incrementKey] += incrementAmount;
return sourceMap.at(incrementKey);
}
}
// Will increment a maped value that exists
// or will add key and initialize to 0 + incrementAmount
template <typename T>
int& unordered_mapIncrement(std::unordered_map<T,int>& sourceMap, T incrementKey, int incrementAmount = 1) {
if(sourceMap.find(incrementKey) == sourceMap.end()) {
sourceMap[incrementKey] = 0 + incrementAmount;
return sourceMap.at(incrementKey);
} else {
sourceMap[incrementKey] += incrementAmount;
return sourceMap.at(incrementKey);
}
}
int
strToInt(std::string str),
lerpInt(int iStart, int iEnd, float fLerp);
float strToFloat(std::string str);
std::vector<std::string> splitStringOnChar(std::string& sToSplit,char cSplit);
bool
endsWith(std::string& sSource, const std::string sEnd),
hasPathPermission(std::filesystem::path pPath, bool bPrintErrors = false),
onlyContains(std::string& sSource, const char cToCheck),
onlyContains(std::string& sSource, const char* filter),
charFilter(char& input, const char* filter, const bool bAlpha = false, const bool bNumbers = false),
containsChar(const char* input, const char cToCheck),
flip(bool& toFlip),
containsAny(std::string& sSource, const char* chars);
std::vector<std::string> fileToVector(std::string file);
std::string
vectorToSingleStr(std::vector<std::string>& sFullVec,bool bAddNewLines = true),
vectorToSingleStr(std::vector<std::string>& sFullVec,std::string sBetweenEach,bool bSkipLast = true),
shorten(std::string& sToShorten, int iLength, char cEndOn),
shorten(std::string& sToShorten, int iLength),
shorten(std::string& sToShorten, char cEndOn),
argvToString(char* argv),
getCurrentDateTime(),
fileToString(std::string file);
void
printMemUse(rusage& usageRef),
toLowercase(std::string& sToModify),
removeAllOfChar(std::string& sToModify, char cToRemove),
removeAllOfChar(std::string &sToModify, std::string sMultiChars),
reverseString(std::string& sInput);
}