implemented rfile reading
This commit is contained in:
parent
ba18b92116
commit
5d2a0f867c
5 changed files with 107 additions and 0 deletions
19
simulator_SIC_XE/include/file_reader.h
Normal file
19
simulator_SIC_XE/include/file_reader.h
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#pragma once
|
||||||
|
#include "reader.h"
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
15
simulator_SIC_XE/include/reader.h
Normal file
15
simulator_SIC_XE/include/reader.h
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
};
|
||||||
17
simulator_SIC_XE/include/string_reader.h
Normal file
17
simulator_SIC_XE/include/string_reader.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
#include "reader.h"
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
29
simulator_SIC_XE/src/file_reader.cpp
Normal file
29
simulator_SIC_XE/src/file_reader.cpp
Normal file
|
|
@ -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<unsigned char>(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileReader::readBytes(uint8_t* buf, size_t len) {
|
||||||
|
in.read(reinterpret_cast<char*>(buf), static_cast<std::streamsize>(len));
|
||||||
|
return static_cast<size_t>(in.gcount()) == len;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string FileReader::readString(size_t len) {
|
||||||
|
std::string s;
|
||||||
|
s.resize(len);
|
||||||
|
in.read(reinterpret_cast<char*>(&s[0]), static_cast<std::streamsize>(len));
|
||||||
|
std::streamsize got = in.gcount();
|
||||||
|
if (static_cast<size_t>(got) < len) s.resize(static_cast<size_t>(got));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileReader::good() const { return static_cast<bool>(in); }
|
||||||
27
simulator_SIC_XE/src/string_reader.cpp
Normal file
27
simulator_SIC_XE/src/string_reader.cpp
Normal file
|
|
@ -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<unsigned char>(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StringReader::readBytes(uint8_t* buf, size_t len) {
|
||||||
|
in.read(reinterpret_cast<char*>(buf), static_cast<std::streamsize>(len));
|
||||||
|
return static_cast<size_t>(in.gcount()) == len;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string StringReader::readString(size_t len) {
|
||||||
|
std::string s;
|
||||||
|
s.resize(len);
|
||||||
|
in.read(reinterpret_cast<char*>(&s[0]), static_cast<std::streamsize>(len));
|
||||||
|
std::streamsize got = in.gcount();
|
||||||
|
if (static_cast<size_t>(got) < len) s.resize(static_cast<size_t>(got));
|
||||||
|
return s;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue