libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
pgf::CPGFMemoryStream Class Reference

Memory stream class. More...

#include <PGFstream.h>

Inheritance diagram for pgf::CPGFMemoryStream:
pgf::CPGFStream

Public Member Functions

 CPGFMemoryStream (size_t size)
 CPGFMemoryStream (uint8_t *pBuffer, size_t size)
void Reinitialize (uint8_t *pBuffer, size_t size)
virtual ~CPGFMemoryStream ()
virtual void Write (int *count, void *buffer)
virtual void Read (int *count, void *buffer)
virtual void SetPos (short posMode, int64_t posOff)
virtual uint64_t GetPos () const
virtual bool IsValid () const
size_t GetSize () const
const uint8_t * GetBuffer () const
uint8_t * GetBuffer ()
uint64_t GetEOS () const
void SetEOS (uint64_t length)

Protected Attributes

uint8_t * m_buffer
uint8_t * m_pos
 buffer start address and current buffer address
uint8_t * m_eos
 end of stream (first address beyond written area)
size_t m_size
 buffer size
bool m_allocated
 indicates a new allocated buffer

Detailed Description

Memory stream class.

A PGF stream subclass for internal memory.

Author
C. Stamm

Definition at line 106 of file PGFstream.h.

Constructor & Destructor Documentation

◆ CPGFMemoryStream() [1/2]

pgf::CPGFMemoryStream::CPGFMemoryStream ( size_t size)

Constructor

Parameters
sizeSize of new allocated memory buffer

Allocate memory block of given size

Parameters
sizeMemory size

Definition at line 79 of file PGFstream.cpp.

80 : m_size(size)
81 , m_allocated(true) {
82 m_buffer = m_pos = m_eos = new(std::nothrow) uint8_t[m_size];
83 if (!m_buffer) ReturnWithError(InsufficientMemory);
84 }
uint8_t * m_pos
buffer start address and current buffer address
Definition PGFstream.h:108
uint8_t * m_eos
end of stream (first address beyond written area)
Definition PGFstream.h:109
size_t m_size
buffer size
Definition PGFstream.h:110
bool m_allocated
indicates a new allocated buffer
Definition PGFstream.h:111

◆ CPGFMemoryStream() [2/2]

pgf::CPGFMemoryStream::CPGFMemoryStream ( uint8_t * pBuffer,
size_t size )

Constructor. Use already allocated memory of given size

Parameters
pBufferMemory location
sizeMemory size

Use already allocated memory of given size

Parameters
pBufferMemory location
sizeMemory size

Definition at line 90 of file PGFstream.cpp.

91 : m_buffer(pBuffer)
92 , m_pos(pBuffer)
93 , m_eos(pBuffer + size)
94 , m_size(size)
95 , m_allocated(false) {
96 ASSERT(IsValid());
97 }
virtual bool IsValid() const
Definition PGFstream.h:140

◆ ~CPGFMemoryStream()

virtual pgf::CPGFMemoryStream::~CPGFMemoryStream ( )
inlinevirtual

Definition at line 128 of file PGFstream.h.

128 {
129 m_pos = 0;
130 if (m_allocated) {
131 // the memory buffer has been allocated inside of CPMFmemoryStream constructor
132 delete[] m_buffer; m_buffer = 0;
133 }
134 }

Member Function Documentation

◆ GetBuffer() [1/2]

uint8_t * pgf::CPGFMemoryStream::GetBuffer ( )
inline
Returns
Memory buffer

Definition at line 147 of file PGFstream.h.

147{ return m_buffer; }

◆ GetBuffer() [2/2]

const uint8_t * pgf::CPGFMemoryStream::GetBuffer ( ) const
inline
Returns
Memory buffer

Definition at line 145 of file PGFstream.h.

145{ return m_buffer; }

◆ GetEOS()

uint64_t pgf::CPGFMemoryStream::GetEOS ( ) const
inline
Returns
relative position of end of stream (= stream length)

Definition at line 149 of file PGFstream.h.

149{ ASSERT(IsValid()); return m_eos - m_buffer; }

◆ GetPos()

virtual uint64_t pgf::CPGFMemoryStream::GetPos ( ) const
inlinevirtual

Get current stream position.

Returns
Current stream position

Implements pgf::CPGFStream.

Definition at line 139 of file PGFstream.h.

139{ ASSERT(IsValid()); return m_pos - m_buffer; }

◆ GetSize()

size_t pgf::CPGFMemoryStream::GetSize ( ) const
inline
Returns
Memory size

Definition at line 143 of file PGFstream.h.

143{ return m_size; }

◆ IsValid()

virtual bool pgf::CPGFMemoryStream::IsValid ( ) const
inlinevirtual

Check stream validity.

Returns
True if stream and current position is valid

Implements pgf::CPGFStream.

Definition at line 140 of file PGFstream.h.

140{ return m_buffer != 0; }

◆ Read()

void pgf::CPGFMemoryStream::Read ( int * count,
void * buffer )
virtual

Read some bytes from this stream and stores them into a buffer.

