7#ifndef AUX_STREAM_DEVICE_H
8#define AUX_STREAM_DEVICE_H
18#include "InputDevice.h"
19#include "OutputDevice.h"
39class 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;
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;
75 void truncate()
override;
77 inline std::ios& GetStream() {
return *m_Stream; }
80 StandardStreamDevice(DeviceAccess access, std::ios* stream, std::istream* istream, std::ostream* ostream,
bool streamOwned);
84 std::istream* m_istream;
85 std::ostream* m_ostream;
101class PODOFO_API FileStreamDevice :
public StreamDevice
106 FileStreamDevice(
const std::string_view & filepath);
110 FileStreamDevice(
const std::string_view & filepath, FileMode mode);
114 FileStreamDevice(
const std::string_view& filepath, FileMode mode,
115 DeviceAccess access);
120 const std::string& GetFilepath()
const {
return m_Filepath; }
122 size_t GetLength()
const override;
124 size_t GetPosition()
const override;
126 bool CanSeek()
const override;
128 bool Eof()
const override;
131 void writeBuffer(
const char* buffer,
size_t size)
override;
132 void flush()
override;
133 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
134 bool readChar(
char& ch)
override;
135 bool peek(
char& ch)
const override;
136 void seek(ssize_t offset, SeekDirection direction)
override;
137 void close()
override;
138 void truncate()
override;
142 std::string m_Filepath;
145template <
typename TContainer>
146class ContainerStreamDevice :
public StreamDevice
149 ContainerStreamDevice(TContainer& container,
150 DeviceAccess access,
bool ate) :
151 StreamDevice(access),
152 m_container(&container),
153 m_Position(ate ? container.size() : 0) { }
158 ContainerStreamDevice(
const TContainer& container) :
159 ContainerStreamDevice(const_cast<TContainer&>(container), DeviceAccess::
Read, false) { }
164 ContainerStreamDevice(TContainer& container) :
165 ContainerStreamDevice(container, DeviceAccess::ReadWrite, true) { }
168 size_t GetLength()
const override {
return m_container->size(); }
170 size_t GetPosition()
const override {
return m_Position; }
172 bool CanSeek()
const override {
return true; }
174 bool Eof()
const override {
return m_Position == m_container->size(); }
177 void writeBuffer(
const char* buffer,
size_t size)
override
179 if (m_Position + size > m_container->size())
180 m_container->resize(m_Position + size);
182 std::memcpy(m_container->data() + m_Position, buffer, size);
186 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override
188 size_t readCount = std::min(size, m_container->size() - m_Position);
189 std::memcpy(buffer, m_container->data() + m_Position, readCount);
190 m_Position += readCount;
191 eof = m_Position == m_container->size();
195 bool readChar(
char& ch)
override
197 if (m_Position == m_container->size())
203 ch = m_container->data()[m_Position];
208 bool peek(
char& ch)
const override
210 if (m_Position == m_container->size())
216 ch = m_container->data()[m_Position];
220 void seek(ssize_t offset, SeekDirection direction)
override
222 m_Position = SeekPosition(m_Position, m_container->size(), offset, direction);
225 void truncate()
override
227 m_container->resize(m_Position);
231 TContainer* m_container;
235class PODOFO_API SpanStreamDevice :
public StreamDevice
241 SpanStreamDevice(
const char* buffer,
size_t size);
242 SpanStreamDevice(
const bufferview& buffer);
243 SpanStreamDevice(
const std::string_view& view);
244 SpanStreamDevice(
const std::string& str);
245 SpanStreamDevice(std::string& str,
246 DeviceAccess access = DeviceAccess::ReadWrite);
247 SpanStreamDevice(
const char* str);
248 SpanStreamDevice(
char* buffer,
size_t size,
249 DeviceAccess access = DeviceAccess::ReadWrite);
250 SpanStreamDevice(
const bufferspan& span,
251 DeviceAccess access = DeviceAccess::ReadWrite);
254 size_t GetLength()
const override;
256 size_t GetPosition()
const override;
258 bool Eof()
const override;
260 bool CanSeek()
const override;
263 void writeBuffer(
const char* buffer,
size_t size)
override;
264 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
265 bool readChar(
char& ch)
override;
266 bool peek(
char& ch)
const override;
267 void seek(ssize_t offset, SeekDirection direction)
override;
268 void truncate()
override;
271 SpanStreamDevice(std::nullptr_t) =
delete;
288 size_t GetLength()
const override;
290 size_t GetPosition()
const override;
292 bool Eof()
const override;
295 void writeBuffer(
const char*
buffer,
size_t size)
override;
296 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
297 bool readChar(
char&
ch)
override;
298 bool peek(
char&
ch)
const override;
300 void truncate()
override;
An StreamDevice device that does nothing.
Definition StreamDevice.h:283
This class provides an output device which operates either on a file or on a buffer in memory.
Definition StreamDevice.h:31
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
FileMode
Definition StreamDevice.h:92
@ 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.