Skip to content

Commit a7022f1

Browse files
marek-trtikpeterschrammel
authored andcommitted
Improve file-util code style
1 parent 82ace0f commit a7022f1

File tree

2 files changed

+156
-143
lines changed

2 files changed

+156
-143
lines changed

src/util/file_util.cpp

+121-118
Original file line numberDiff line numberDiff line change
@@ -163,174 +163,177 @@ std::string concat_dir_file(
163163
# include <sys/stat.h>
164164
#endif
165165

166-
namespace fileutl { namespace {
167166

168-
169-
std::string::size_type parse_last_dir_pos(std::string const& file_pathname)
167+
static std::string::size_type fileutl_parse_last_dir_pos(
168+
std::string const& file_pathname
169+
)
170170
{
171-
std::string::size_type const last_slash_pos = file_pathname.find_last_of('/');
172-
std::string::size_type const last_backslash_pos = file_pathname.find_last_of('\\');
173-
std::string::size_type const last_dir_pos =
174-
last_slash_pos == std::string::npos ?
175-
(last_backslash_pos == std::string::npos ? 0ULL :
176-
last_backslash_pos + 1ULL) :
177-
(last_backslash_pos == std::string::npos ? last_slash_pos + 1ULL :
178-
(last_slash_pos < last_backslash_pos ? last_backslash_pos :
179-
last_slash_pos))
180-
;
181-
return last_dir_pos;
171+
std::string::size_type const last_slash_pos =
172+
file_pathname.find_last_of('/');
173+
std::string::size_type const last_backslash_pos =
174+
file_pathname.find_last_of('\\');
175+
std::string::size_type const last_dir_pos =
176+
last_slash_pos == std::string::npos ?
177+
(last_backslash_pos == std::string::npos ?
178+
0ULL :
179+
last_backslash_pos + 1ULL) :
180+
(last_backslash_pos == std::string::npos ?
181+
last_slash_pos + 1ULL :
182+
(last_slash_pos < last_backslash_pos ?
183+
last_backslash_pos :
184+
last_slash_pos))
185+
;
186+
return last_dir_pos;
182187
}
183188

184189

185-
}}
186-
187-
namespace fileutl {
188-
189-
190-
bool file_exists(std::string const& pathname)
190+
bool fileutl_file_exists(std::string const& pathname)
191191
{
192-
std::ifstream f{pathname,std::ios::binary};
193-
return f.good();
192+
std::ifstream f{pathname,std::ios::binary};
193+
return f.good();
194194
}
195195

196-
bool is_directory(std::string const& pathname)
196+
bool fileutl_is_directory(std::string const& pathname)
197197
{
198-
if (!file_exists(pathname))
199-
return false;
200-
# if defined(WIN32)
198+
if (!fileutl_file_exists(pathname))
199+
return false;
200+
# if defined(WIN32)
201201
return PathIsDirectory(pathname.c_str());
202-
# elif defined(__linux__) || defined(__APPLE__)
203-
struct stat buf;
204-
stat(pathname.c_str(), &buf);
205-
return S_ISDIR(buf.st_mode);
206-
//return S_ISREG(buf.st_mode); // is file ?
207-
# else
208-
# error "Unsuported platform."
209-
# endif
202+
# elif defined(__linux__) || defined(__APPLE__)
203+
struct stat buf;
204+
stat(pathname.c_str(), &buf);
205+
return S_ISDIR(buf.st_mode);
206+
//return S_ISREG(buf.st_mode); // is file ?
207+
# else
208+
# error "Unsuported platform."
209+
# endif
210210
}
211211

212-
uint64_t file_size(std::string const& file_pathname)
212+
uint64_t fileutl_file_size(std::string const& file_pathname)
213213
{
214-
std::ifstream f{file_pathname,std::ios::binary};
215-
std::streampos const begin = f.tellg();
216-
f.seekg(0ULL,std::ios::end);
217-
std::streampos const end = f.tellg();
218-
return end - begin;
214+
std::ifstream f{file_pathname,std::ios::binary};
215+
std::streampos const begin = f.tellg();
216+
f.seekg(0ULL,std::ios::end);
217+
std::streampos const end = f.tellg();
218+
return end - begin;
219219
}
220220

221-
std::string parse_name_in_pathname(std::string const& file_pathname)
221+
std::string fileutl_parse_name_in_pathname(std::string const& file_pathname)
222222
{
223-
return file_pathname.substr(parse_last_dir_pos(file_pathname));
223+
return file_pathname.substr(fileutl_parse_last_dir_pos(file_pathname));
224224
}
225225

226-
std::string parse_path_in_pathname(std::string const& file_pathname)
226+
std::string fileutl_parse_path_in_pathname(std::string const& file_pathname)
227227
{
228-
return file_pathname.substr(0U,parse_last_dir_pos(file_pathname));
228+
return file_pathname.substr(0U,fileutl_parse_last_dir_pos(file_pathname));
229229
}
230230

231-
std::string remove_extension(std::string const& filename)
231+
std::string fileutl_remove_extension(std::string const& filename)
232232
{
233233
return filename.substr(0U,filename.find_last_of('.'));
234234
}
235235

236-
void create_directory(std::string const& pathname)
236+
void fileutl_create_directory(std::string const& pathname)
237237
{
238-
# if defined(WIN32)
239-
std::system((std::string("mkdir \"") + pathname + "\"").c_str());
240-
# elif defined(__linux__) || defined(__APPLE__)
241-
auto ignore = std::system((std::string("mkdir -p \"") + pathname + "\"").c_str());
242-
(void)ignore;
243-
# else
244-
# error "Unsuported platform."
245-
# endif
238+
# if defined(WIN32)
239+
std::system((std::string("mkdir \"") + pathname + "\"").c_str());
240+
# elif defined(__linux__) || defined(__APPLE__)
241+
auto ignore = std::system(
242+
(std::string("mkdir -p \"") + pathname + "\"").c_str()
243+
);
244+
(void)ignore;
245+
# else
246+
# error "Unsuported platform."
247+
# endif
246248
}
247249

248-
std::string concatenate_file_paths(std::string const& left_path, std::string const& right_path)
250+
std::string fileutl_concatenate_file_paths(std::string const& left_path,
251+
std::string const& right_path)
249252
{
250-
if (left_path.empty())
251-
return right_path;
252-
if (right_path.empty())
253-
return left_path;
254-
return left_path + "/" + right_path;
253+
if (left_path.empty())
254+
return right_path;
255+
if (right_path.empty())
256+
return left_path;
257+
return left_path + "/" + right_path;
255258
}
256259

257-
std::string absolute_path(std::string const& path)
260+
std::string fileutl_absolute_path(std::string const& path)
258261
{
259-
// TODO: portability - this implementation won't probably work on Windows...
260-
std::vector<char> buffer(10000,0);
261-
return realpath(path.c_str(),&buffer.at(0));
262+
// TODO: portability - this implementation won't probably work on Windows...
263+
std::vector<char> buffer(10000,0);
264+
return realpath(path.c_str(),&buffer.at(0));
262265
}
263266

264-
std::string normalise_path(std::string const& path)
267+
std::string fileutl_normalise_path(std::string const& path)
265268
{
266-
std::string result = path;
267-
std::replace(result.begin(),result.end(),'\\','/');
268-
std::string::size_type pos = 0;
269-
while((pos = result.find("/./",0)) != std::string::npos)
270-
result.replace(pos,3,"/");
271-
// TODO: more fixes should be applied (e.g. /../, //, etc.)
272-
return result;
269+
std::string result = path;
270+
std::replace(result.begin(),result.end(),'\\','/');
271+
std::string::size_type pos = 0;
272+
while((pos = result.find("/./",0)) != std::string::npos)
273+
result.replace(pos,3,"/");
274+
// TODO: more fixes should be applied (e.g. /../, //, etc.)
275+
return result;
273276
}
274277

275-
void split_pathname(std::string const& pathname, std::vector<std::string>& output)
278+
void fileutl_split_pathname(std::string const& pathname,
279+
std::vector<std::string>& output)
276280
{
277-
std::istringstream istr(normalise_path(pathname));
278-
std::string token;
279-
while (std::getline(istr,token,'/'))
280-
output.push_back(token);
281+
std::istringstream istr(fileutl_normalise_path(pathname));
282+
std::string token;
283+
while (std::getline(istr,token,'/'))
284+
output.push_back(token);
281285
}
282286

283-
std::string join_path_parts(std::vector<std::string> const& parts)
287+
std::string fileutl_join_path_parts(std::vector<std::string> const& parts)
284288
{
285-
if (parts.empty())
286-
return "";
287-
std::string result = parts.at(0);
288-
for (uint64_t i = 1ULL; i < parts.size(); ++i)
289-
{
290-
result.push_back('/');
291-
result.append(parts.at(i));
292-
}
293-
return result;
289+
if (parts.empty())
290+
return "";
291+
std::string result = parts.at(0);
292+
for (uint64_t i = 1ULL; i < parts.size(); ++i)
293+
{
294+
result.push_back('/');
295+
result.append(parts.at(i));
296+
}
297+
return result;
294298
}
295299

296-
std::string get_common_preffix(std::string const& pathname1, std::string const& pathname2)
300+
std::string fileutl_get_common_preffix(std::string const& pathname1,
301+
std::string const& pathname2)
297302
{
298-
std::vector<std::string> split1;
299-
split_pathname(pathname1,split1);
303+
std::vector<std::string> split1;
304+
fileutl_split_pathname(pathname1,split1);
300305

301-
std::vector<std::string> split2;
302-
split_pathname(pathname2,split2);
306+
std::vector<std::string> split2;
307+
fileutl_split_pathname(pathname2,split2);
303308

304-
std::vector<std::string> common_split;
305-
for (uint64_t i = 0ULL, size = std::min(split1.size(),split2.size());
306-
i < size && split1.at(i) == split2.at(i);
307-
++i)
308-
common_split.push_back(split1.at(i));
309+
std::vector<std::string> common_split;
310+
for (uint64_t i = 0ULL, size = std::min(split1.size(),split2.size());
311+
i < size && split1.at(i) == split2.at(i);
312+
++i)
313+
common_split.push_back(split1.at(i));
309314

310-
std::string const result = join_path_parts(common_split);
311-
return result;
315+
std::string const result = fileutl_join_path_parts(common_split);
316+
return result;
312317
}
313318

314-
std::string get_relative_path(std::string const& pathname, std::string const& directory)
319+
std::string fileutl_get_relative_path(std::string const& pathname,
320+
std::string const& directory)
315321
{
316-
std::vector<std::string> split1;
317-
split_pathname(pathname,split1);
318-
std::reverse(split1.begin(),split1.end());
319-
320-
std::vector<std::string> split2;
321-
split_pathname(directory,split2);
322-
std::reverse(split2.begin(),split2.end());
323-
324-
while (!split1.empty() && !split2.empty() && split1.back() == split2.back())
325-
{
326-
split1.pop_back();
327-
split2.pop_back();
328-
}
329-
330-
std::reverse(split1.begin(),split1.end());
331-
std::string const result = join_path_parts(split1);
332-
return result;
333-
}
322+
std::vector<std::string> split1;
323+
fileutl_split_pathname(pathname,split1);
324+
std::reverse(split1.begin(),split1.end());
325+
326+
std::vector<std::string> split2;
327+
fileutl_split_pathname(directory,split2);
328+
std::reverse(split2.begin(),split2.end());
334329

330+
while (!split1.empty() && !split2.empty() && split1.back() == split2.back())
331+
{
332+
split1.pop_back();
333+
split2.pop_back();
334+
}
335335

336+
std::reverse(split1.begin(),split1.end());
337+
std::string const result = fileutl_join_path_parts(split1);
338+
return result;
336339
}

src/util/file_util.h

+35-25
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,40 @@ std::string get_current_working_directory();
2121
std::string concat_dir_file(const std::string &directory,
2222
const std::string &file_name);
2323

24-
/**
25-
* This functions implement utility functions which provide us disc-related
26-
* functionality, like manipulation with disc paths (concatenation/splitting),
27-
* checking for existence of files, etc.
28-
*/
29-
namespace fileutl {
30-
31-
32-
bool file_exists(std::string const& pathname);
33-
bool is_directory(std::string const& pathname);
34-
uint64_t file_size(std::string const& file_pathname);
35-
std::string parse_name_in_pathname(std::string const& file_pathname);
36-
std::string parse_path_in_pathname(std::string const& file_pathname);
37-
std::string remove_extension(std::string const& filename);
38-
void create_directory(std::string const& pathname);
39-
std::string concatenate_file_paths(std::string const& left_path, std::string const& right_path);
40-
std::string absolute_path(std::string const& path);
41-
std::string normalise_path(std::string const& path);
42-
void split_pathname(std::string const& pathname, std::vector<std::string>& output);
43-
std::string join_path_parts(std::vector<std::string> const& parts);
44-
std::string get_common_preffix(std::string const& pathname1, std::string const& pathname2);
45-
std::string get_relative_path(std::string const& pathname, std::string const& directory);
46-
47-
48-
}
24+
/// These functions provide us disc-related functionality, like manipulation
25+
/// with disc paths (concatenation/splitting), checking for existence of files,
26+
/// etc.
27+
28+
bool fileutl_file_exists(std::string const& pathname);
29+
30+
bool fileutl_is_directory(std::string const& pathname);
31+
32+
uint64_t fileutl_file_size(std::string const& file_pathname);
33+
34+
std::string fileutl_parse_name_in_pathname(std::string const& file_pathname);
35+
36+
std::string fileutl_parse_path_in_pathname(std::string const& file_pathname);
37+
38+
std::string fileutl_remove_extension(std::string const& filename);
39+
40+
void fileutl_create_directory(std::string const& pathname);
41+
42+
std::string fileutl_concatenate_file_paths(std::string const& left_path,
43+
std::string const& right_path);
44+
45+
std::string fileutl_absolute_path(std::string const& path);
46+
47+
std::string fileutl_normalise_path(std::string const& path);
48+
49+
void fileutl_split_pathname(std::string const& pathname,
50+
std::vector<std::string>& output);
51+
52+
std::string fileutl_join_path_parts(std::vector<std::string> const& parts);
53+
54+
std::string fileutl_get_common_preffix(std::string const& pathname1,
55+
std::string const& pathname2);
56+
57+
std::string fileutl_get_relative_path(std::string const& pathname,
58+
std::string const& directory);
4959

5060
#endif // CPROVER_UTIL_FILE_UTIL_H

0 commit comments

Comments
 (0)