diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-16 18:13:24 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-16 21:20:11 +0300 |
commit | 182bff76382d59f44f0ac3c69beb55dacde75eb5 (patch) | |
tree | a1a90b748831594eeb14def8eef1c13c98ca5aa6 /src/file.cpp | |
parent | Process: add get_exit_code() (diff) | |
download | winapi-common-182bff76382d59f44f0ac3c69beb55dacde75eb5.tar.gz winapi-common-182bff76382d59f44f0ac3c69beb55dacde75eb5.zip |
add path.hpp: CanonicalPath class, etc.
Apparently, relative paths in the Win32 namespace (e.g. \\?\test.txt)
are not a thing.
Diffstat (limited to 'src/file.cpp')
-rw-r--r-- | src/file.cpp | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/file.cpp b/src/file.cpp index cb9be81..a299d70 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -6,6 +6,7 @@ #include <winapi/error.hpp> #include <winapi/file.hpp> #include <winapi/handle.hpp> +#include <winapi/path.hpp> #include <winapi/utf8.hpp> #include <cstring> @@ -14,6 +15,14 @@ namespace winapi { namespace { +std::wstring to_system_path(const std::string& path) { + return widen(path); +} + +std::wstring to_system_path(const CanonicalPath& path) { + return widen(R"(\\?\)" + path.get()); +} + struct CreateFileParams { static CreateFileParams read() { CreateFileParams params; @@ -39,15 +48,13 @@ private: CreateFileParams() = default; }; -Handle open_file(const std::string& path, const CreateFileParams& params) { - const auto unicode_path = LR"(\\?\)" + widen(path); - +Handle open_file(const std::wstring& path, const CreateFileParams& params) { SECURITY_ATTRIBUTES attributes; std::memset(&attributes, 0, sizeof(attributes)); attributes.nLength = sizeof(attributes); attributes.bInheritHandle = TRUE; - const auto handle = ::CreateFileW(unicode_path.c_str(), + const auto handle = ::CreateFileW(path.c_str(), params.dwDesiredAccess, params.dwShareMode, &attributes, @@ -65,11 +72,19 @@ Handle open_file(const std::string& path, const CreateFileParams& params) { } // namespace Handle File::open_for_reading(const std::string& path) { - return open_file(path, CreateFileParams::read()); + return open_file(to_system_path(path), CreateFileParams::read()); +} + +Handle File::open_for_reading(const CanonicalPath& path) { + return open_file(to_system_path(path), CreateFileParams::read()); } Handle File::open_for_writing(const std::string& path) { - return open_file(path, CreateFileParams::write()); + return open_file(to_system_path(path), CreateFileParams::write()); +} + +Handle File::open_for_writing(const CanonicalPath& path) { + return open_file(to_system_path(path), CreateFileParams::write()); } } // namespace winapi |