~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to frmts/pdf/pdfio.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id $
 
3
 *
 
4
 * Project:  PDF driver
 
5
 * Purpose:  GDALDataset driver for PDF dataset.
 
6
 * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2010, Even Rouault
 
10
 *
 
11
 * Permission is hereby granted, free of charge, to any person obtaining a
 
12
 * copy of this software and associated documentation files (the "Software"),
 
13
 * to deal in the Software without restriction, including without limitation
 
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
 * and/or sell copies of the Software, and to permit persons to whom the
 
16
 * Software is furnished to do so, subject to the following conditions:
 
17
 *
 
18
 * The above copyright notice and this permission notice shall be included
 
19
 * in all copies or substantial portions of the Software.
 
20
 *
 
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
27
 * DEALINGS IN THE SOFTWARE.
 
28
 ****************************************************************************/
 
29
 
 
30
#ifdef USE_POPPLER
 
31
 
 
32
/* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
 
33
/* in include/poppler/goo/gtypes.h with the one defined in cpl_port.h */
 
34
#define CPL_GBOOL_DEFINED
 
35
 
 
36
#include "pdfio.h"
 
37
 
 
38
#include "cpl_vsi.h"
 
39
 
 
40
CPL_CVSID("$Id: pdfio.cpp 22491 2011-06-03 16:04:53Z rouault $");
 
41
 
 
42
/************************************************************************/
 
43
/*                         VSIPDFFileStream()                           */
 
44
/************************************************************************/
 
45
 
 
46
VSIPDFFileStream::VSIPDFFileStream(VSILFILE* f, const char* pszFilename,
 
47
                                   Guint startA, GBool limitedA,
 
48
                                   Guint lengthA, Object *dictA):
 
49
#ifdef POPPLER_BASE_STREAM_HAS_TWO_ARGS
 
50
                                                        BaseStream(dictA, lengthA)
 
51
#else
 
52
                                                        BaseStream(dictA)
 
53
#endif
 
54
{
 
55
    poParent = NULL;
 
56
    poFilename = new GooString(pszFilename);
 
57
    this->f = f;
 
58
    nStart = startA;
 
59
    bLimited = limitedA;
 
60
    nLength = lengthA;
 
61
    nCurrentPos = -1;
 
62
    nSavedPos = -1;
 
63
    nPosInBuffer = nBufferLength = -1;
 
64
}
 
65
 
 
66
/************************************************************************/
 
67
/*                         VSIPDFFileStream()                           */
 
68
/************************************************************************/
 
69
 
 
70
VSIPDFFileStream::VSIPDFFileStream(VSIPDFFileStream* poParent,
 
71
                                   Guint startA, GBool limitedA,
 
72
                                   Guint lengthA, Object *dictA):
 
73
#ifdef POPPLER_BASE_STREAM_HAS_TWO_ARGS
 
74
                                                        BaseStream(dictA, lengthA)
 
75
#else
 
76
                                                        BaseStream(dictA)
 
77
#endif
 
78
{
 
79
    this->poParent = poParent;
 
80
    poFilename = poParent->poFilename;
 
81
    f = poParent->f;
 
82
    nStart = startA;
 
83
    bLimited = limitedA;
 
84
    nLength = lengthA;
 
85
    nCurrentPos = -1;
 
86
    nSavedPos = -1;
 
87
    nPosInBuffer = nBufferLength = -1;
 
88
}
 
89
 
 
90
/************************************************************************/
 
91
/*                        ~VSIPDFFileStream()                           */
 
92
/************************************************************************/
 
93
 
 
94
VSIPDFFileStream::~VSIPDFFileStream()
 
95
{
 
96
    close();
 
97
    if (poParent == NULL)
 
98
    {
 
99
        delete poFilename;
 
100
        if (f)
 
101
            VSIFCloseL(f);
 
102
    }
 
103
}
 
104
 
 
105
/************************************************************************/
 
106
/*                             makeSubStream()                          */
 
107
/************************************************************************/
 
108
 
 
109
Stream *VSIPDFFileStream::makeSubStream(Guint startA, GBool limitedA,
 
110
                                        Guint lengthA, Object *dictA)
 
111
{
 
112
    return new VSIPDFFileStream(this,
 
113
                                startA, limitedA,
 
114
                                lengthA, dictA);
 
115
}
 
116
 
 
117
/************************************************************************/
 
118
/*                                 getPos()                             */
 
119
/************************************************************************/
 
120
 
 
121
int VSIPDFFileStream::getPos()
 
122
{
 
123
    return nCurrentPos;
 
124
}
 
125
 
 
126
/************************************************************************/
 
127
/*                                getStart()                            */
 
128
/************************************************************************/
 
129
 
 
130
Guint VSIPDFFileStream::getStart()
 
131
{
 
132
    return nStart;
 
133
}
 
134
 
 
135
/************************************************************************/
 
136
/*                             getKind()                                */
 
137
/************************************************************************/
 
138
 
 
139
StreamKind VSIPDFFileStream::getKind()
 
140
{
 
141
    return strFile;
 
142
}
 
143
 
 
144
/************************************************************************/
 
145
/*                           getFileName()                               */
 
146
/************************************************************************/
 
147
 
 
148
GooString *VSIPDFFileStream::getFileName()
 
149
{
 
150
    return poFilename;
 
151
}
 
152
 
 
153
/************************************************************************/
 
154
/*                             FillBuffer()                             */
 
155
/************************************************************************/
 
156
 
 
157
int VSIPDFFileStream::FillBuffer()
 
