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
//*****************************************************************************
// Copyright 2017-2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <dirent.h>
#include <fcntl.h>
#include <fstream>
#include <iomanip>
#include <sstream>
#include "util.hpp"
using namespace std;
string trim(const string& s)
{
string rc = s;
// trim trailing spaces
size_t pos = rc.find_last_not_of(" \t");
if (string::npos != pos)
{
rc = rc.substr(0, pos + 1);
}
// trim leading spaces
pos = rc.find_first_not_of(" \t");
if (string::npos != pos)
{
rc = rc.substr(pos);
}
return rc;
}
vector<string> split(const string& src, char delimiter, bool do_trim)
{
size_t pos;
string token;
size_t start = 0;
vector<string> rc;
while ((pos = src.find(delimiter, start)) != std::string::npos)
{
token = src.substr(start, pos - start);
start = pos + 1;
if (do_trim)
{
token = trim(token);
}
rc.push_back(token);
}
if (start <= src.size())
{
token = src.substr(start);
if (do_trim)
{
token = trim(token);
}
rc.push_back(token);
}
return rc;
}
bool is_version_number(const string& path)
{
bool rc = true;
vector<string> tokens = split(path, '.');
for (string s : tokens)
{
for (char c : s)
{
if (!isdigit(c))
{
rc = false;
}
}
}
return rc;
}
string path_join(const string& s1, const string& s2)
{
string rc;
if (s2.size() > 0)
{
if (s2[0] == '/')
{
rc = s2;
}
else if (s1.size() > 0)
{
rc = s1;
if (rc[rc.size() - 1] != '/')
{
rc += "/";
}
rc += s2;
}
else
{
rc = s2;
}
}
else
{
rc = s1;
}
return rc;
}
std::string read_file_to_string(const std::string& path)
{
std::ifstream f(path);
std::stringstream ss;
ss << f.rdbuf();
return ss.str();
}
void iterate_files_worker(const string& path,
std::function<void(const string& file, bool is_dir)> func,
bool recurse)
{
DIR* dir;
struct dirent* ent;
// If we cannot open the directory, we silently ignore it.
if ((dir = opendir(path.c_str())) != nullptr)
{
while ((ent = readdir(dir)) != nullptr)
{
string name = ent->d_name;
switch (ent->d_type)
{
case DT_DIR:
if (name != "." && name != "..")
{
string dir_path = path_join(path, name);
if (recurse)
{
iterate_files(dir_path, func, recurse);
}
func(dir_path, true);
}
break;
case DT_LNK: break;
case DT_REG:
{
string file_name = path_join(path, name);
func(file_name, false);
break;
}
default: break;
}
}
closedir(dir);
}
}
void iterate_files(const string& path,
std::function<void(const string& file, bool is_dir)> func,
bool recurse)
{
vector<string> files;
vector<string> dirs;
iterate_files_worker(path,
[&files, &dirs](const string& file, bool is_dir) {
if (is_dir)
dirs.push_back(file);
else
files.push_back(file);
},
recurse);
for (auto f : files)
{
func(f, false);
}
for (auto f : dirs)
{
func(f, true);
}
}
std::string get_file_name(const std::string& s)
{
string rc = s;
auto pos = s.find_last_of('/');
if (pos != string::npos)
{
rc = s.substr(pos + 1);
}
return rc;
}
std::string get_file_ext(const std::string& s)
{
string rc = get_file_name(s);
auto pos = rc.find_last_of('.');
if (pos != string::npos)
{
rc = rc.substr(pos);
}
else
{
rc = "";
}
return rc;
}
string to_hex(int value)
{
stringstream ss;
ss << "0x" << std::hex << std::setw(2) << std::setfill('0') << value;
return ss.str();
}
void dump(ostream& out, const void* vdata, size_t size)
{
const uint8_t* data = reinterpret_cast<const uint8_t*>(vdata);
size_t index = 0;
while (index < size)
{
out << " ";
for (size_t i = 0; i < 16 && index < size; i++)
{
if (i != 0)
{
out << ", ";
}
out << to_hex(data[index++]);
}
out << ",\n";
}
}
time_t get_timestamp(const std::string& filename)
{
time_t rc = 0;
struct stat st;
if (stat(filename.c_str(), &st) == 0)
{
rc = st.st_mtime;
}
return rc;
}