From b5b8627b36cd3e4ae310609943bacdd90301fd40 Mon Sep 17 00:00:00 2001 From: maolipeng Date: Wed, 11 Dec 2024 03:23:40 +0800 Subject: [PATCH] wchar_t filename support for windows --- asio/include/asio/basic_file.hpp | 13 ++++++++----- asio/include/asio/basic_stream_file.hpp | 7 ++++--- .../asio/detail/impl/win_iocp_file_service.ipp | 14 ++++++++++++-- asio/include/asio/detail/win_iocp_file_service.hpp | 4 +++- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/asio/include/asio/basic_file.hpp b/asio/include/asio/basic_file.hpp index dbdb6d843d..99ce761ffe 100644 --- a/asio/include/asio/basic_file.hpp +++ b/asio/include/asio/basic_file.hpp @@ -129,8 +129,9 @@ class basic_file * @param open_flags A set of flags that determine how the file should be * opened. */ + template explicit basic_file(const executor_type& ex, - const char* path, file_base::flags open_flags) + const CharacterType* path, file_base::flags open_flags) : impl_(0, ex) { asio::error_code ec; @@ -151,9 +152,9 @@ class basic_file * @param open_flags A set of flags that determine how the file should be * opened. */ - template + template explicit basic_file(ExecutionContext& context, - const char* path, file_base::flags open_flags, + const CharacterType* path, file_base::flags open_flags, constraint_t< is_convertible::value, defaulted_constraint @@ -360,7 +361,8 @@ class basic_file * file.open("/path/to/my/file", asio::stream_file::read_only); * @endcode */ - void open(const char* path, file_base::flags open_flags) + template + void open(const CharacterType* path, file_base::flags open_flags) { asio::error_code ec; impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec); @@ -389,7 +391,8 @@ class basic_file * } * @endcode */ - ASIO_SYNC_OP_VOID open(const char* path, + template + ASIO_SYNC_OP_VOID open(const CharacterType* path, file_base::flags open_flags, asio::error_code& ec) { impl_.get_service().open(impl_.get_implementation(), path, open_flags, ec); diff --git a/asio/include/asio/basic_stream_file.hpp b/asio/include/asio/basic_stream_file.hpp index 5582e82b97..bc0a51bffa 100644 --- a/asio/include/asio/basic_stream_file.hpp +++ b/asio/include/asio/basic_stream_file.hpp @@ -130,8 +130,9 @@ class basic_stream_file * * @throws asio::system_error Thrown on failure. */ + template basic_stream_file(const executor_type& ex, - const char* path, file_base::flags open_flags) + const CharacterType* path, file_base::flags open_flags) : basic_file(ex) { asio::error_code ec; @@ -158,9 +159,9 @@ class basic_stream_file * * @throws asio::system_error Thrown on failure. */ - template + template basic_stream_file(ExecutionContext& context, - const char* path, file_base::flags open_flags, + const CharacterType* path, file_base::flags open_flags, constraint_t< is_convertible::value, defaulted_constraint diff --git a/asio/include/asio/detail/impl/win_iocp_file_service.ipp b/asio/include/asio/detail/impl/win_iocp_file_service.ipp index a698a2eda8..b40caf1975 100644 --- a/asio/include/asio/detail/impl/win_iocp_file_service.ipp +++ b/asio/include/asio/detail/impl/win_iocp_file_service.ipp @@ -48,9 +48,11 @@ void win_iocp_file_service::shutdown() handle_service_.shutdown(); } +template || std::is_same_v, bool> = true> asio::error_code win_iocp_file_service::open( win_iocp_file_service::implementation_type& impl, - const char* path, file_base::flags open_flags, + const CharacterType* path, file_base::flags open_flags, asio::error_code& ec) { if (is_open(impl)) @@ -95,7 +97,15 @@ asio::error_code win_iocp_file_service::open( flags |= FILE_FLAG_WRITE_THROUGH; impl.offset_ = 0; - HANDLE handle = ::CreateFileA(path, access, share, 0, disposition, flags, 0); + HANDLE handle = INVALID_HANDLE_VALUE; + if constexpr(std::is_same_v) + { + handle = ::CreateFileA(path, access, share, 0, disposition, flags, 0); + } + if constexpr(std::is_same_v) + { + handle = ::CreateFileW(path, access, share, 0, disposition, flags, 0); + } if (handle != INVALID_HANDLE_VALUE) { if (disposition == OPEN_ALWAYS) diff --git a/asio/include/asio/detail/win_iocp_file_service.hpp b/asio/include/asio/detail/win_iocp_file_service.hpp index f10a8d3cdf..d47a223c9d 100644 --- a/asio/include/asio/detail/win_iocp_file_service.hpp +++ b/asio/include/asio/detail/win_iocp_file_service.hpp @@ -99,8 +99,10 @@ class win_iocp_file_service : } // Open the file using the specified path name. + template || std::is_same_v, bool> = true> ASIO_DECL asio::error_code open(implementation_type& impl, - const char* path, file_base::flags open_flags, + const CharacterType* path, file_base::flags open_flags, asio::error_code& ec); // Assign a native handle to a file implementation.