-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
49 lines (35 loc) · 995 Bytes
/
util.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
// util.cpp - utility code
#include "util.h"
#include <sys/stat.h>
#include <string>
namespace zezax::sensorrd {
using std::string;
using std::string_view;
bool startsWith(string_view str, string_view head) {
if (str.size() < head.size())
return false;
string_view pfx(str.data(), head.size());
return (pfx == head);
}
bool endsWith(string_view str, string_view tail) {
if (str.size() < tail.size())
return false;
string_view suf(str.data() + (str.size() - tail.size()), tail.size());
return (suf == tail);
}
void split(string_view src,
char delim,
string_view &pfx,
string_view &suf) {
size_t pos = src.find(delim);
if (pos == string_view::npos)
pos = src.size();
pfx = string_view(src.data(), pos);
suf = string_view(src.data() + pos, src.size() - pos);
}
bool pathExists(const string &path) {
struct stat sst;
int res = stat(path.c_str(), &sst);
return (res == 0);
}
} // namespace zezax::sensorrd