CachedFile.h 3.95 KB
Newer Older
Hib Eris's avatar
Hib Eris committed
1 2 3 4 5 6 7 8 9 10
//========================================================================
//
// 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>
11
// Copyright 2010, 2018, 2019 Albert Astals Cid <aacid@kde.org>
Hib Eris's avatar
Hib Eris committed
12 13 14 15 16 17 18 19 20 21
//
//========================================================================

#ifndef CACHEDFILE_H
#define CACHEDFILE_H

#include "poppler-config.h"

#include "Object.h"
#include "Stream.h"
Albert Astals Cid's avatar
Albert Astals Cid committed
22 23

#include <vector>
Hib Eris's avatar
Hib Eris committed
24 25 26

//------------------------------------------------------------------------

Hib Eris's avatar
Hib Eris committed
27
#define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
Hib Eris's avatar
Hib Eris committed
28 29 30 31

class GooString;
class CachedFileLoader;

Hib Eris's avatar
Hib Eris committed
32 33 34 35 36 37 38
//------------------------------------------------------------------------
// 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.
Hib Eris's avatar
Hib Eris committed
39 40 41 42 43 44 45 46 47 48
//------------------------------------------------------------------------

class CachedFile {

friend class CachedFileWriter;

public:

  CachedFile(CachedFileLoader *cacheLoader, GooString *uri);

49 50 51
  CachedFile(const CachedFile &) = delete;
  CachedFile& operator=(const CachedFile &) = delete;

52
  unsigned int getLength() const { return length; }
Hib Eris's avatar
Hib Eris committed
53 54 55 56
  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);
Albert Astals Cid's avatar
Albert Astals Cid committed
57
  int cache(const std::vector<ByteRange> &ranges);
Hib Eris's avatar
Hib Eris committed
58 59 60 61 62 63 64

  // Reference counting.
  void incRefCnt();
  void decRefCnt();

private:

65 66
  ~CachedFile();

Hib Eris's avatar
Hib Eris committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  enum ChunkState {
    chunkStateNew = 0,
    chunkStateLoaded
  };

  typedef struct {
    ChunkState state;
    char data[CachedFileChunkSize];
  } Chunk;

  int cache(size_t offset, size_t length);

  CachedFileLoader *loader;
  GooString *uri;

  size_t length;
  size_t streamPos;

Albert Astals Cid's avatar
Albert Astals Cid committed
85
  std::vector<Chunk> *chunks;
Hib Eris's avatar
Hib Eris committed
86 87 88 89 90

  int refCnt;  // reference count

};

Hib Eris's avatar
Hib Eris committed
91 92 93 94 95 96
//------------------------------------------------------------------------
// 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.
Hib Eris's avatar
Hib Eris committed
97 98 99 100 101 102
//------------------------------------------------------------------------

class CachedFileWriter {

public:

Hib Eris's avatar
Hib Eris committed
103 104
  // Construct a CachedFile Writer.
  // The caller is responsible for deleting the cachedFile and chunksA.
Albert Astals Cid's avatar
Albert Astals Cid committed
105
  CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
Hib Eris's avatar
Hib Eris committed
106

Hib Eris's avatar
Hib Eris committed
107 108
  ~CachedFileWriter();

Hib Eris's avatar
Hib Eris committed
109
  // Writes size bytes from ptr to cachedFile, returns number of bytes written.
Hib Eris's avatar
Hib Eris committed
110 111 112 113 114
  size_t write(const char *ptr, size_t size);

private:

  CachedFile *cachedFile;
Albert Astals Cid's avatar
Albert Astals Cid committed
115 116
  std::vector<int> *chunks;
  std::vector<int>::iterator it;
Hib Eris's avatar
Hib Eris committed
117 118 119 120
  size_t offset;

};

Hib Eris's avatar
Hib Eris committed
121 122 123 124 125
//------------------------------------------------------------------------
// CachedFileLoader
//
// CachedFileLoader is an abstact class that specifies the interface for
// loadng data from an URI into a CachedFile.
Hib Eris's avatar
Hib Eris committed
126 127 128 129 130 131
//------------------------------------------------------------------------

class CachedFileLoader {

public:

132
  CachedFileLoader() = default;
Hib Eris's avatar
Hib Eris committed
133
  virtual ~CachedFileLoader() {};
Hib Eris's avatar
Hib Eris committed
134

135 136 137
  CachedFileLoader(const CachedFileLoader &) = delete;
  CachedFileLoader& operator=(const CachedFileLoader &) = delete;

Hib Eris's avatar
Hib Eris committed
138 139 140
  // Initializes the file load.
  // Returns the length of the file.
  // The caller is responsible for deleting uri and cachedFile.
Hib Eris's avatar
Hib Eris committed
141
  virtual size_t init(GooString *uri, CachedFile *cachedFile) = 0;
Hib Eris's avatar
Hib Eris committed
142

Yuri Chornoivan's avatar
Yuri Chornoivan committed
143
  // Loads specified byte ranges and passes it to the writer to store them.
Hib Eris's avatar
Hib Eris committed
144 145
  // Returns 0 on success, Anything but 0 on failure.
  // The caller is responsible for deleting the writer.
Albert Astals Cid's avatar
Albert Astals Cid committed
146
  virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
Hib Eris's avatar
Hib Eris committed
147 148 149 150 151 152

};

//------------------------------------------------------------------------

#endif