5#ifndef AUX_STREAM_DEVICE_H
6#define AUX_STREAM_DEVICE_H
16#include "InputDevice.h"
17#include "OutputDevice.h"
35 void resetBuffers()
override;
40class PODOFO_API StandardStreamDevice :
public StreamDevice
46 StandardStreamDevice(std::ostream&
stream);
48 StandardStreamDevice(std::istream&
stream);
53 StandardStreamDevice(std::iostream&
stream);
55 ~StandardStreamDevice();
58 size_t GetLength()
const override;
60 size_t GetPosition()
const override;
62 bool CanSeek()
const override;
64 bool Eof()
const override;
68 void writeBuffer(
const char*
buffer,
size_t size)
override;
69 void flush()
override;
70 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
71 bool readChar(
char&
ch)
override;
72 bool peek(
char&
ch)
const override;
74 void truncate()
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;
100class PODOFO_API FileStreamDevice :
public StreamDevice
104 FileStreamDevice(
const std::string_view & filepath);
107 FileStreamDevice(
const std::string_view & filepath, FileMode mode);
110 FileStreamDevice(
const std::string_view& filepath, FileMode mode,
111 DeviceAccess access);
116 const std::string& GetFilepath()
const {
return m_Filepath; }
118 size_t GetLength()
const override;
120 size_t GetPosition()
const override;
122 bool CanSeek()
const override;
124 bool Eof()
const override;
127 void writeBuffer(
const char* buffer,
size_t size)
override;
128 void flush()
override;
129 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
130 bool readChar(
char& ch)
override;
131 bool peek(
char& ch)
const override;
132 void seek(ssize_t offset, SeekDirection direction)
override;
133 void close()
override;
134 void truncate()
override;
135 void resetBuffers()
override;
139 enum class BufferDirection : uint8_t
150 void setDirection(BufferDirection direction);
152 size_t getPendingBytesCount()
const;
166 bool tryRetainBuffer(
size_t pos);
167 void ensureOpen()
const;
168 void assertInvariants()
const;
171 std::string m_Filepath;
172 std::unique_ptr<char[]> m_Buffer;
173 size_t m_BufferOffset;
177 BufferDirection m_Direction;
182template <
typename TContainer>
183class ContainerStreamDevice :
public StreamDevice
186 ContainerStreamDevice(TContainer& container,
187 DeviceAccess access,
bool ate) :
188 StreamDevice(access),
189 m_container(&container),
190 m_Position(ate ? container.size() : 0)
192 tryEnableReadWindow();
196 ContainerStreamDevice(
const TContainer& container) :
197 ContainerStreamDevice(const_cast<TContainer&>(container), DeviceAccess::
Read, false) { }
200 ContainerStreamDevice(TContainer& container) :
201 ContainerStreamDevice(container, DeviceAccess::ReadWrite, true) { }
204 size_t GetLength()
const override {
return m_container->size(); }
206 size_t GetPosition()
const override {
return getPos(); }
208 bool CanSeek()
const override {
return true; }
210 bool Eof()
const override {
return getPos() == m_container->size(); }
213 void writeBuffer(
const char* buffer,
size_t size)
override
215 size_t pos = getPos();
216 if (pos + size > m_container->size())
220 m_container->resize(pos + size);
221 std::memcpy(m_container->data() + pos, buffer, size);
222 m_Position = pos + size;
223 tryEnableReadWindow();
227 std::memcpy(m_container->data() + pos, buffer, size);
231 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override
233 size_t pos = getPos();
234 size_t readCount = std::min(size, m_container->size() - pos);
235 std::memcpy(buffer, m_container->data() + pos, readCount);
236 setPos(pos + readCount);
237 eof = pos + readCount == m_container->size();
241 bool readChar(
char& ch)
override
243 size_t pos = getPos();
244 if (pos == m_container->size())
250 ch = m_container->data()[pos];
255 bool peek(
char& ch)
const override
257 size_t pos = getPos();
258 if (pos == m_container->size())
264 ch = m_container->data()[pos];
268 void seek(ssize_t offset, SeekDirection direction)
override
271 m_Position = SeekPosition(m_Position, m_container->size(), offset, direction);
272 tryEnableReadWindow();
275 void truncate()
override
278 m_container->resize(m_Position);
279 tryEnableReadWindow();
282 void resetBuffers()
override
284 m_Position = getPos();
290 size_t getPos()
const
294 return m_tail ==
nullptr ? m_Position : (size_t)(
m_head - m_container->data());
298 void setPos(
size_t pos)
301 if (m_tail !=
nullptr)
302 m_head = m_container->data() + pos;
307 void tryEnableReadWindow()
309 if ((GetAccess() & DeviceAccess::Read) != DeviceAccess{ })
310 enableReadWindow(m_container->data() + m_Position, m_container->data() + m_container->size());
314 TContainer* m_container;
318class PODOFO_API SpanStreamDevice :
public StreamDevice
323 SpanStreamDevice(
const char* buffer,
size_t size);
324 SpanStreamDevice(
const bufferview& buffer);
325 SpanStreamDevice(
const std::string_view& view);
326 SpanStreamDevice(
const std::string& str);
327 SpanStreamDevice(std::string& str,
328 DeviceAccess access = DeviceAccess::ReadWrite);
329 SpanStreamDevice(
const char* str);
330 SpanStreamDevice(
char* buffer,
size_t size,
331 DeviceAccess access = DeviceAccess::ReadWrite);
332 SpanStreamDevice(
const bufferspan& span,
333 DeviceAccess access = DeviceAccess::ReadWrite);
336 size_t GetLength()
const override;
338 size_t GetPosition()
const override;
340 bool Eof()
const override;
342 bool CanSeek()
const override;
345 void writeBuffer(
const char* buffer,
size_t size)
override;
346 size_t readBuffer(
char* buffer,
size_t size,
bool& eof)
override;
347 bool readChar(
char& ch)
override;
348 bool peek(
char& ch)
const override;
349 void seek(ssize_t offset, SeekDirection direction)
override;
350 void truncate()
override;
351 void resetBuffers()
override;
354 SpanStreamDevice(std::nullptr_t) =
delete;
358 size_t getPos()
const;
360 void setPos(
size_t pos);
362 void tryEnableReadWindow();
377 size_t GetLength()
const override;
379 size_t GetPosition()
const override;
381 bool Eof()
const override;
384 void writeBuffer(
const char*
buffer,
size_t size)
override;
385 size_t readBuffer(
char*
buffer,
size_t size,
bool&
eof)
override;
386 bool readChar(
char&
ch)
override;
387 bool peek(
char&
ch)
const override;
389 void truncate()
override;
An StreamDevice device that does nothing.
Definition StreamDevice.h:372
This class provides an output device which operates either on a file or on a buffer in memory.
Definition StreamDevice.h:28
void resetBuffers() override
NOTE: It just forwards to InputStreamDevice, disambiguating the InputStreamDevice/OutputStreamDevice ...
Definition StreamDevice.cpp:113
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
@ None
Do not add a default appearance.
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.