1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
*/
#include <algorithm>
#include <cstring>
#include <memory>
#include <utility>
#include <vector>
#include <Text.h>
namespace starshatter
{
namespace foundation
{
// If extension is needed, use type_traits or update to C++20 and use concepts
inline bool ok(const char* data) { return data != nullptr; }
inline bool ok(const std::unique_ptr<const char[]>& data) { return static_cast<bool>(data); }
inline const char* at(const char* data) { return data; }
inline const char* at(const std::unique_ptr<const char[]>& data) { return data.get(); }
template <typename Source>
BufferReader<Source>::BufferReader()
{
}
template <typename Source>
BufferReader<Source>::BufferReader(Source src) :
buffer {std::move(src)},
size {std::strlen(at(buffer)) + 1}
{
}
template <typename Source>
BufferReader<Source>::BufferReader(Source src, Count bytes) :
buffer {std::move(src)},
size {bytes}
{
}
template <typename Source>
bool
BufferReader<Source>::valid() const
{
return ok(buffer);
}
template <typename Source>
Count
BufferReader<Source>::available() const
{
return size - position;
}
template <typename Source>
Count
BufferReader<Source>::seek(Count pos)
{
position = pos;
if (position > size)
position = size;
return position;
}
template <typename Source>
Count
BufferReader<Source>::seek(Offset offset, Direction dir)
{
switch (dir) {
case Direction::Start:
break; // no-op
case Direction::End:
offset = size + offset;
break;
case Direction::Current:
offset = position + offset;
break;
}
position = static_cast<Count>(std::clamp<Offset>(offset, 0, size));
return position;
}
template <typename Source>
Count
BufferReader<Source>::read(char* dest)
{
return read(dest, available());
}
template <typename Source>
Count
BufferReader<Source>::read(char* dest, Count bytes)
{
bytes = peek(dest, bytes);
position += bytes;
return bytes;
}
template <typename Source>
Count
BufferReader<Source>::peek(char* dest) const
{
return peek(dest, available());
}
template <typename Source>
Count
BufferReader<Source>::peek(char* dest, Count bytes) const
{
bytes = std::min(bytes, available());
std::copy(at(buffer) + position, at(buffer) + position + bytes, dest);
return bytes;
}
template <typename Source>
Text
BufferReader<Source>::more()
{
const auto size = available();
if (size < 1)
return Text();
std::vector<char> tmp(size);
read(tmp.data(), size);
return Text(tmp.data(), size);
}
} // namespace foundation
} // namespace starshatter
|