diff options
-rw-r--r-- | compat/defs.hpp | 2 | ||||
-rw-r--r-- | compat/os-file.cpp | 27 | ||||
-rw-r--r-- | compat/os-file.hpp | 11 |
3 files changed, 40 insertions, 0 deletions
diff --git a/compat/defs.hpp b/compat/defs.hpp index b967833e..ff2171c3 100644 --- a/compat/defs.hpp +++ b/compat/defs.hpp @@ -90,4 +90,6 @@ #define fm_ASAN 0 #endif +#ifndef fm_FILENAME_MAX #define fm_FILENAME_MAX (260) +#endif diff --git a/compat/os-file.cpp b/compat/os-file.cpp new file mode 100644 index 00000000..0d8f95c2 --- /dev/null +++ b/compat/os-file.cpp @@ -0,0 +1,27 @@ +#include "os-file.hpp" +#include "assert.hpp" +#include <cerrno> +#include <cr/StringView.h> + +#ifdef _WIN32 +#include <io.h> +#define fm_os_access _access +#else +#include <unistd.h> +#define fm_os_access access +#endif + +namespace floormat::fs { + +bool file_exists(StringView name) +{ + fm_assert(name.flags() & StringViewFlag::NullTerminated); + fm_debug_assert(!name.find('\0')); + if (!fm_os_access(name.data(), F_OK)) + return true; + int error = errno; + // just let it die if the file exists but can't be accessed + return error != ENOENT; +} + +} // namespace floormat::fs diff --git a/compat/os-file.hpp b/compat/os-file.hpp new file mode 100644 index 00000000..8e7962bf --- /dev/null +++ b/compat/os-file.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace floormat::fs { + +[[nodiscard]] bool file_exists(StringView name); + +} // namespace floormat::fs + +#ifndef fm_FILENAME_MAX +#define fm_FILENAME_MAX (260) +#endif |