5#ifndef AUX_STREAM_DEVICE_H
6#define AUX_STREAM_DEVICE_H
16#include "InputDevice.h"
17#include "OutputDevice.h"
36class PODOFO_API StandardStreamDevice :
public StreamDevice
42 StandardStreamDevice(std::ostream&
stream);
44 StandardStreamDevice(std::istream&
stream);
49 StandardStreamDevice(std::iostream&
stream);
51 ~StandardStreamDevice();
54 size_t GetLength()
const override;
56 size_t GetPosition()
const override;
58 bool CanSeek()
const override;
60 bool Eof()
const override;
64 void writeBuffer(
const char*
buffer,
size_t size)
override;
65 void flush()
override;
66 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
67 bool readChar(
char&
ch)
override;
68 bool peek(
char&
ch)
const override;
70 void truncate()
override;
72 inline std::ios& GetStream() {
return *m_Stream; }
75 StandardStreamDevice(DeviceAccess access, std::ios* stream, std::istream* istream, std::ostream* ostream,
bool streamOwned);
79 std::istream* m_istream;
80 std::ostream* m_ostream;
96class PODOFO_API FileStreamDevice :
public StreamDevice
100 FileStreamDevice(
const std::string_view & filepath);
103 FileStreamDevice(
const std::string_view & filepath, FileMode mode);
106 FileStreamDevice(
const std::string_view& filepath, FileMode mode,
107 DeviceAccess access);
112 const std::string& GetFilepath()
const {
return m_Filepath; }
114 size_t GetLength()
const override;
116 size_t GetPosition()
const override;
118 bool CanSeek()
const override;
120 bool Eof()
const override;
123 void writeBuffer(
const char* buffer,
size_t size)
override;
124 void flush()
override;
125 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
126 bool readChar(
char& ch)
override;
127 bool peek(
char& ch)
const override;
128 void seek(ssize_t offset, SeekDirection direction)
override;
129 void close()
override;
130 void truncate()
override;
134 std::string m_Filepath;
137template <
typename TContainer>
138class ContainerStreamDevice :
public StreamDevice
141 ContainerStreamDevice(TContainer& container,
142 DeviceAccess access,
bool ate) :
143 StreamDevice(access),
144 m_container(&container),
145 m_Position(ate ? container.size() : 0) { }
148 ContainerStreamDevice(
const TContainer& container) :
149 ContainerStreamDevice(const_cast<TContainer&>(container), DeviceAccess::
Read, false) { }
152 ContainerStreamDevice(TContainer& container) :
153 ContainerStreamDevice(container, DeviceAccess::ReadWrite, true) { }
156 size_t GetLength()
const override {
return m_container->size(); }
158 size_t GetPosition()
const override {
return m_Position; }
160 bool CanSeek()
const override {
return true; }
162 bool Eof()
const override {
return m_Position == m_container->size(); }
165 void writeBuffer(
const char* buffer,
size_t size)
override
167 if (m_Position + size > m_container->size())
168 m_container->resize(m_Position + size);
170 std::memcpy(m_container->data() + m_Position, buffer, size);
174 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override
176 size_t readCount = std::min(size, m_container->size() - m_Position);
177 std::memcpy(buffer, m_container->data() + m_Position, readCount);
178 m_Position += readCount;
179 eof = m_Position == m_container->size();
183 bool readChar(
char& ch)
override
185 if (m_Position == m_container->size())
191 ch = m_container->data()[m_Position];
196 bool peek(
char& ch)
const override
198 if (m_Position == m_container->size())
204 ch = m_container->data()[m_Position];
208 void seek(ssize_t offset, SeekDirection direction)
override
210 m_Position = SeekPosition(m_Position, m_container->size(), offset, direction);
213 void truncate()
override
215 m_container->resize(m_Position);
219 TContainer* m_container;
223class PODOFO_API SpanStreamDevice :
public StreamDevice
228 SpanStreamDevice(
const char* buffer,
size_t size);
229 SpanStreamDevice(
const bufferview& buffer);
230 SpanStreamDevice(
const std::string_view& view);
231 SpanStreamDevice(
const std::string& str);
232 SpanStreamDevice(std::string& str,
233 DeviceAccess access = DeviceAccess::ReadWrite);
234 SpanStreamDevice(
const char* str);
235 SpanStreamDevice(
char* buffer,
size_t size,
236 DeviceAccess access = DeviceAccess::ReadWrite);
237 SpanStreamDevice(
const bufferspan& span,
238 DeviceAccess access = DeviceAccess::ReadWrite);
241 size_t GetLength()
const override;
243 size_t GetPosition()
const override;
245 bool Eof()
const override;
247 bool CanSeek()
const override;
250 void writeBuffer(
const char* buffer,
size_t size)
override;
251 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
252 bool readChar(
char& ch)
override;
253 bool peek(
char& ch)
const override;
254 void seek(ssize_t offset, SeekDirection direction)
override;
255 void truncate()
override;
258 SpanStreamDevice(std::nullptr_t) =
delete;
273 size_t GetLength()
const override;
275 size_t GetPosition()
const override;
277 bool Eof()
const override;
280 void writeBuffer(
const char*
buffer,
size_t size)
override;
281 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
282 bool readChar(
char&
ch)
override;
283 bool peek(
char&
ch)
const override;
285 void truncate()
override;
An StreamDevice device that does nothing.
Definition StreamDevice.h:268
This class provides an output device which operates either on a file or on a buffer in memory.
Definition StreamDevice.h:28
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
FileMode
Definition StreamDevice.h:87
@ 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.