beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / CachedFile.h
blobb99ea1ecad4c9cbc0c9fb604e8c9d7f7915367ab
1 //========================================================================
2 //
3 // CachedFile.h
4 //
5 // Caching files support.
6 //
7 // This file is licensed under the GPLv2 or later
8 //
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 //========================================================================
15 #ifndef CACHEDFILE_H
16 #define CACHEDFILE_H
18 #include "poppler-config.h"
20 #include "goo/gtypes.h"
21 #include "Object.h"
22 #include "Stream.h"
24 #include <vector>
26 //------------------------------------------------------------------------
28 #define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
30 class GooString;
31 class CachedFileLoader;
33 //------------------------------------------------------------------------
34 // CachedFile
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 //------------------------------------------------------------------------
42 class CachedFile {
44 friend class CachedFileWriter;
46 public:
48 CachedFile(CachedFileLoader *cacheLoader, GooString *uri);
50 Guint getLength() { return length; }
51 long int tell();
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.
58 void incRefCnt();
59 void decRefCnt();
61 private:
63 ~CachedFile();
65 enum ChunkState {
66 chunkStateNew = 0,
67 chunkStateLoaded
70 typedef struct {
71 ChunkState state;
72 char data[CachedFileChunkSize];
73 } Chunk;
75 int cache(size_t offset, size_t length);
77 CachedFileLoader *loader;
78 GooString *uri;
80 size_t length;
81 size_t streamPos;
83 std::vector<Chunk> *chunks;
85 int refCnt; // reference count
89 //------------------------------------------------------------------------
90 // CachedFileWriter
92 // CachedFileWriter handles sequential writes to a CachedFile.
93 // On construction, you specify the CachedFile and the chunks of it to which data
94 // should be written.
95 //------------------------------------------------------------------------
97 class CachedFileWriter {
99 public:
101 // Construct a CachedFile Writer.
102 // The caller is responsible for deleting the cachedFile and chunksA.
103 CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
105 ~CachedFileWriter();
107 // Writes size bytes from ptr to cachedFile, returns number of bytes written.
108 size_t write(const char *ptr, size_t size);
110 private:
112 CachedFile *cachedFile;
113 std::vector<int> *chunks;
114 std::vector<int>::iterator it;
115 size_t offset;
119 //------------------------------------------------------------------------
120 // CachedFileLoader
122 // CachedFileLoader is an abstact class that specifies the interface for
123 // loadng data from an URI into a CachedFile.
124 //------------------------------------------------------------------------
126 class CachedFileLoader {
128 public:
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 //------------------------------------------------------------------------
146 #endif