Skip to content

Commit af7c6a5

Browse files
author
Daniel Kroening
committed
add file_exists(path) as wrapper for access(path)
This offers the obvious wrapper around the OS-specific access() function, to be used until we switch to C++17.
1 parent 4682d07 commit af7c6a5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/util/file_util.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,12 @@ bool create_directory(const std::string &path)
203203
return mkdir(path.c_str(), 0777) == 0;
204204
#endif
205205
}
206+
207+
bool file_exists(const std::string &path)
208+
{
209+
#ifdef _WIN32
210+
return _waccess(utf8_to_utf16_native_endian(path).c_str(), 0) == 0;
211+
#else
212+
return access(path.c_str(), F_OK) == 0;
213+
#endif
214+
}

src/util/file_util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ bool is_directory(const std::string &path);
3232
/// \return true iff the directory was created
3333
bool create_directory(const std::string &path);
3434

35+
/// Check whether file with given path exists.
36+
/// C++17 will allow us to use std::filesystem::directory_entry(file).exists()
37+
/// \return true iff the file exists
38+
bool file_exists(const std::string &path);
39+
3540
#endif // CPROVER_UTIL_FILE_UTIL_H

0 commit comments

Comments
 (0)