-
Notifications
You must be signed in to change notification settings - Fork 0
/
utl.cpp
238 lines (165 loc) · 4.54 KB
/
utl.cpp
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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cctype>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include "utl.h"
using namespace std;
bool file_exists (const string &name)
{
if (name.empty())
return false;
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
bool ends_with (std::string const & value, std::string const & ending)
{
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
std::string resolve_symlink (const std::string &path)
{
bool is_symlink = false;
struct stat buf;
int r = stat (path.c_str(), &buf);
if (S_ISLNK(buf.st_mode))
is_symlink = true;
//if (S_ISREG(buf.st_mode)) printf (" stat says file\n");
if (is_symlink)
{
char resolved_fname[FILENAME_MAX];
int count = readlink (path.c_str(), resolved_fname, sizeof(resolved_fname));
if (count >= 0)
{
resolved_fname[count] = '\0';
return resolved_fname;
}
}
return path;
}
std::vector <std::string> files_get_list (const std::string &path)
{
DIR *directory;
struct dirent *dir_entry;
std::vector <std::string> result;
directory = opendir (path.c_str());
if (! directory)
return result;
while ((dir_entry = readdir (directory)))
{
std::string t = dir_entry->d_name;
if (t != "." && t != "..")
result.push_back (path + "/" + t);
}
closedir (directory);
return result;
}
std::vector <std::string> files_get_list (const std::string &path, const std::string &ext) //ext with dot: ".txt"
{
DIR *directory;
struct dirent *dir_entry;
vector <string> result;
directory = opendir(path.c_str());
if (! directory)
return result;
while (dir_entry = readdir (directory))
{
// std::cout << dir_entry->d_name << std::endl;
string t = dir_entry->d_name;
if (t.rfind (ext) != string::npos)
result.push_back (path + "/" + t);
}
closedir (directory);
return result;
}
std::string get_home_dir()
{
std::string result;
#if !defined(_WIN32) || !defined(_WIN64)
const char *homedir = getenv ("HOME");
if (homedir != NULL)
result = homedir;
#else
char homeDirStr[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, homeDirStr)))
result = homeDirStr;
#endif
return result;
}
std::string get_file_path (const std::string &path)
{
size_t i = path.rfind ("/", path.length());
if (i != std::string::npos)
return path.substr (0, i);
return std::string();
}
std::string string_file_load (const string &fname)
{
if (fname.empty())
return string();
std::ifstream t (fname.c_str());
std::string s ((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return s;
}
/// Try to find in the Haystack the Needle - ignore case
bool findStringIC(const std::string & strHaystack, const std::string & strNeedle)
{
auto it = std::search(
strHaystack.begin(), strHaystack.end(),
strNeedle.begin(), strNeedle.end(),
[](unsigned char ch1, unsigned char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
);
return (it != strHaystack.end() );
}
std::string string_to_lower (const std::string &s)
{
string result = s;
std::for_each (
result.begin(),
result.end(),
[](char & c) {
c = ::tolower(c);
});
return result;
}
vector <string> split_string_to_vector (const string& s, const string& delimeter, const bool keep_empty)
{
vector <string> result;
if (delimeter.empty())
{
result.push_back (s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true)
{
subend = search (substart, s.end(), delimeter.begin(), delimeter.end());
string temp (substart, subend);
if (keep_empty || ! temp.empty())
result.push_back (temp);
if (subend == s.end())
break;
substart = subend + delimeter.size();
}
return result;
}
string string_replace_all (const string &s, const string &from, const string &to)
{
string result = s;
size_t i = 0;
do
{
i = result.find (from);
if (i != string::npos)
result.replace (i, from.length(), to);
}
while (i != string::npos);
return result;
}