-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedFile.h
149 lines (113 loc) · 4.11 KB
/
CachedFile.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//========================================================================
//
// CachedFile.h
//
// Caching files support.
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2009 Stefan Thomas <thomas@eload24.com>
// Copyright 2010 Hib Eris <hib@hiberis.nl>
// Copyright 2010, 2018-2020, 2022 Albert Astals Cid <aacid@kde.org>
//
//========================================================================
#ifndef CACHEDFILE_H
#define CACHEDFILE_H
#include "poppler-config.h"
#include "poppler_private_export.h"
#include "Object.h"
#include "Stream.h"
#include <vector>
//------------------------------------------------------------------------
#define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
class GooString;
class CachedFileLoader;
//------------------------------------------------------------------------
// CachedFile
//
// CachedFile gives FILE-like access to a document at a specified URI.
// In the constructor, you specify a CachedFileLoader that handles loading
// the data from the document. The CachedFile requests no more data then it
// needs from the CachedFileLoader.
//------------------------------------------------------------------------
class POPPLER_PRIVATE_EXPORT CachedFile
{
friend class CachedFileWriter;
public:
explicit CachedFile(CachedFileLoader *cacheLoader);
CachedFile(const CachedFile &) = delete;
CachedFile &operator=(const CachedFile &) = delete;
unsigned int getLength() const { return length; }
long int tell();
int seek(long int offset, int origin);
size_t read(void *ptr, size_t unitsize, size_t count);
size_t write(const char *ptr, size_t size, size_t fromByte);
int cache(const std::vector<ByteRange> &ranges);
// Reference counting.
void incRefCnt();
void decRefCnt();
private:
~CachedFile();
enum ChunkState
{
chunkStateNew = 0,
chunkStateLoaded
};
typedef struct
{
ChunkState state;
char data[CachedFileChunkSize];
} Chunk;
int cache(size_t offset, size_t length);
CachedFileLoader *loader;
size_t length;
size_t streamPos;
std::vector<Chunk> *chunks;
int refCnt; // reference count
};
//------------------------------------------------------------------------
// CachedFileWriter
//
// CachedFileWriter handles sequential writes to a CachedFile.
// On construction, you specify the CachedFile and the chunks of it to which data
// should be written.
//------------------------------------------------------------------------
class POPPLER_PRIVATE_EXPORT CachedFileWriter
{
public:
// Construct a CachedFile Writer.
// The caller is responsible for deleting the cachedFile and chunksA.
CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
~CachedFileWriter();
// Writes size bytes from ptr to cachedFile, returns number of bytes written.
size_t write(const char *ptr, size_t size);
private:
CachedFile *cachedFile;
std::vector<int> *chunks;
std::vector<int>::iterator it;
size_t offset;
};
//------------------------------------------------------------------------
// CachedFileLoader
//
// CachedFileLoader is an abstact class that specifies the interface for
// loadng data from an URI into a CachedFile.
//------------------------------------------------------------------------
class POPPLER_PRIVATE_EXPORT CachedFileLoader
{
public:
CachedFileLoader() = default;
virtual ~CachedFileLoader();
CachedFileLoader(const CachedFileLoader &) = delete;
CachedFileLoader &operator=(const CachedFileLoader &) = delete;
// Initializes the file load.
// Returns the length of the file.
// The caller is responsible for deleting cachedFile.
virtual size_t init(CachedFile *cachedFile) = 0;
// Loads specified byte ranges and passes it to the writer to store them.
// Returns 0 on success, Anything but 0 on failure.
// The caller is responsible for deleting the writer.
virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
};
//------------------------------------------------------------------------
#endif