diff --git a/src/io/fd_istream.hpp b/src/io/fd_istream.hpp new file mode 100644 index 000000000..9c0223aea --- /dev/null +++ b/src/io/fd_istream.hpp @@ -0,0 +1,131 @@ +#pragma once + +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +#ifndef _WIN32 +#include +#endif + +class fd_istreambuf final : public std::streambuf { + static constexpr size_t BUFFER_SIZE = 4096; +public: +#ifdef _WIN32 + explicit fd_istreambuf(HANDLE handle) + : handle(handle) { + setg(buffer.data(), buffer.data(), buffer.data()); + } + + ~fd_istreambuf() override { + if (handle != nullptr && handle != INVALID_HANDLE_VALUE) { + CloseHandle(handle); + handle = INVALID_HANDLE_VALUE; + } + } + +protected: + int_type underflow() override { + if (handle == nullptr || handle == INVALID_HANDLE_VALUE) { + return traits_type::eof(); + } + if (gptr() != egptr()) { + return traits_type::to_int_type(*gptr()); + } + + std::array chunk {}; + DWORD readCount = 0; + if (!ReadFile( + handle, + chunk.data(), + static_cast(chunk.size()), + &readCount, + nullptr + ) || + readCount == 0) { + return traits_type::eof(); + } + + buffer.assign(chunk.begin(), chunk.begin() + readCount); + setg(buffer.data(), buffer.data(), buffer.data() + buffer.size()); + return traits_type::to_int_type(*gptr()); + } + + pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) + override { + return pos_type(off_type(-1)); + } + + pos_type seekpos(pos_type, std::ios_base::openmode) override { + return pos_type(off_type(-1)); + } +private: + HANDLE handle; + std::vector buffer; +#else + explicit fd_istreambuf(int fd) + : fd(fd) { + setg(buffer.data(), buffer.data(), buffer.data()); + } + + ~fd_istreambuf() override { + if (fd >= 0) { + ::close(fd); + fd = -1; + } + } + +protected: + int_type underflow() override { + if (fd < 0) { + return traits_type::eof(); + } + if (gptr() != egptr()) { + return traits_type::to_int_type(*gptr()); + } + + buffer.resize(BUFFER_SIZE); + const auto count = ::read(fd, buffer.data(), buffer.size()); + if (count <= 0) { + return traits_type::eof(); + } + buffer.resize(static_cast(count)); + setg(buffer.data(), buffer.data(), buffer.data() + buffer.size()); + return traits_type::to_int_type(*gptr()); + } + + pos_type seekoff( + off_type, + std::ios_base::seekdir, + std::ios_base::openmode + ) override { + return pos_type(off_type(-1)); + } + + pos_type seekpos(pos_type, std::ios_base::openmode) override { + return pos_type(off_type(-1)); + } + +private: + int fd; + std::vector buffer; +#endif +}; + +class fd_istream final : public std::istream { +public: +#ifdef _WIN32 + explicit fd_istream(HANDLE handle) + : std::istream(&buf), buf(handle) {} +#else + explicit fd_istream(int fd) + : std::istream(&buf), buf(fd) {} +#endif + +private: + fd_istreambuf buf; +}; diff --git a/src/io/fd_ostream.hpp b/src/io/fd_ostream.hpp new file mode 100644 index 000000000..813eb060f --- /dev/null +++ b/src/io/fd_ostream.hpp @@ -0,0 +1,155 @@ +#pragma once + +#include +#include +#include + +#ifdef _WIN32 +#include +#endif + +#ifndef _WIN32 +#include +#endif + + +class fd_ostreambuf final : public std::streambuf { + static constexpr size_t BUFFER_SIZE = 4096; +public: +#ifdef _WIN32 + explicit fd_ostreambuf(HANDLE handle) : handle(handle) { + setp(buffer.data(), buffer.data() + buffer.size()); + } + + ~fd_ostreambuf() override { + fd_ostreambuf::sync(); + if (handle != nullptr && handle != INVALID_HANDLE_VALUE) { + CloseHandle(handle); + handle = INVALID_HANDLE_VALUE; + } + } +protected: + int_type overflow(int_type ch) override { + if (ch == traits_type::eof()) { + return traits_type::not_eof(ch); + } + if (pptr() == epptr()) { + if (sync() != 0) { + return traits_type::eof(); + } + } + *pptr() = traits_type::to_char_type(ch); + pbump(1); + return ch; + } + + int sync() override { + if (handle == nullptr || handle == INVALID_HANDLE_VALUE) { + return -1; + } + + const auto size = static_cast(pptr() - pbase()); + if (size == 0) { + return 0; + } + + DWORD written = 0; + if (!WriteFile( + handle, pbase(), static_cast(size), &written, nullptr + )) { + return -1; + } + + setp(buffer.data(), buffer.data() + buffer.size()); + return 0; + } + + pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) + override { + return pos_type(off_type(-1)); + } + + pos_type seekpos(pos_type, std::ios_base::openmode) override { + return pos_type(off_type(-1)); + } +private: + HANDLE handle; + std::array buffer {}; +#else + explicit fd_ostreambuf(int fd) : fd(fd) { + setp(buffer.data(), buffer.data() + buffer.size()); + } + + ~fd_ostreambuf() override { + fd_ostreambuf::sync(); + if (fd >= 0) { + ::close(fd); + fd = -1; + } + } +protected: + int_type overflow(int_type ch) override { + if (ch == traits_type::eof()) { + return traits_type::not_eof(ch); + } + if (pptr() == epptr()) { + if (sync() != 0) { + return traits_type::eof(); + } + } + *pptr() = traits_type::to_char_type(ch); + pbump(1); + return ch; + } + + int sync() override { + if (fd < 0) { + return -1; + } + + const auto size = static_cast(pptr() - pbase()); + if (size == 0) { + return 0; + } + + char* ptr = pbase(); + size_t remaining = size; + while (remaining > 0) { + const auto written = ::write(fd, ptr, remaining); + if (written <= 0) { + return -1; + } + ptr += written; + remaining -= static_cast(written); + } + + setp(buffer.data(), buffer.data() + buffer.size()); + return 0; + } + + pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) + override { + return pos_type(off_type(-1)); + } + + pos_type seekpos(pos_type, std::ios_base::openmode) override { + return pos_type(off_type(-1)); + } +private: + int fd; + std::array buffer {}; +#endif +}; + +class fd_ostream final : public std::ostream { +public: +#ifdef _WIN32 + explicit fd_ostream(HANDLE handle) : std::ostream(&buf), buf(handle) { + } +#else + explicit fd_ostream(int fd) : std::ostream(&buf), buf(fd) { + } +#endif +private: + fd_ostreambuf buf; +};