libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
PGFstream.cpp
Go to the documentation of this file.
1/*
2 * The Progressive Graphics File; http://www.libpgf.org
3 *
4 * $Date: 2007-01-19 11:51:24 +0100 (Fr, 19 Jan 2007) $
5 * $Revision: 268 $
6 *
7 * This file Copyright (C) 2006 xeraina GmbH, Switzerland
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
28
29#include "PGFstream.h"
30
31#ifdef WIN32
32#include <malloc.h>
33#endif
34
35namespace pgf {
37 // CPGFFileStream
39 void CPGFFileStream::Write(int* count, void* buffPtr) {
40 ASSERT(count);
41 ASSERT(buffPtr);
42 ASSERT(IsValid());
43 OSError err;
44 if ((err = FileWrite(m_hFile, count, buffPtr)) != NoError) ReturnWithError(err);
45
46 }
47
49 void CPGFFileStream::Read(int* count, void* buffPtr) {
50 ASSERT(count);
51 ASSERT(buffPtr);
52 ASSERT(IsValid());
53 OSError err;
54 if ((err = FileRead(m_hFile, count, buffPtr)) != NoError) ReturnWithError(err);
55 }
56
58 void CPGFFileStream::SetPos(short posMode, int64_t posOff) {
59 ASSERT(IsValid());
60 OSError err;
61 if ((err = SetFPos(m_hFile, posMode, posOff)) != NoError) ReturnWithError(err);
62 }
63
65 uint64_t CPGFFileStream::GetPos() const {
66 ASSERT(IsValid());
67 OSError err;
68 uint64_t pos = 0;
69 if ((err = GetFPos(m_hFile, &pos)) != NoError) ReturnWithError2(err, pos);
70 return pos;
71 }
72
73
75 // CPGFMemoryStream
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 }
85
90 CPGFMemoryStream::CPGFMemoryStream(uint8_t* pBuffer, size_t size)
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 }
98
103 void CPGFMemoryStream::Reinitialize(uint8_t* pBuffer, size_t size) {
104 if (!m_allocated) {
105 m_buffer = m_pos = pBuffer;
106 m_size = size;
107 m_eos = m_buffer + size;
108 }
109 }
110
112 void CPGFMemoryStream::Write(int* count, void* buffPtr) {
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 }
147
149 void CPGFMemoryStream::Read(int* count, void* buffPtr) {
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 }
167
169 void CPGFMemoryStream::SetPos(short posMode, int64_t posOff) {
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 }
187
188
190 // CPGFMemFileStream
191#ifdef _MFC_VER
193 void CPGFMemFileStream::Write(int* count, void* buffPtr) {
194 ASSERT(count);
195 ASSERT(buffPtr);
196 ASSERT(IsValid());
197 m_memFile->Write(buffPtr, *count);
198 }
199
201 void CPGFMemFileStream::Read(int* count, void* buffPtr) {
202 ASSERT(count);
203 ASSERT(buffPtr);
204 ASSERT(IsValid());
205 m_memFile->Read(buffPtr, *count);
206 }
207
209 void CPGFMemFileStream::SetPos(short posMode, int64_t posOff) {
210 ASSERT(IsValid());
211 m_memFile->Seek(posOff, posMode);
212 }
213
215 uint64_t CPGFMemFileStream::GetPos() const {
216 return (uint64_t)m_memFile->GetPosition();
217 }
218#endif // _MFC_VER
219
221 // CPGFIStream
222#if defined(WIN32) || defined(WINCE)
224 void CPGFIStream::Write(int* count, void* buffPtr) {
225 ASSERT(count);
226 ASSERT(buffPtr);
227 ASSERT(IsValid());
228
229 HRESULT hr = m_stream->Write(buffPtr, *count, (ULONG*)count);
230 if (FAILED(hr)) {
231 ReturnWithError(hr);
232 }
233 }
234
236 void CPGFIStream::Read(int* count, void* buffPtr) {
237 ASSERT(count);
238 ASSERT(buffPtr);
239 ASSERT(IsValid());
240
241 HRESULT hr = m_stream->Read(buffPtr, *count, (ULONG*)count);
242 if (FAILED(hr)) {
243 ReturnWithError(hr);
244 }
245 }
246
248 void CPGFIStream::SetPos(short posMode, int64_t posOff) {
249 ASSERT(IsValid());
250
251 LARGE_INTEGER li;
252 li.QuadPart = posOff;
253
254 HRESULT hr = m_stream->Seek(li, posMode, nullptr);
255 if (FAILED(hr)) {
256 ReturnWithError(hr);
257 }
258 }
259
261 uint64_t CPGFIStream::GetPos() const {
262 ASSERT(IsValid());
263
264 LARGE_INTEGER n;
265 ULARGE_INTEGER pos;
266 n.QuadPart = 0;
267
268 HRESULT hr = m_stream->Seek(n, FSFromCurrent, &pos);
269 if (SUCCEEDED(hr)) {
270 return pos.QuadPart;
271 } else {
272 ReturnWithError2(hr, pos.QuadPart);
273 }
274 }
275#endif // WIN32 || WINCE
276}
#define __max(x, y)
PGF stream class.
virtual void Read(int *count, void *buffer)
Definition PGFstream.cpp:49
virtual void Write(int *count, void *buffer)
Definition PGFstream.cpp:39
virtual uint64_t GetPos() const
Definition PGFstream.cpp:65
virtual bool IsValid() const
Definition PGFstream.h:99
HANDLE m_hFile
file handle
Definition PGFstream.h:84
virtual void SetPos(short posMode, int64_t posOff)
Definition PGFstream.cpp:58
virtual void SetPos(short posMode, int64_t posOff)
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
virtual void Read(int *count, void *buffer)
virtual void Write(int *count, void *buffer)
void Reinitialize(uint8_t *pBuffer, size_t size)
virtual bool IsValid() const
Definition PGFstream.h:140
size_t m_size
buffer size
Definition PGFstream.h:110
CPGFMemoryStream(size_t size)
Definition PGFstream.cpp:79
bool m_allocated
indicates a new allocated buffer
Definition PGFstream.h:111
Definition PGFimage.h:33