Subversion Repositories Scribus

Rev

Rev 16775 | Rev 19721 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16775 fschmid 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
18122 mrdocs 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16775 fschmid 22
 */
23
 
24
//////////////////////////////////////////////////////////////////////
25
/// @file PGFstream.cpp
26
/// @brief PGF stream class implementation
27
/// @author C. Stamm
28
 
29
#include "PGFstream.h"
30
 
31
#ifdef WIN32
32
#include <malloc.h>
33
#endif
34
 
35
//////////////////////////////////////////////////////////////////////
36
// CPGFFileStream
37
//////////////////////////////////////////////////////////////////////
38
void CPGFFileStream::Write(int *count, void *buffPtr) THROW_ {
39
        ASSERT(count);
40
        ASSERT(buffPtr);
41
        ASSERT(IsValid());
42
        OSError err;
43
        if ((err = FileWrite(m_hFile, count, buffPtr)) != NoError) ReturnWithError(err);
44
 
45
}
46
 
47
//////////////////////////////////////////////////////////////////////
48
void CPGFFileStream::Read(int *count, void *buffPtr) THROW_ {
49
        ASSERT(count);
50
        ASSERT(buffPtr);
51
        ASSERT(IsValid());
52
        OSError err;
53
        if ((err = FileRead(m_hFile, count, buffPtr)) != NoError) ReturnWithError(err);
54
}
55
 
56
//////////////////////////////////////////////////////////////////////
57
void CPGFFileStream::SetPos(short posMode, INT64 posOff) THROW_ {
58
        ASSERT(IsValid());
59
        OSError err;
60
        if ((err = SetFPos(m_hFile, posMode, posOff)) != NoError) ReturnWithError(err);
61
}
62
 
63
//////////////////////////////////////////////////////////////////////
64
UINT64 CPGFFileStream::GetPos() const THROW_ {
65
        ASSERT(IsValid());
66
        OSError err;
67
        UINT64 pos;
68
        if ((err = GetFPos(m_hFile, &pos)) != NoError) ReturnWithError2(err, pos);
69
        return pos;
70
}
71
 
72
 
73
//////////////////////////////////////////////////////////////////////
74
// CPGFMemoryStream
75
//////////////////////////////////////////////////////////////////////
76
/// Allocate memory block of given size
77
/// @param size Memory size
78
CPGFMemoryStream::CPGFMemoryStream(size_t size) THROW_
79
: m_size(size)
80
, m_allocated(true) {
81
        m_buffer = m_pos = new UINT8[m_size];
82
        if (!m_buffer) ReturnWithError(InsufficientMemory);
83
}
84
 
85
//////////////////////////////////////////////////////////////////////
86
/// Use already allocated memory of given size
87
/// @param pBuffer Memory location
88
/// @param size Memory size
89
CPGFMemoryStream::CPGFMemoryStream(UINT8 *pBuffer, size_t size) THROW_
90
: m_buffer(pBuffer)
91
, m_pos(pBuffer)
92
, m_size(size)
93
, m_allocated(false) {
94
        ASSERT(IsValid());
95
}
96
 
97
//////////////////////////////////////////////////////////////////////
98
/// Use already allocated memory of given size
99
/// @param pBuffer Memory location
100
/// @param size Memory size
101
void CPGFMemoryStream::Reinitialize(UINT8 *pBuffer, size_t size) THROW_ {
102
        if (!m_allocated) {
103
                m_buffer = pBuffer;
104
                m_pos = pBuffer;
105
                m_size = size;
106
        }
107
}
108
 
109
//////////////////////////////////////////////////////////////////////
110
void CPGFMemoryStream::Write(int *count, void *buffPtr) THROW_ {
111
        ASSERT(count);
112
        ASSERT(buffPtr);
113
        ASSERT(IsValid());
114
        const size_t deltaSize = 0x4000 + *count;
115
 
116
        if (m_pos + *count <= m_buffer + m_size) {
117
                memcpy(m_pos, buffPtr, *count);
118
                m_pos += *count;
119
        } else if (m_allocated) {
120
                // memory block is too small -> reallocate a deltaSize larger block
121
                size_t offset = m_pos - m_buffer;
122
                UINT8 *buf_tmp = (UINT8 *)realloc(m_buffer, m_size + deltaSize);
123
                if (!buf_tmp) {
124
                        delete[] m_buffer;
125
                        m_buffer = 0;
126
                        ReturnWithError(InsufficientMemory);
127
                } else {
128
                        m_buffer = buf_tmp;
129
                }
130
                m_size += deltaSize;
131
 
132
                // reposition m_pos
133
                m_pos = m_buffer + offset;
134
 
135
                // write block
136
                memcpy(m_pos, buffPtr, *count);
137
                m_pos += *count;
138
        } else {
139
                ReturnWithError(InsufficientMemory);
140
        }
141
}
142
 