158
{
 
159
    if (nBufferLength == 0)
 
160
        return FALSE;
 
161
    if (nBufferLength != -1 && nBufferLength < BUFFER_SIZE)
 
162
        return FALSE;
 
163
 
 
164
    nPosInBuffer = 0;
 
165
    int nToRead;
 
166
    if (!bLimited)
 
167
        nToRead = BUFFER_SIZE;
 
168
    else if (nCurrentPos + BUFFER_SIZE > nStart + nLength)
 
169
        nToRead = nStart + nLength - nCurrentPos;
 
170
    else
 
171
        nToRead = BUFFER_SIZE;
 
172
    nBufferLength = VSIFReadL(abyBuffer, 1, nToRead, f);
 
173
    if (nBufferLength == 0)
 
174
        return FALSE;
 
175
 
 
176
    return TRUE;
 
177
}
 
178
 
 
179
/************************************************************************/
 
180
/*                                getChar()                             */
 
181
/************************************************************************/
 
182
 
 
183
/* The unoptimized version performs a bit well since we must go through */
 
184
/* the whole virtual I/O chain for each character reading. We save a few */
 
185
/* percent with this extra internal caching */
 
186
 
 
187
int VSIPDFFileStream::getChar()
 
188
{
 
189
#ifdef unoptimized_version
 
190
    GByte chRead;
 
191
    if (bLimited && nCurrentPos >= nStart + nLength)
 
192
        return EOF;
 
193
    if (VSIFReadL(&chRead, 1, 1, f) == 0)
 
194
        return EOF;
 
195
#else
 
196
    if (nPosInBuffer == nBufferLength)
 
197
    {
 
198
        if (!FillBuffer())
 
199
            return EOF;
 
200
    }
 
201
 
 
202
    GByte chRead = abyBuffer[nPosInBuffer];
 
203
    nPosInBuffer ++;
 
204
#endif
 
205
    nCurrentPos ++;
 
206
    return chRead;
 
207
}
 
208
 
 
209
/************************************************************************/
 
210
/*                       getUnfilteredChar()                            */
 
211
/************************************************************************/
 
212
 
 
213
int VSIPDFFileStream::getUnfilteredChar ()
 
214
{
 
215
    return getChar();
 
216
}
 
217
 
 
218
/************************************************************************/
 
219
/*                               lookChar()                             */
 
220
/************************************************************************/
 
221
 
 
222
int VSIPDFFileStream::lookChar()
 
223
{
 
224
#ifdef unoptimized_version
 
225
    int nPosBefore = nCurrentPos;
 
226
    int chRead = getChar();
 
227
    if (chRead == EOF)
 
228
        return EOF;
 
229
    VSIFSeekL(f, nCurrentPos = nPosBefore, SEEK_SET);
 
230
    return chRead;
 
231
#else
 
232
    int chRead = getChar();
 
233
    if (chRead == EOF)
 
234
        return EOF;
 
235
    nPosInBuffer --;
 
236
    nCurrentPos --;
 
237
    return chRead;
 
238
#endif
 
239
}
 
240
 
 
241
/************************************************************************/
 
242
/*                                reset()                               */
 
243
/************************************************************************/
 
244
 
 
245
void VSIPDFFileStream::reset()
 
246
{
 
247
    nSavedPos = (int)VSIFTellL(f);
 
248
    VSIFSeekL(f, nCurrentPos = nStart, SEEK_SET);
 
249
    nPosInBuffer = nBufferLength = -1;
 
250
}
 
251
 
 
252
/************************************************************************/
 
253
/*                         unfilteredReset()                            */
 
254
/************************************************************************/
 
255
 
 
256
void VSIPDFFileStream::unfilteredReset ()
 
257
{
 
258
    reset();
 
259
}
 
260
 
 
261
/************************************************************************/
 
262
/*                                close()                               */
 
263
/************************************************************************/
 
264
 
 
265
void VSIPDFFileStream::close()
 
266
{
 
267
    if (nSavedPos != -1)
 
268
        VSIFSeekL(f, nCurrentPos = nSavedPos, SEEK_SET);
 
269
    nSavedPos = -1;
 
270
}
 
271
 
 
272
/************************************************************************/
 
273
/*                               setPos()                               */
 
274
/************************************************************************/
 
275
 
 
276
void VSIPDFFileStream::setPos(Guint pos, int dir)
 
277
{
 
278
    if (dir >= 0)
 
279
    {
 
280
        VSIFSeekL(f, nCurrentPos = pos, SEEK_SET);
 
281
    }
 
282
    else
 
283
    {
 
284
        if (bLimited == gFalse)
 
285
        {
 
286
            VSIFSeekL(f, 0, SEEK_END);
 
287
        }
 
288
        else
 
289
        {
 
290
            VSIFSeekL(f, nStart + nLength, SEEK_SET);
 
291
        }
 
292
        Guint size = (Guint)VSIFTellL(f);
 
293
        if (pos > size)
 
294
            pos = (Guint)size;
 
295
        VSIFSeekL(f, nCurrentPos = size - pos, SEEK_SET);
 
296
    }
 
297
    nPosInBuffer = nBufferLength = -1;
 
298
}
 
299
 
 
300
/************************************************************************/
 
301
/*                            moveStart()                               */
 
302
/************************************************************************/
 
303
 
 
304
void VSIPDFFileStream::moveStart(int delta)
 
305
{
 
306
    nStart += delta;
 
307
    VSIFSeekL(f, nCurrentPos = nStart, SEEK_SET);
 
308
    nPosInBuffer = nBufferLength = -1;
 
309
}
 
310
 
 
311
#endif