forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem_stream.h
174 lines (148 loc) · 4.87 KB
/
filesystem_stream.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EP_FILESYSTEM_STREAM_H
#define EP_FILESYSTEM_STREAM_H
// Headers
#include <cassert>
#include <istream>
#include <ostream>
#include "filesystem.h"
#include "utils.h"
namespace Filesystem_Stream {
class InputStream final : public std::istream {
public:
explicit InputStream(): std::istream(nullptr) {}
explicit InputStream(std::streambuf* sb, std::string name);
~InputStream() override;
InputStream(const InputStream&) = delete;
InputStream& operator=(const InputStream&) = delete;
InputStream(InputStream&& is) noexcept;
InputStream& operator=(InputStream&& is) noexcept;
StringView GetName() const;
std::streampos GetSize() const;
void Close();
template <typename T>
bool ReadIntoObj(T& obj);
private:
template <typename T>
bool Read0(T& obj);
std::string name;
mutable bool size_cached = false;
mutable std::streampos size = 0;
};
class OutputStream final : public std::ostream {
public:
explicit OutputStream(): std::ostream(nullptr) {}
explicit OutputStream(std::streambuf* sb, FilesystemView fs, std::string name);
~OutputStream() override;
OutputStream(const OutputStream&) = delete;
OutputStream& operator=(const OutputStream&) = delete;
OutputStream(OutputStream&& os) noexcept;
OutputStream& operator=(OutputStream&& os) noexcept;
StringView GetName() const;
void Close();
private:
FilesystemView fs;
std::string name;
};
/** Streambuf interface for an in-memory buffer. Does not take ownership of the buffer. */
class InputMemoryStreamBufView : public std::streambuf {
public:
explicit InputMemoryStreamBufView(Span<uint8_t> buffer_view);
InputMemoryStreamBufView(InputMemoryStreamBufView const& other) = delete;
InputMemoryStreamBufView const& operator=(InputMemoryStreamBufView const& other) = delete;
protected:
std::streambuf::pos_type seekoff(std::streambuf::off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode mode) override;
std::streambuf::pos_type seekpos(std::streambuf::pos_type pos, std::ios_base::openmode mode) override;
private:
Span<uint8_t> buffer_view;
};
/** Streambuf interface for an in-memory buffer. Takes ownership of the buffer. */
class InputMemoryStreamBuf : public InputMemoryStreamBufView {
public:
explicit InputMemoryStreamBuf(std::vector<uint8_t> buffer);
InputMemoryStreamBuf(InputMemoryStreamBuf const& other) = delete;
InputMemoryStreamBuf const& operator=(InputMemoryStreamBuf const& other) = delete;
private:
std::vector<uint8_t> buffer;
};
static constexpr std::ios_base::seekdir CSeekdirToCppSeekdir(int origin);
static constexpr int CppSeekdirToCSeekdir(std::ios_base::seekdir origin);
};
template<typename T>
inline bool Filesystem_Stream::InputStream::Read0(T& obj) {
return read(reinterpret_cast<char*>(&obj), sizeof(obj)).gcount() == sizeof(obj);
}
template<typename T>
inline bool Filesystem_Stream::InputStream::ReadIntoObj(T& obj) {
return Read0(obj);
}
template <>
inline bool Filesystem_Stream::InputStream::ReadIntoObj(int16_t& obj) {
bool success = Read0(obj);
uint16_t uobj = obj;
Utils::SwapByteOrder(uobj);
obj = uobj;
return success;
}
template <>
inline bool Filesystem_Stream::InputStream::ReadIntoObj(uint16_t& obj) {
bool success = Read0(obj);
Utils::SwapByteOrder(obj);
return success;
}
template <>
inline bool Filesystem_Stream::InputStream::ReadIntoObj(int32_t& obj) {
bool success = Read0(obj);
uint32_t uobj = obj;
Utils::SwapByteOrder(uobj);
obj = uobj;
return success;
}
template <>
inline bool Filesystem_Stream::InputStream::ReadIntoObj(uint32_t& obj) {
bool success = Read0(obj);
Utils::SwapByteOrder(obj);
return success;
}
constexpr std::ios_base::seekdir Filesystem_Stream::CSeekdirToCppSeekdir(int origin) {
switch (origin) {
case SEEK_SET:
return std::ios_base::beg;
case SEEK_CUR:
return std::ios_base::cur;
case SEEK_END:
return std::ios_base::end;
default:
assert(false);
return std::ios_base::beg;
}
}
constexpr int Filesystem_Stream::CppSeekdirToCSeekdir(std::ios_base::seekdir origin) {
switch (origin) {
case std::ios_base::beg:
return SEEK_SET;
case std::ios_base::cur:
return SEEK_CUR;
case std::ios_base::end:
return SEEK_END;
default:
assert(false);
return SEEK_SET;
}
}
#endif