-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.hpp
96 lines (72 loc) · 2.37 KB
/
Helper.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
#ifndef Helper_hpp
#define Helper_hpp
#include "Debug.cpp"
#include <boost/filesystem.hpp>
#include <string.h>
#include "fcntl.h"
using namespace std;
class Socket {
public:
static void makeNonblocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK ) < 0) {
ERR("Error setting Socket flag O_NONBLOCKING.");
exit(EXIT_FAILURE);
}
}
};
using namespace boost::filesystem;
class FilesystemHelper {
public:
static void GetDirListingByFiletype(vector<string>& FileListRef, const string Path, const string FileType)
{
recursive_directory_iterator rdi(Path);
recursive_directory_iterator end_rdi;
for (; rdi != end_rdi; rdi++)
{
if (FileType.compare((*rdi).path().extension().string()) == 0)
{
FileListRef.push_back((*rdi).path().string());
DBG(210, (*rdi).path().string());
}
}
}
};
class String {
public:
//- TODO: make erase optional, ugly
static void split(string& String, const string Delimiter, vector<string>& ResultRef)
{
size_t FindPos = 0;
string Token;
while ((FindPos = String.find(Delimiter)) != String.npos) {
Token = String.substr(0, FindPos);
ResultRef.push_back(Token);
String.erase(0, FindPos + Delimiter.length());
}
//ResultRef.push_back(String);
//String.erase(0, Delimiter.length());
DBG(200, "String:'" << String << "'");
}
//- TODO: ugly, refactor (lambda?)
static void rsplit(string& String, size_t StartPos, const string Delimiter, vector<string>& ResultRef)
{
size_t FindPos = 0;
size_t FindPosLast = 0;
string Token;
StartPos -= Delimiter.length();
while ((FindPos = String.rfind(Delimiter, StartPos)) != String.npos) {
DBG(200, "FindPos:" << FindPos << " StartPos:" << StartPos);
Token = String.substr(FindPos+Delimiter.length(), (StartPos-FindPos));
DBG(200, "Token:" << Token);
ResultRef.push_back(Token);
StartPos = FindPos - Delimiter.length();
FindPosLast = FindPos;
}
DBG(200, "End FindPos:" << FindPosLast);
Token = String.substr(0, FindPosLast);
ResultRef.push_back(Token);
}
};
#endif