7 #ifndef AUX_STREAM_DEVICE_H
8 #define AUX_STREAM_DEVICE_H
16 #include "basetypes.h"
18 #include "InputDevice.h"
19 #include "OutputDevice.h"
36 static size_t SeekPosition(
size_t curpos,
size_t devlen, ssize_t offset, SeekDirection direction);
39 class PODOFO_API StandardStreamDevice :
public StreamDevice
46 StandardStreamDevice(std::ostream& stream);
48 StandardStreamDevice(std::istream& stream);
54 StandardStreamDevice(std::iostream& stream);
56 ~StandardStreamDevice();
59 size_t GetLength()
const override;
61 size_t GetPosition()
const override;
63 bool CanSeek()
const override;
65 bool Eof()
const override;
68 StandardStreamDevice(DeviceAccess access, std::ios& stream,
bool streamOwned);
69 void writeBuffer(
const char* buffer,
size_t size)
override;
70 void flush()
override;
71 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
72 bool readChar(
char& ch)
override;
73 bool peek(
char& ch)
const override;
74 void seek(ssize_t offset, SeekDirection direction)
override;
76 inline std::ios& GetStream() {
return *m_Stream; }
79 StandardStreamDevice(DeviceAccess access, std::ios* stream, std::istream* istream, std::ostream* ostream,
bool streamOwned);
83 std::istream* m_istream;
84 std::ostream* m_ostream;
100 class PODOFO_API FileStreamDevice :
public StreamDevice
105 FileStreamDevice(
const std::string_view & filepath);
109 FileStreamDevice(
const std::string_view & filepath,
FileMode mode);
113 FileStreamDevice(
const std::string_view& filepath,
FileMode mode,
114 DeviceAccess access);
119 const std::string& GetFilepath()
const {
return m_Filepath; }
121 size_t GetLength()
const override;
123 size_t GetPosition()
const override;
125 bool CanSeek()
const override;
127 bool Eof()
const override;
130 void writeBuffer(
const char* buffer,
size_t size)
override;
131 void flush()
override;
132 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
133 bool readChar(
char& ch)
override;
134 bool peek(
char& ch)
const override;
135 void seek(ssize_t offset, SeekDirection direction)
override;
136 void close()
override;
140 std::string m_Filepath;
143 template <
typename TContainer>
144 class ContainerStreamDevice :
public StreamDevice
147 ContainerStreamDevice(TContainer& container,
148 DeviceAccess access,
bool ate) :
149 StreamDevice(access),
150 m_container(&container),
151 m_Position(ate ? container.size() : 0) { }
156 ContainerStreamDevice(
const TContainer& container) :
157 ContainerStreamDevice(const_cast<TContainer&>(container), DeviceAccess::
Read, false) { }
162 ContainerStreamDevice(TContainer& container) :
163 ContainerStreamDevice(container, DeviceAccess::ReadWrite, true) { }
166 size_t GetLength()
const override {
return m_container->size(); }
168 size_t GetPosition()
const override {
return m_Position; }
170 bool CanSeek()
const override {
return true; }
172 bool Eof()
const override {
return m_Position == m_container->size(); }
175 void writeBuffer(
const char* buffer,
size_t size)
override
177 if (m_Position + size > m_container->size())
178 m_container->resize(m_Position + size);
180 std::memcpy(m_container->data() + m_Position, buffer, size);
184 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override
186 size_t readCount = std::min(size, m_container->size() - m_Position);
187 std::memcpy(buffer, m_container->data() + m_Position, readCount);
188 m_Position += readCount;
189 eof = m_Position == m_container->size();
193 bool readChar(
char& ch)
override
195 if (m_Position == m_container->size())
201 ch = m_container->data()[m_Position];
206 bool peek(
char& ch)
const override
208 if (m_Position == m_container->size())
214 ch = m_container->data()[m_Position];
218 void seek(ssize_t offset, SeekDirection direction)
override
220 m_Position = SeekPosition(m_Position, m_container->size(), offset, direction);
224 TContainer* m_container;
228 class PODOFO_API SpanStreamDevice :
public StreamDevice
234 SpanStreamDevice(
const char* buffer,
size_t size);
236 SpanStreamDevice(
const std::string_view& view);
237 SpanStreamDevice(
const std::string& str);
238 SpanStreamDevice(std::string& str,
239 DeviceAccess access = DeviceAccess::ReadWrite);
240 SpanStreamDevice(
const char* str);
241 SpanStreamDevice(
char* buffer,
size_t size,
242 DeviceAccess access = DeviceAccess::ReadWrite);
244 DeviceAccess access = DeviceAccess::ReadWrite);
247 size_t GetLength()
const override;
249 size_t GetPosition()
const override;
251 bool Eof()
const override;
253 bool CanSeek()
const override;
256 void writeBuffer(
const char* buffer,
size_t size)
override;
257 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
258 bool readChar(
char& ch)
override;
259 bool peek(
char& ch)
const override;
260 void seek(ssize_t offset, SeekDirection direction)
override;
263 SpanStreamDevice(std::nullptr_t) =
delete;
280 size_t GetLength()
const override;
282 size_t GetPosition()
const override;
284 bool Eof()
const override;
287 void writeBuffer(
const char* buffer,
size_t size)
override;
288 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
289 bool readChar(
char& ch)
override;
290 bool peek(
char& ch)
const override;
291 void seek(ssize_t offset, SeekDirection direction)
override;
298 using VectorStreamDevice = ContainerStreamDevice<std::vector<char>>;
299 using StringStreamDevice = ContainerStreamDevice<std::string>;
300 using BufferStreamDevice = ContainerStreamDevice<charbuff>;
An StreamDevice device that does nothing.
Definition: StreamDevice.h:275
This class provides an output device which operates either on a file or on a buffer in memory.
Definition: StreamDevice.h:31
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition: basetypes.h:16
mspan< char > bufferspan
Convenient writable char buffer span.
Definition: basetypes.h:23
FileMode
Definition: StreamDevice.h:91
@ CreateNew
Create a new file (throw if existing) for writing/reading.
@ OpenOrCreate
Open an existing file or create a new one for writing/reading.
@ Append
Open an existing file and seek to the end for writing.
@ Create
Create a new file or truncate existing one for writing/reading.
@ Truncate
Truncate an existing file for writing/reading.
@ Open
Open an existing file for reading and/or writing.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition: basetypes.h:19