summaryrefslogtreecommitdiffstats
path: root/src/osd/modules/file/winptty.cpp
diff options
context:
space:
mode:
author Vas Crabb <cuavas@users.noreply.github.com>2021-08-22 09:06:15 +1000
committer GitHub <noreply@github.com>2021-08-22 09:06:15 +1000
commite8bbea1fc6e94e14768509d322f6c624403ffb36 (patch)
tree74dd1606a900d83de8aecff17a6737af4113308d /src/osd/modules/file/winptty.cpp
parente319bde5fc3696d7f48f62b15b6366c4377fe5d1 (diff)
formats, osd, util: Started refactoring file I/O stuff. (#8456)
Added more modern generic I/O interfaces with implementation backed by stdio, osd_file and core_file, replacing io_generic. Also replaced core_file's build-in zlib compression with a filter. unzip.cpp, un7z.cpp: Added option to supply abstract I/O interface rather than filename. Converted osd_file, core_file, archive_file, chd_file and device_image_interface to use std::error_condition rather than their own error enums. Allow mounting TI-99 RPK from inside archives.
Diffstat (limited to 'src/osd/modules/file/winptty.cpp')
-rw-r--r--src/osd/modules/file/winptty.cpp44
1 files changed, 23 insertions, 21 deletions
diff --git a/src/osd/modules/file/winptty.cpp b/src/osd/modules/file/winptty.cpp
index 65650fd4bfe..7feeacce137 100644
--- a/src/osd/modules/file/winptty.cpp
+++ b/src/osd/modules/file/winptty.cpp
@@ -15,6 +15,7 @@
namespace {
+
char const *const winfile_ptty_identifier = "\\\\.\\pipe\\";
@@ -26,7 +27,7 @@ public:
win_osd_ptty& operator=(win_osd_ptty const &) = delete;
win_osd_ptty& operator=(win_osd_ptty &&) = delete;
- win_osd_ptty(HANDLE handle) : m_handle(handle)
+ win_osd_ptty(HANDLE handle) noexcept : m_handle(handle)
{
assert(m_handle);
assert(INVALID_HANDLE_VALUE != m_handle);
@@ -39,36 +40,36 @@ public:
CloseHandle(m_handle);
}
- virtual error read(void *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) override
+ virtual std::error_condition read(void *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) noexcept override
{
DWORD bytes_read;
if (!ReadFile(m_handle, buffer, count, &bytes_read, nullptr))
return win_error_to_file_error(GetLastError());
actual = bytes_read;
- return error::NONE;
+ return std::error_condition();
}
- virtual error write(void const *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) override
+ virtual std::error_condition write(void const *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) noexcept override
{
DWORD bytes_written;
if (!WriteFile(m_handle, buffer, count, &bytes_written, nullptr))
return win_error_to_file_error(GetLastError());
actual = bytes_written;
- return error::NONE;
+ return std::error_condition();
}
- virtual error truncate(std::uint64_t offset) override
+ virtual std::error_condition truncate(std::uint64_t offset) noexcept override
{
// doesn't make sense for a PTTY
- return error::INVALID_ACCESS;
+ return std::errc::bad_file_descriptor;
}
- virtual error flush() override
+ virtual std::error_condition flush() noexcept override
{
// don't want to wait for client to read all data as implied by FlushFileBuffers
- return error::NONE;
+ return std::error_condition();
}
private:
@@ -78,16 +79,18 @@ private:
} // anonymous namespace
-bool win_check_ptty_path(std::string const &path)
+bool win_check_ptty_path(std::string const &path) noexcept
{
if (strncmp(path.c_str(), winfile_ptty_identifier, strlen(winfile_ptty_identifier)) == 0) return true;
return false;
}
-osd_file::error win_open_ptty(std::string const &path, std::uint32_t openflags, osd_file::ptr &file, std::uint64_t &filesize)
+std::error_condition win_open_ptty(std::string const &path, std::uint32_t openflags, osd_file::ptr &file, std::uint64_t &filesize) noexcept
{
- osd::text::tstring t_name = osd::text::to_tstring(path);
+ osd::text::tstring t_name;
+ try { t_name = osd::text::to_tstring(path); }
+ catch (...) { return std::errc::not_enough_memory; }
HANDLE pipe = CreateFileW(t_name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
@@ -96,23 +99,22 @@ osd_file::error win_open_ptty(std::string const &path, std::uint32_t openflags,
if (openflags & OPEN_FLAG_CREATE)
pipe = CreateNamedPipe(t_name.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_NOWAIT, 1, 32, 32, 0, nullptr);
if (INVALID_HANDLE_VALUE == pipe)
- return osd_file::error::ACCESS_DENIED;
+ return win_error_to_file_error(GetLastError());
}
else
{
DWORD state = PIPE_NOWAIT;
SetNamedPipeHandleState(pipe, &state, nullptr, nullptr);
+ // TODO: check for error?
}
- try
- {
- file = std::make_unique<win_osd_ptty>(pipe);
- filesize = 0;
- return osd_file::error::NONE;
- }
- catch (...)
+ osd_file::ptr result(new (std::nothrow) win_osd_ptty(pipe));
+ if (!result)
{
CloseHandle(pipe);
- return osd_file::error::OUT_OF_MEMORY;
+ return std::errc::not_enough_memory;
}
+ file = std::move(result);
+ filesize = 0;
+ return std::error_condition();
}