CachedFile.h 3.96 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 Albert Astals Cid <aacid@kde.org>
Hib Eris's avatar
Hib Eris committed
12 13 14 15 16 17 18 19 20 21 22
//
//========================================================================

#ifndef CACHEDFILE_H
#define CACHEDFILE_H

#include "poppler-config.h"

#include "goo/gtypes.h"
#include "Object.h"
#include "Stream.h"
Albert Astals Cid's avatar
Albert Astals Cid committed
23 24

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

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

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

class GooString;
class CachedFileLoader;

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

class CachedFile {

friend class CachedFileWriter;

public:

  CachedFile(CachedFileLoader *cacheLoader, GooString *uri);

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

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

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

private:

66 67
  ~CachedFile();

Hib Eris's avatar
Hib Eris committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  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
86
  std::vector<Chunk> *chunks;
Hib Eris's avatar
Hib Eris committed
87 88 89 90 91

  int refCnt;  // reference count

};

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

class CachedFileWriter {

public:

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

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

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

private:

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

};

Hib Eris's avatar
Hib Eris committed
122 123 124 125 126
//------------------------------------------------------------------------
// 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
127 128 129 130 131 132
//------------------------------------------------------------------------

class CachedFileLoader {

public:

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

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

Hib Eris's avatar
Hib Eris committed
139 140 141
  // 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
142
  virtual size_t init(GooString *uri, CachedFile *cachedFile) = 0;
Hib Eris's avatar
Hib Eris committed
143 144 145 146

  // Loads speficified 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.
Albert Astals Cid's avatar
Albert Astals Cid committed
147
  virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
Hib Eris's avatar
Hib Eris committed
148 149 150 151 152 153

};

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

#endif