RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
reader.h
1#ifndef READER_H
2#define READER_H
3
4#include <cstring>
5#include <iostream>
6#include <string>
7#include <vector>
8#include "../common.h"
9
10namespace Linker
11{
15 class Reader
16 {
17 public:
21 EndianType endiantype;
25 std::istream * in;
26
27 const offset_t start_offset;
28 const offset_t maximum_size;
29
30 Reader(EndianType endiantype, std::istream * in = nullptr)
31 : endiantype(endiantype), in(in), start_offset(0), maximum_size(offset_t(-1))
32 {
33 }
34
35 Reader(offset_t start_offset, offset_t maximum_size, EndianType endiantype, std::istream * in = nullptr)
36 : endiantype(endiantype), in(in), start_offset(start_offset), maximum_size(maximum_size)
37 {
38 }
39
40 Reader CreateWindow(offset_t new_start_offset, offset_t new_maximum_size = offset_t(-1));
41
45 void ReadData(size_t count, void * data);
46
50 void ReadData(size_t count, std::vector<uint8_t>& data, size_t offset = 0);
51
55 void ReadData(std::vector<uint8_t>& data, size_t offset = 0);
56
60 std::string ReadData(size_t count, bool terminate_at_null = false);
61
65 template <class T, std::size_t N>
66 void ReadData(std::array<T, N>& data, size_t offset = 0)
67 {
68 if(offset >= N)
69 return;
70 ReadData(N - offset, reinterpret_cast<char *>(data.data()) + offset);
71 }
72
76 std::string ReadASCII(char terminator, size_t maximum = size_t(-1));
77
81 std::string ReadASCIIZ(size_t maximum = size_t(-1));
82
86 uint64_t ReadUnsigned(size_t bytes, EndianType endiantype);
87
91 uint64_t ReadUnsigned(size_t bytes);
92
96 uint64_t ReadSigned(size_t bytes, EndianType endiantype);
97
101 uint64_t ReadSigned(size_t bytes);
102
106 void Seek(offset_t offset);
107
111 void Skip(offset_t offset);
112
116 void SeekEnd(relative_offset_t offset = 0);
117
121 offset_t Tell();
122 };
123}
124
125#endif /* READER_H */
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
void Skip(offset_t offset)
Jump to a distance in the input stream.
Definition reader.cc:120
std::string ReadASCIIZ(size_t maximum=size_t(-1))
Read a zero terminated ASCII string.
Definition reader.cc:81
std::string ReadASCII(char terminator, size_t maximum=size_t(-1))
Read an ASCII string up to a terminator.
Definition reader.cc:70
void Seek(offset_t offset)
Jump to a specific location in the input stream.
Definition reader.cc:110
uint64_t ReadSigned(size_t bytes, EndianType endiantype)
Read a signed word.
Definition reader.cc:98
void SeekEnd(relative_offset_t offset=0)
Jump to end of the input stream.
Definition reader.cc:142
EndianType endiantype
The default endianness of the binary format, used for reading multibyte numeric data.
Definition reader.h:21
std::istream * in
The input stream.
Definition reader.h:25
void ReadData(size_t count, void *data)
Read in a sequence of bytes.
Definition reader.cc:30
void ReadData(std::array< T, N > &data, size_t offset=0)
Read in a sequence of bytes.
Definition reader.h:66
offset_t Tell()
Retrieve the current location.
Definition reader.cc:164
uint64_t ReadUnsigned(size_t bytes, EndianType endiantype)
Read an unsigned word.
Definition reader.cc:86