5#ifndef AUX_STREAM_DEVICE_H
6#define AUX_STREAM_DEVICE_H
16#include "InputDevice.h"
17#include "OutputDevice.h"
37class PODOFO_API StandardStreamDevice :
public StreamDevice
44 StandardStreamDevice(std::ostream&
stream);
46 StandardStreamDevice(std::istream&
stream);
52 StandardStreamDevice(std::iostream&
stream);
54 ~StandardStreamDevice();
57 size_t GetLength()
const override;
59 size_t GetPosition()
const override;
61 bool CanSeek()
const override;
63 bool Eof()
const override;
67 void writeBuffer(
const char*
buffer,
size_t size)
override;
68 void flush()
override;
69 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
70 bool readChar(
char&
ch)
override;
71 bool peek(
char&
ch)
const override;
73 void truncate()
override;
75 inline std::ios& GetStream() {
return *m_Stream; }
78 StandardStreamDevice(DeviceAccess access, std::ios* stream, std::istream* istream, std::ostream* ostream,
bool streamOwned);
82 std::istream* m_istream;
83 std::ostream* m_ostream;
99class PODOFO_API FileStreamDevice :
public StreamDevice
104 FileStreamDevice(
const std::string_view & filepath);
108 FileStreamDevice(
const std::string_view & filepath, FileMode mode);
112 FileStreamDevice(
const std::string_view& filepath, FileMode mode,
113 DeviceAccess access);
118 const std::string& GetFilepath()
const {
return m_Filepath; }
120 size_t GetLength()
const override;
122 size_t GetPosition()
const override;
124 bool CanSeek()
const override;
126 bool Eof()
const override;
129 void writeBuffer(
const char* buffer,
size_t size)
override;
130 void flush()
override;
131 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
132 bool readChar(
char& ch)
override;
133 bool peek(
char& ch)
const override;
134 void seek(ssize_t offset, SeekDirection direction)
override;
135 void close()
override;
136 void truncate()
override;
140 std::string m_Filepath;
143template <
typename TContainer>
144class 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);
223 void truncate()
override
225 m_container->resize(m_Position);
229 TContainer* m_container;
233class PODOFO_API SpanStreamDevice :
public StreamDevice
239 SpanStreamDevice(
const char* buffer,
size_t size);
240 SpanStreamDevice(
const bufferview& buffer);
241 SpanStreamDevice(
const std::string_view& view);
242 SpanStreamDevice(
const std::string& str);
243 SpanStreamDevice(std::string& str,
244 DeviceAccess access = DeviceAccess::ReadWrite);
245 SpanStreamDevice(
const char* str);
246 SpanStreamDevice(
char* buffer,
size_t size,
247 DeviceAccess access = DeviceAccess::ReadWrite);
248 SpanStreamDevice(
const bufferspan& span,
249 DeviceAccess access = DeviceAccess::ReadWrite);
252 size_t GetLength()
const override;
254 size_t GetPosition()
const override;
256 bool Eof()
const override;
258 bool CanSeek()
const override;
261 void writeBuffer(
const char* buffer,
size_t size)
override;
262 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
263 bool readChar(
char& ch)
override;
264 bool peek(
char& ch)
const override;
265 void seek(ssize_t offset, SeekDirection direction)
override;
266 void truncate()
override;
269 SpanStreamDevice(std::nullptr_t) =
delete;
286 size_t GetLength()
const override;
288 size_t GetPosition()
const override;
290 bool Eof()
const override;
293 void writeBuffer(
const char*
buffer,
size_t size)
override;
294 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
295 bool readChar(
char&
ch)
override;
296 bool peek(
char&
ch)
const override;
298 void truncate()
override;
An StreamDevice device that does nothing.
Definition StreamDevice.h:281
This class provides an output device which operates either on a file or on a buffer in memory.
Definition StreamDevice.h:29
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
FileMode
Definition StreamDevice.h:90
@ 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.