diff --git a/deps/ox/src/ox/std/reader.cpp b/deps/ox/src/ox/std/reader.cpp index 9c1ab5c6..ac5098ed 100644 --- a/deps/ox/src/ox/std/reader.cpp +++ b/deps/ox/src/ox/std/reader.cpp @@ -8,7 +8,7 @@ #ifdef OX_USE_STDLIB -#include +#include #include "array.hpp" #include "reader.hpp" @@ -16,41 +16,56 @@ namespace ox { [[nodiscard]] -constexpr int sdMap(ox::ios_base::seekdir in) noexcept { +constexpr std::ios_base::seekdir sdMap(ox::ios_base::seekdir in) noexcept { switch (in) { case ox::ios_base::beg: - return SEEK_SET; + return std::ios_base::seekdir::beg; case ox::ios_base::end: - return SEEK_END; + return std::ios_base::seekdir::end; case ox::ios_base::cur: - return SEEK_CUR; + return std::ios_base::seekdir::cur; } - return -1; + return std::ios_base::seekdir::beg; } -ox::Result FileReader::peek() const noexcept { - auto const c = fgetc(m_file); - auto const ok = c != EOF; - if (ok && ungetc(c, m_file)) [[unlikely]] { - return OxError(1, "Unable to unget character"); +ox::Result StreamReader::peek() const noexcept { + try { + char c{}; + m_strm.get(c); + auto const ok = c != EOF; + if (ok && m_strm.unget()) [[unlikely]] { + return OxError(1, "Unable to unget character"); + } + return {static_cast(c), OxError(!ok, "File peek failed")}; + } catch (std::exception const&) { + return OxError(1, "peek failed"); } - return {static_cast(c), OxError(!ok, "File peek failed")}; } -ox::Result FileReader::read(char *v, std::size_t cnt) noexcept { - return fread(v, 1, cnt, m_file); +ox::Result StreamReader::read(char *v, std::size_t cnt) noexcept { + return static_cast(m_strm.read(v, static_cast(cnt)).gcount()); } -ox::Error FileReader::seekg(std::size_t p) noexcept { - return OxError(fseek(m_file, static_cast(p), SEEK_CUR) != 0); +ox::Error StreamReader::seekg(std::size_t p) noexcept { + try { + m_strm.seekg(static_cast(p), std::ios_base::seekdir::cur); + } catch (std::exception const&) { + return OxError(1, "seekg failed"); + } + return {}; } -ox::Error FileReader::seekg(int64_t p, ios_base::seekdir sd) noexcept { - return OxError(fseek(m_file, static_cast(p), sdMap(sd)) != 0); +ox::Error StreamReader::seekg(int64_t p, ios_base::seekdir sd) noexcept { + try { + m_strm.seekg(p, sdMap(sd)); + } catch (std::exception const&) { + return OxError(1, "seekg failed"); + } + return {}; } -ox::Result FileReader::tellg() noexcept { - const auto sz = ftell(m_file); +ox::Result StreamReader::tellg() noexcept { + const auto sz = m_strm.tellg(); return {static_cast(sz), OxError(sz == -1)}; } diff --git a/deps/ox/src/ox/std/reader.hpp b/deps/ox/src/ox/std/reader.hpp index 73e9ea41..b6339883 100644 --- a/deps/ox/src/ox/std/reader.hpp +++ b/deps/ox/src/ox/std/reader.hpp @@ -9,7 +9,7 @@ #pragma once #ifdef OX_USE_STDLIB -#include +#include #endif #include "concepts.hpp" @@ -65,11 +65,11 @@ class ReaderT: public Reader_v { }; #ifdef OX_USE_STDLIB -class FileReader: public Reader_v { +class StreamReader: public Reader_v { private: - FILE *m_file = nullptr; + std::istream &m_strm; public: - constexpr explicit FileReader(FILE *file) noexcept: m_file(file) {} + constexpr explicit StreamReader(std::istream &stream) noexcept: m_strm(stream) {} ox::Result peek() const noexcept override; ox::Result read(char *v, std::size_t cnt) noexcept override; ox::Error seekg(std::size_t p) noexcept override;