Parameters
countA pointer to a value containing the number of bytes should be read. After this call it contains the number of read bytes.
bufferA memory buffer

Implements pgf::CPGFStream.

Definition at line 149 of file PGFstream.cpp.

149 {
150 ASSERT(IsValid());
151 ASSERT(count);
152 ASSERT(buffPtr);
153 ASSERT(m_buffer + m_size >= m_eos);
154 ASSERT(m_pos <= m_eos);
155
156 if (m_pos + *count <= m_eos) {
157 memcpy(buffPtr, m_pos, *count);
158 m_pos += *count;
159 } else {
160 // end of memory block reached -> read only until end
161 *count = (int)__max(0, m_eos - m_pos);
162 memcpy(buffPtr, m_pos, *count);
163 m_pos += *count;
164 }
165 ASSERT(m_pos <= m_eos);
166 }
#define __max(x, y)

◆ Reinitialize()

void pgf::CPGFMemoryStream::Reinitialize ( uint8_t * pBuffer,
size_t size )

Use already allocated memory of given size

Parameters
pBufferMemory location
sizeMemory size

Definition at line 103 of file PGFstream.cpp.

103 {
104 if (!m_allocated) {
105 m_buffer = m_pos = pBuffer;
106 m_size = size;
107 m_eos = m_buffer + size;
108 }
109 }

◆ SetEOS()

void pgf::CPGFMemoryStream::SetEOS ( uint64_t length)
inline
Parameters
lengthStream length (= relative position of end of stream)

Definition at line 151 of file PGFstream.h.

151{ ASSERT(IsValid()); m_eos = m_buffer + length; }

◆ SetPos()

void pgf::CPGFMemoryStream::SetPos ( short posMode,
int64_t posOff )
virtual

Set stream position either absolute or relative.

Parameters
posModeA position mode (FSFromStart, FSFromCurrent, FSFromEnd)
posOffA new stream position (absolute positioning) or a position offset (relative positioning)

Implements pgf::CPGFStream.

Definition at line 169 of file PGFstream.cpp.

169 {
170 ASSERT(IsValid());
171 switch (posMode) {
172 case FSFromStart:
173 m_pos = m_buffer + posOff;
174 break;
175 case FSFromCurrent:
176 m_pos += posOff;
177 break;
178 case FSFromEnd:
179 m_pos = m_eos + posOff;
180 break;
181 default:
182 ASSERT(false);
183 }
184 if (m_pos > m_eos)
185 ReturnWithError(InvalidStreamPos);
186 }

◆ Write()

void pgf::CPGFMemoryStream::Write ( int * count,
void * buffer )
virtual

Write some bytes out of a buffer into this stream.

Parameters
countA pointer to a value containing the number of bytes should be written. After this call it contains the number of written bytes.
bufferA memory buffer

Implements pgf::CPGFStream.

Definition at line 112 of file PGFstream.cpp.

112 {
113 ASSERT(count);
114 ASSERT(buffPtr);
115 ASSERT(IsValid());
116 const size_t deltaSize = 0x4000 + *count;
117
118 if (m_pos + *count <= m_buffer + m_size) {
119 memcpy(m_pos, buffPtr, *count);
120 m_pos += *count;
121 if (m_pos > m_eos) m_eos = m_pos;
122 } else if (m_allocated) {
123 // memory block is too small -> reallocate a deltaSize larger block
124 size_t offset = m_pos - m_buffer;
125 uint8_t* buf_tmp = (uint8_t*)realloc(m_buffer, m_size + deltaSize);
126 if (!buf_tmp) {
127 delete[] m_buffer;
128 m_buffer = 0;
129 ReturnWithError(InsufficientMemory);
130 } else {
131 m_buffer = buf_tmp;
132 }
133 m_size += deltaSize;
134
135 // reposition m_pos
136 m_pos = m_buffer + offset;
137
138 // write block
139 memcpy(m_pos, buffPtr, *count);
140 m_pos += *count;
141 if (m_pos > m_eos) m_eos = m_pos;
142 } else {
143 ReturnWithError(InsufficientMemory);
144 }
145 ASSERT(m_pos <= m_eos);
146 }

Member Data Documentation

◆ m_allocated

bool pgf::CPGFMemoryStream::m_allocated
protected

indicates a new allocated buffer

Definition at line 111 of file PGFstream.h.

◆ m_buffer

uint8_t* pgf::CPGFMemoryStream::m_buffer
protected

Definition at line 108 of file PGFstream.h.

◆ m_eos

uint8_t* pgf::CPGFMemoryStream::m_eos
protected

end of stream (first address beyond written area)

Definition at line 109 of file PGFstream.h.

◆ m_pos

uint8_t * pgf::CPGFMemoryStream::m_pos
protected

buffer start address and current buffer address

Definition at line 108 of file PGFstream.h.

◆ m_size

size_t pgf::CPGFMemoryStream::m_size
protected

buffer size

Definition at line 110 of file PGFstream.h.


The documentation for this class was generated from the following files: