aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/ClosedFileInputSource.cc
blob: e9c9b3bdc7565c9190a53f9bbd31461f5f0389e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <qpdf/ClosedFileInputSource.hh>
#include <qpdf/FileInputSource.hh>

ClosedFileInputSource::Members::Members(char const* filename) :
    filename(filename),
    offset(0),
    fis(0),
    stay_open(false)
{
}

ClosedFileInputSource::Members::~Members()
{
    delete fis;
}

ClosedFileInputSource::ClosedFileInputSource(char const* filename) :
    m(new Members(filename))
{
}

ClosedFileInputSource::~ClosedFileInputSource()
{
}

void
ClosedFileInputSource::before()
{
    if (0 == this->m->fis)
    {
        this->m->fis = new FileInputSource();
        this->m->fis->setFilename(this->m->filename.c_str());
        this->m->fis->seek(this->m->offset, SEEK_SET);
        this->m->fis->setLastOffset(this->last_offset);
    }
}

void
ClosedFileInputSource::after()
{
    this->last_offset = this->m->fis->getLastOffset();
    this->m->offset = this->m->fis->tell();
    if (this->m->stay_open)
    {
        return;
    }
    delete this->m->fis;
    this->m->fis = 0;
}

qpdf_offset_t
ClosedFileInputSource::findAndSkipNextEOL()
{
    before();
    qpdf_offset_t r = this->m->fis->findAndSkipNextEOL();
    after();
    return r;
}

std::string const&
ClosedFileInputSource::getName() const
{
    return this->m->filename;
}

qpdf_offset_t
ClosedFileInputSource::tell()
{
    before();
    qpdf_offset_t r = this->m->fis->tell();
    after();
    return r;
}

void
ClosedFileInputSource::seek(qpdf_offset_t offset, int whence)
{
    before();
    this->m->fis->seek(offset, whence);
    after();
}

void
ClosedFileInputSource::rewind()
{
    this->m->offset = 0;
    if (this->m->fis)
    {
        this->m->fis->rewind();
    }
}

size_t
ClosedFileInputSource::read(char* buffer, size_t length)
{
    before();
    size_t r = this->m->fis->read(buffer, length);
    after();
    return r;
}

void
ClosedFileInputSource::unreadCh(char ch)
{
    before();
    this->m->fis->unreadCh(ch);
    // Don't call after -- the file has to stay open after this
    // operation.
}

void
ClosedFileInputSource::stayOpen(bool val)
{
    this->m->stay_open = val;
    if ((! val) && this->m->fis)
    {
        after();
    }
}