Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-10 10:23:45

0001 //========================================================================
0002 //
0003 // CachedFile.h
0004 //
0005 // Caching files support.
0006 //
0007 // This file is licensed under the GPLv2 or later
0008 //
0009 // Copyright 2009 Stefan Thomas <thomas@eload24.com>
0010 // Copyright 2010 Hib Eris <hib@hiberis.nl>
0011 // Copyright 2010, 2018-2020, 2022 Albert Astals Cid <aacid@kde.org>
0012 //
0013 //========================================================================
0014 
0015 #ifndef CACHEDFILE_H
0016 #define CACHEDFILE_H
0017 
0018 #include "poppler-config.h"
0019 #include "poppler_private_export.h"
0020 
0021 #include "Object.h"
0022 #include "Stream.h"
0023 
0024 #include <vector>
0025 
0026 //------------------------------------------------------------------------
0027 
0028 #define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
0029 
0030 class GooString;
0031 class CachedFileLoader;
0032 
0033 //------------------------------------------------------------------------
0034 // CachedFile
0035 //
0036 // CachedFile gives FILE-like access to a document at a specified URI.
0037 // In the constructor, you specify a CachedFileLoader that handles loading
0038 // the data from the document. The CachedFile requests no more data then it
0039 // needs from the CachedFileLoader.
0040 //------------------------------------------------------------------------
0041 
0042 class POPPLER_PRIVATE_EXPORT CachedFile
0043 {
0044 
0045     friend class CachedFileWriter;
0046 
0047 public:
0048     explicit CachedFile(CachedFileLoader *cacheLoader);
0049 
0050     CachedFile(const CachedFile &) = delete;
0051     CachedFile &operator=(const CachedFile &) = delete;
0052 
0053     unsigned int getLength() const { return length; }
0054     long int tell();
0055     int seek(long int offset, int origin);
0056     size_t read(void *ptr, size_t unitsize, size_t count);
0057     size_t write(const char *ptr, size_t size, size_t fromByte);
0058     int cache(const std::vector<ByteRange> &ranges);
0059 
0060     // Reference counting.
0061     void incRefCnt();
0062     void decRefCnt();
0063 
0064 private:
0065     ~CachedFile();
0066 
0067     enum ChunkState
0068     {
0069         chunkStateNew = 0,
0070         chunkStateLoaded
0071     };
0072 
0073     typedef struct
0074     {
0075         ChunkState state;
0076         char data[CachedFileChunkSize];
0077     } Chunk;
0078 
0079     int cache(size_t offset, size_t length);
0080 
0081     CachedFileLoader *loader;
0082 
0083     size_t length;
0084     size_t streamPos;
0085 
0086     std::vector<Chunk> *chunks;
0087 
0088     int refCnt; // reference count
0089 };
0090 
0091 //------------------------------------------------------------------------
0092 // CachedFileWriter
0093 //
0094 // CachedFileWriter handles sequential writes to a CachedFile.
0095 // On construction, you specify the CachedFile and the chunks of it to which data
0096 // should be written.
0097 //------------------------------------------------------------------------
0098 
0099 class POPPLER_PRIVATE_EXPORT CachedFileWriter
0100 {
0101 
0102 public:
0103     // Construct a CachedFile Writer.
0104     // The caller is responsible for deleting the cachedFile and chunksA.
0105     CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
0106 
0107     ~CachedFileWriter();
0108 
0109     // Writes size bytes from ptr to cachedFile, returns number of bytes written.
0110     size_t write(const char *ptr, size_t size);
0111 
0112 private:
0113     CachedFile *cachedFile;
0114     std::vector<int> *chunks;
0115     std::vector<int>::iterator it;
0116     size_t offset;
0117 };
0118 
0119 //------------------------------------------------------------------------
0120 // CachedFileLoader
0121 //
0122 // CachedFileLoader is an abstact class that specifies the interface for
0123 // loadng data from an URI into a CachedFile.
0124 //------------------------------------------------------------------------
0125 
0126 class POPPLER_PRIVATE_EXPORT CachedFileLoader
0127 {
0128 
0129 public:
0130     CachedFileLoader() = default;
0131     virtual ~CachedFileLoader();
0132 
0133     CachedFileLoader(const CachedFileLoader &) = delete;
0134     CachedFileLoader &operator=(const CachedFileLoader &) = delete;
0135 
0136     // Initializes the file load.
0137     // Returns the length of the file.
0138     // The caller is responsible for deleting cachedFile.
0139     virtual size_t init(CachedFile *cachedFile) = 0;
0140 
0141     // Loads specified byte ranges and passes it to the writer to store them.
0142     // Returns 0 on success, Anything but 0 on failure.
0143     // The caller is responsible for deleting the writer.
0144     virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
0145 };
0146 
0147 //------------------------------------------------------------------------
0148 
0149 #endif