143
//////////////////////////////////////////////////////////////////////
144
void CPGFMemoryStream::Read(int *count, void *buffPtr) THROW_ {
145
        ASSERT(count);
146
        ASSERT(buffPtr);
147
        ASSERT(IsValid());
148
 
149
        if (m_pos + *count <= m_buffer + m_size) {
150
                memcpy(buffPtr, m_pos, *count);
151
                m_pos += *count;
152
        } else {
153
                // end of memory block reached -> read only until end
154
                *count = (int)(m_buffer + m_size - m_pos);
155
                memcpy(buffPtr, m_pos, *count);
156
                m_pos += *count;
157
                ReturnWithError(EndOfMemory);
158
        }
159
}
160
 
161
//////////////////////////////////////////////////////////////////////
162
void CPGFMemoryStream::SetPos(short posMode, INT64 posOff) THROW_ {
163
        ASSERT(IsValid());
164
        switch(posMode) {
165
        case FSFromStart:
166
                m_pos = m_buffer + posOff;
167
                break;
168
        case FSFromCurrent:
169
                m_pos += posOff;
170
                break;
171
        case FSFromEnd:
172
                m_pos = m_buffer + m_size + posOff;
173
                break;
174
        default:
175
                ASSERT(false);
176
        }
177
}
178
 
179
//////////////////////////////////////////////////////////////////////
180
UINT64 CPGFMemoryStream::GetPos() const THROW_ {
181
        ASSERT(IsValid());
182
        return m_pos - m_buffer;
183
}
184
 
185
 
186
//////////////////////////////////////////////////////////////////////
187
// CPGFMemFileStream
188
#ifdef _MFC_VER
189
//////////////////////////////////////////////////////////////////////
190
void CPGFMemFileStream::Write(int *count, void *buffPtr) THROW_ {
191
        ASSERT(count);
192
        ASSERT(buffPtr);
193
        ASSERT(IsValid());
194
        m_memFile->Write(buffPtr, *count);
195
}
196
 
197
//////////////////////////////////////////////////////////////////////
198
void CPGFMemFileStream::Read(int *count, void *buffPtr) THROW_ {
199
        ASSERT(count);
200
        ASSERT(buffPtr);
201
        ASSERT(IsValid());
202
        m_memFile->Read(buffPtr, *count);
203
}
204
 
205
//////////////////////////////////////////////////////////////////////
206
void CPGFMemFileStream::SetPos(short posMode, INT64 posOff) THROW_ {
207
        ASSERT(IsValid());
208
        m_memFile->Seek(posOff, posMode);
209
}
210
 
211
//////////////////////////////////////////////////////////////////////
212
UINT64 CPGFMemFileStream::GetPos() const THROW_ {
213
        return (UINT64)m_memFile->GetPosition();
214
}
215
#endif // _MFC_VER
216
 
217
//////////////////////////////////////////////////////////////////////
218
// CPGFIStream
219
#if defined(WIN32) || defined(WINCE)
220
//////////////////////////////////////////////////////////////////////
221
void CPGFIStream::Write(int *count, void *buffPtr) THROW_ {
222
        ASSERT(count);
223
        ASSERT(buffPtr);
224
        ASSERT(IsValid());
225
 
226
        HRESULT hr = m_stream->Write(buffPtr, *count, (ULONG *)count);
227
        if (FAILED(hr)) {
228
                ReturnWithError(hr);
229
        }
230
}
231
 
232
//////////////////////////////////////////////////////////////////////
233
void CPGFIStream::Read(int *count, void *buffPtr) THROW_ {
234
        ASSERT(count);
235
        ASSERT(buffPtr);
236
        ASSERT(IsValid());
237
 
238
        HRESULT hr = m_stream->Read(buffPtr, *count, (ULONG *)count);
239
        if (FAILED(hr)) {
240
                ReturnWithError(hr);
241
        }
242
}
243
 
244
//////////////////////////////////////////////////////////////////////
245
void CPGFIStream::SetPos(short posMode, INT64 posOff) THROW_ {
246
        ASSERT(IsValid());
247
 
248
        LARGE_INTEGER li;
249
        li.QuadPart = posOff;
250
 
251
        HRESULT hr = m_stream->Seek(li, posMode, NULL);
252
        if (FAILED(hr)) {
253
                ReturnWithError(hr);
254
        }
255
}
256
 
257
//////////////////////////////////////////////////////////////////////
258
UINT64 CPGFIStream::GetPos() const THROW_ {
259
        ASSERT(IsValid());
260
 
261
        LARGE_INTEGER n;
262
        ULARGE_INTEGER pos;
263
        n.QuadPart = 0;
264
 
265
        HRESULT hr = m_stream->Seek(n, FSFromCurrent, &pos);
266
        if (SUCCEEDED(hr)) {
267
                return pos.QuadPart;
268
        } else {
269
                ReturnWithError2(hr, pos.QuadPart);
270
        }
271
}
272
#endif // WIN32 || WINCE