1 //========================================================================
5 // Caching files support.
7 // This file is licensed under the GPLv2 or later
9 // Copyright 2009 Stefan Thomas <thomas@eload24.com>
10 // Copyright 2010 Hib Eris <hib@hiberis.nl>
11 // Copyright 2010 Albert Astals Cid <aacid@kde.org>
13 //========================================================================
18 #include "poppler-config.h"
20 #include "goo/gtypes.h"
26 //------------------------------------------------------------------------
28 #define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
31 class CachedFileLoader
;
33 //------------------------------------------------------------------------
36 // CachedFile gives FILE-like access to a document at a specified URI.
37 // In the constructor, you specify a CachedFileLoader that handles loading
38 // the data from the document. The CachedFile requests no more data then it
39 // needs from the CachedFileLoader.
40 //------------------------------------------------------------------------
44 friend class CachedFileWriter
;
48 CachedFile(CachedFileLoader
*cacheLoader
, GooString
*uri
);
50 Guint
getLength() { return length
; }
52 int seek(long int offset
, int origin
);
53 size_t read(void * ptr
, size_t unitsize
, size_t count
);
54 size_t write(const char *ptr
, size_t size
, size_t fromByte
);
55 int cache(const std::vector
<ByteRange
> &ranges
);
57 // Reference counting.
72 char data
[CachedFileChunkSize
];
75 int cache(size_t offset
, size_t length
);
77 CachedFileLoader
*loader
;
83 std::vector
<Chunk
> *chunks
;
85 int refCnt
; // reference count
89 //------------------------------------------------------------------------
92 // CachedFileWriter handles sequential writes to a CachedFile.
93 // On construction, you specify the CachedFile and the chunks of it to which data
95 //------------------------------------------------------------------------
97 class CachedFileWriter
{
101 // Construct a CachedFile Writer.
102 // The caller is responsible for deleting the cachedFile and chunksA.
103 CachedFileWriter(CachedFile
*cachedFile
, std::vector
<int> *chunksA
);
107 // Writes size bytes from ptr to cachedFile, returns number of bytes written.
108 size_t write(const char *ptr
, size_t size
);
112 CachedFile
*cachedFile
;
113 std::vector
<int> *chunks
;
114 std::vector
<int>::iterator it
;
119 //------------------------------------------------------------------------
122 // CachedFileLoader is an abstact class that specifies the interface for
123 // loadng data from an URI into a CachedFile.
124 //------------------------------------------------------------------------
126 class CachedFileLoader
{
130 virtual ~CachedFileLoader() {};
132 // Initializes the file load.
133 // Returns the length of the file.
134 // The caller is responsible for deleting uri and cachedFile.
135 virtual size_t init(GooString
*uri
, CachedFile
*cachedFile
) = 0;
137 // Loads speficified byte ranges and passes it to the writer to store them.
138 // Returns 0 on success, Anything but 0 on failure.
139 // The caller is responsible for deleting the writer.
140 virtual int load(const std::vector
<ByteRange
> &ranges
, CachedFileWriter
*writer
) = 0;
144 //------------------------------------------------------------------------