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