diff --git a/simulator_SIC_XE/include/file_reader.h b/simulator_SIC_XE/include/file_reader.h new file mode 100644 index 0000000..2ee9fcd --- /dev/null +++ b/simulator_SIC_XE/include/file_reader.h @@ -0,0 +1,19 @@ +#pragma once +#include "reader.h" +#include +#include + +class FileReader : public Reader { +public: + explicit FileReader(const std::string &path, std::ios::openmode m = std::ios::binary); + ~FileReader() override; + + int readByte() override; + bool readBytes(uint8_t* buf, size_t len) override; + std::string readString(size_t len) override; + + bool good() const; + +private: + std::ifstream in; +}; diff --git a/simulator_SIC_XE/include/reader.h b/simulator_SIC_XE/include/reader.h new file mode 100644 index 0000000..ff64e77 --- /dev/null +++ b/simulator_SIC_XE/include/reader.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include + +// Abstract Reader class: read bytes/strings from a source (file, string, etc.) +class Reader { +public: + virtual ~Reader() = default; + // return 0..255 on success, -1 on EOF/error + virtual int readByte() = 0; + // read exactly len bytes into buf; return true on success + virtual bool readBytes(uint8_t* buf, size_t len) = 0; + // read up to len bytes into a std::string; may return shorter string on EOF + virtual std::string readString(size_t len) = 0; +}; \ No newline at end of file diff --git a/simulator_SIC_XE/include/string_reader.h b/simulator_SIC_XE/include/string_reader.h new file mode 100644 index 0000000..6b9187e --- /dev/null +++ b/simulator_SIC_XE/include/string_reader.h @@ -0,0 +1,17 @@ +#pragma once +#include "reader.h" +#include +#include + +class StringReader : public Reader { +public: + explicit StringReader(const std::string &s); + ~StringReader() override; + + int readByte() override; + bool readBytes(uint8_t* buf, size_t len) override; + std::string readString(size_t len) override; + +private: + std::istringstream in; +}; diff --git a/simulator_SIC_XE/src/file_reader.cpp b/simulator_SIC_XE/src/file_reader.cpp new file mode 100644 index 0000000..5785f06 --- /dev/null +++ b/simulator_SIC_XE/src/file_reader.cpp @@ -0,0 +1,29 @@ +#include "file_reader.h" + +FileReader::FileReader(const std::string &path, std::ios::openmode m) + : in(path, m) +{} + +FileReader::~FileReader() = default; + +int FileReader::readByte() { + char c; + if (!in.get(c)) return -1; + return static_cast(c); +} + +bool FileReader::readBytes(uint8_t* buf, size_t len) { + in.read(reinterpret_cast(buf), static_cast(len)); + return static_cast(in.gcount()) == len; +} + +std::string FileReader::readString(size_t len) { + std::string s; + s.resize(len); + in.read(reinterpret_cast(&s[0]), static_cast(len)); + std::streamsize got = in.gcount(); + if (static_cast(got) < len) s.resize(static_cast(got)); + return s; +} + +bool FileReader::good() const { return static_cast(in); } diff --git a/simulator_SIC_XE/src/string_reader.cpp b/simulator_SIC_XE/src/string_reader.cpp new file mode 100644 index 0000000..3a24cb9 --- /dev/null +++ b/simulator_SIC_XE/src/string_reader.cpp @@ -0,0 +1,27 @@ +#include "string_reader.h" + +StringReader::StringReader(const std::string &s) + : in(s) +{} + +StringReader::~StringReader() = default; + +int StringReader::readByte() { + char c; + if (!in.get(c)) return -1; + return static_cast(c); +} + +bool StringReader::readBytes(uint8_t* buf, size_t len) { + in.read(reinterpret_cast(buf), static_cast(len)); + return static_cast(in.gcount()) == len; +} + +std::string StringReader::readString(size_t len) { + std::string s; + s.resize(len); + in.read(reinterpret_cast(&s[0]), static_cast(len)); + std::streamsize got = in.gcount(); + if (static_cast(got) < len) s.resize(static_cast(got)); + return s; +}