added reading start

This commit is contained in:
zanostro 2025-11-17 13:07:24 +01:00
parent 5d2a0f867c
commit d438feb9ee
7 changed files with 128 additions and 4 deletions

View file

@ -1,4 +1,6 @@
#pragma once
#ifndef FILE_READER_H
#define FILE_READER_H
#include "reader.h"
#include <string>
#include <fstream>
@ -11,9 +13,12 @@ public:
int readByte() override;
bool readBytes(uint8_t* buf, size_t len) override;
std::string readString(size_t len) override;
std::string readLine() override;
bool good() const;
private:
std::ifstream in;
};
#endif // FILE_READER_H

View file

@ -0,0 +1,61 @@
#ifndef LOADER_H
#define LOADER_H
#include <memory>
#include <string>
#include <vector>
#include "file_reader.h"
class Machine;
using std::shared_ptr;
using std::string;
class Loader {
public:
Loader( shared_ptr<Machine> machine, string filename) : _machine(machine), _filename(filename) {
_file_reader = std::make_shared<FileReader>(filename, std::ios::in);
}
~Loader();
enum class RecordType {
HEADER,
TEXT,
END,
UNKNOWN
};
struct HeaderMetadata {
string program_name;
int start_address;
int length;
};
struct TextRecord {
int start_address;
std::vector<uint8_t> data;
};
struct EndRecord {
int execution_start_address;
};
void load();
private :
static RecordType parseRecordType(char c);
shared_ptr<Machine> _machine;
string _filename;
shared_ptr<FileReader> _file_reader;
HeaderMetadata readHeader();
};
#endif // LOADER_H

View file

@ -1,4 +1,6 @@
#pragma once
#ifndef READER_H
#define READER_H
#include <string>
#include <cstdint>
@ -12,4 +14,8 @@ public:
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;
};
// read a line (up to newline), return empty string on EOF
virtual std::string readLine() = 0;
};
#endif // READER_H

View file

@ -1,4 +1,6 @@
#pragma once
#ifndef STRING_READER_H
#define STRING_READER_H
#include "reader.h"
#include <string>
#include <sstream>
@ -11,7 +13,10 @@ public:
int readByte() override;
bool readBytes(uint8_t* buf, size_t len) override;
std::string readString(size_t len) override;
std::string readLine() override;
private:
std::istringstream in;
};
#endif // STRING_READER_H

View file

@ -27,3 +27,9 @@ std::string FileReader::readString(size_t len) {
}
bool FileReader::good() const { return static_cast<bool>(in); }
std::string FileReader::readLine() {
std::string s;
if (!std::getline(in, s)) return std::string();
return s;
}

View file

@ -0,0 +1,35 @@
#include "loader.h"
#include "file_reader.h"
Loader::~Loader()
{
_machine.reset();
}
void Loader::load()
{
}
Loader::RecordType Loader::parseRecordType(char c)
{
switch (c) {
case 'H': return RecordType::HEADER;
case 'T': return RecordType::TEXT;
case 'E': return RecordType::END;
default: return RecordType::UNKNOWN; // fallback; adjust as needed
}
}
Loader::HeaderMetadata Loader::readHeader()
{
RecordType type = parseRecordType(static_cast<char>(_file_reader->readByte()));
if (type != RecordType::HEADER) {
throw std::runtime_error("Expected HEADER record");
}
HeaderMetadata header;
}

View file

@ -25,3 +25,9 @@ std::string StringReader::readString(size_t len) {
if (static_cast<size_t>(got) < len) s.resize(static_cast<size_t>(got));
return s;
}
std::string StringReader::readLine() {
std::string s;
if (!std::getline(in, s)) return std::string();
return s;
}