Scroll to navigation

CPGFMemoryStream(3) Library Functions Manual CPGFMemoryStream(3)

NAME

CPGFMemoryStream - Memory stream class.

SYNOPSIS

#include <PGFstream.h>

Inherits 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::CPGFMemoryStream (size_t size)

Constructor

Parameters

size Size of new allocated memory buffer

Allocate memory block of given size

Parameters

size Memory size

Definition at line 78 of file PGFstream.cpp..PP

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 }

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

Constructor. Use already allocated memory of given size

Parameters

pBuffer Memory location
size Memory size

Use already allocated memory of given size

Parameters

pBuffer Memory location
size Memory size

Definition at line 89 of file PGFstream.cpp..PP

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 CPGFMemoryStream::~CPGFMemoryStream () [inline], [virtual]

Definition at line 128 of file PGFstream.h..PP

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

UINT8 * CPGFMemoryStream::GetBuffer () [inline]

Returns

Memory buffer

Definition at line 147 of file PGFstream.h..PP

147 { return m_buffer; }

const UINT8 * CPGFMemoryStream::GetBuffer () const [inline]

Returns

Memory buffer

Definition at line 145 of file PGFstream.h..PP

145 { return m_buffer; }

UINT64 CPGFMemoryStream::GetEOS () const [inline]

Returns

relative position of end of stream (= stream length)

Definition at line 149 of file PGFstream.h..PP

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

virtual UINT64 CPGFMemoryStream::GetPos () const [inline], [virtual]

Get current stream position.

Returns

Current stream position

Implements CPGFStream.

Definition at line 139 of file PGFstream.h..PP

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

size_t CPGFMemoryStream::GetSize () const [inline]

Returns

Memory size

Definition at line 143 of file PGFstream.h..PP

143 { return m_size; }

virtual bool CPGFMemoryStream::IsValid () const [inline], [virtual]

Check stream validity.

Returns

True if stream and current position is valid

Implements CPGFStream.

Definition at line 140 of file PGFstream.h..PP

140 { return m_buffer != 0; }

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

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

Parameters

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

Implements CPGFStream.

Definition at line 148 of file PGFstream.cpp..PP

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 }

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

Use already allocated memory of given size

Parameters

pBuffer Memory location
size Memory size

Definition at line 102 of file PGFstream.cpp..PP

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

void CPGFMemoryStream::SetEOS (UINT64 length) [inline]

Parameters

length Stream length (= relative position of end of stream)

Definition at line 151 of file PGFstream.h..PP

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

void CPGFMemoryStream::SetPos (short posMode, INT64 posOff) [virtual]

Set stream position either absolute or relative.

Parameters

posMode A position mode (FSFromStart, FSFromCurrent, FSFromEnd)
posOff A new stream position (absolute positioning) or a position offset (relative positioning)

Implements CPGFStream.

Definition at line 168 of file PGFstream.cpp..PP

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 }

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

Write some bytes out of a buffer into this stream.

Parameters

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

Implements CPGFStream.

Definition at line 111 of file PGFstream.cpp..PP

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

bool CPGFMemoryStream::m_allocated [protected]

indicates a new allocated buffer

Definition at line 111 of file PGFstream.h.

UINT8* CPGFMemoryStream::m_buffer [protected]

Definition at line 108 of file PGFstream.h.

UINT8* CPGFMemoryStream::m_eos [protected]

end of stream (first address beyond written area)

Definition at line 109 of file PGFstream.h.

UINT8 * CPGFMemoryStream::m_pos [protected]

buffer start address and current buffer address

Definition at line 108 of file PGFstream.h.

size_t CPGFMemoryStream::m_size [protected]

buffer size

Definition at line 110 of file PGFstream.h.

Author

Generated automatically by Doxygen for libpgf from the source code.

Version 7.21.2 libpgf