libpgf 7.21.2
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
CPGFMemoryStream Class Reference

Memory stream class. More...

#include <PGFstream.h>

Inheritance diagram for CPGFMemoryStream:
CPGFStream

Public Member Functions

 CPGFMemoryStream (size_t size)
 
 CPGFMemoryStream (UINT8 *pBuffer, size_t size)
 
void Reinitialize (UINT8 *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 posOff)
 
virtual UINT64 GetPos () const
 
virtual bool IsValid () const
 
size_t GetSize () const
 
const UINT8 * GetBuffer () const
 
UINT8 * GetBuffer ()
 
UINT64 GetEOS () const
 
void SetEOS (UINT64 length)
 

Protected Attributes

UINT8 * m_buffer
 
UINT8 * m_pos
 buffer start address and current buffer address
 
UINT8 * 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]

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 78 of file PGFstream.cpp.

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

◆ CPGFMemoryStream() [2/2]

CPGFMemoryStream::CPGFMemoryStream ( UINT8 *  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 89 of file PGFstream.cpp.

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

◆ ~CPGFMemoryStream()

virtual 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 * CPGFMemoryStream::GetBuffer ( )
inline
Returns
Memory buffer

Definition at line 147 of file PGFstream.h.

147{ return m_buffer; }

◆ GetBuffer() [2/2]

const UINT8 * CPGFMemoryStream::GetBuffer ( ) const
inline
Returns
Memory buffer

Definition at line 145 of file PGFstream.h.

145{ return m_buffer; }

◆ GetEOS()

UINT64 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 CPGFMemoryStream::GetPos ( ) const
inlinevirtual

Get current stream position.

Returns
Current stream position

Implements CPGFStream.

Definition at line 139 of file PGFstream.h.

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

◆ GetSize()

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

Definition at line 143 of file PGFstream.h.

143{ return m_size; }

◆ IsValid()

virtual bool CPGFMemoryStream::IsValid ( ) const
inlinevirtual

Check stream validity.

Returns
True if stream and current position is valid

Implements CPGFStream.

Definition at line 140 of file PGFstream.h.

140{ return m_buffer != 0; }

◆ Read()

void 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 CPGFStream.

Definition at line 148 of file PGFstream.cpp.

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

◆ Reinitialize()

void CPGFMemoryStream::Reinitialize ( UINT8 *  pBuffer,
size_t  size 
)

Use already allocated memory of given size

Parameters
pBufferMemory location
sizeMemory size

Definition at line 102 of file PGFstream.cpp.

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

◆ SetEOS()

void CPGFMemoryStream::SetEOS ( UINT64  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 CPGFMemoryStream::SetPos ( short  posMode,
INT64  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 CPGFStream.

Definition at line 168 of file PGFstream.cpp.

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

◆ Write()

void 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 CPGFStream.

Definition at line 111 of file PGFstream.cpp.

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

Member Data Documentation

◆ m_allocated

bool CPGFMemoryStream::m_allocated
protected

indicates a new allocated buffer

Definition at line 111 of file PGFstream.h.

◆ m_buffer

UINT8* CPGFMemoryStream::m_buffer
protected

Definition at line 108 of file PGFstream.h.

◆ m_eos

UINT8* CPGFMemoryStream::m_eos
protected

end of stream (first address beyond written area)

Definition at line 109 of file PGFstream.h.

◆ m_pos

UINT8 * CPGFMemoryStream::m_pos
protected

buffer start address and current buffer address

Definition at line 108 of file PGFstream.h.

◆ m_size

size_t 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: