openMSX
CompressedFileAdapter.cc
Go to the documentation of this file.
2 #include "FileException.hh"
3 #include "hash_set.hh"
4 #include "xxhash.hh"
5 #include <cstring>
6 
7 using std::string;
8 
9 namespace openmsx {
10 
12  template<typename Ptr> const string& operator()(const Ptr& p) const {
13  return p->cachedURL;
14  }
15 };
17  GetURLFromDecompressed, XXHasher> decompressCache;
18 
19 
20 CompressedFileAdapter::CompressedFileAdapter(std::unique_ptr<FileBase> file_)
21  : file(std::move(file_)), pos(0)
22 {
23 }
24 
26 {
27  auto it = decompressCache.find(getURL());
28  decompressed.reset();
29  if (it != end(decompressCache) && it->unique()) {
30  // delete last user of Decompressed, remove from cache
31  decompressCache.erase(it);
32  }
33 }
34 
35 void CompressedFileAdapter::decompress()
36 {
37  if (decompressed) return;
38 
39  string url = getURL();
40  if (auto it = decompressCache.find(url); it != end(decompressCache)) {
41  decompressed = *it;
42  } else {
43  decompressed = std::make_shared<Decompressed>();
44  decompress(*file, *decompressed);
45  decompressed->cachedModificationDate = getModificationDate();
46  decompressed->cachedURL = std::move(url);
47  decompressCache.insert_noDuplicateCheck(decompressed);
48  }
49 
50  // close original file after succesful decompress
51  file.reset();
52 }
53 
54 void CompressedFileAdapter::read(void* buffer, size_t num)
55 {
56  decompress();
57  if (decompressed->size < (pos + num)) {
58  throw FileException("Read beyond end of file");
59  }
60  const auto& buf = decompressed->buf;
61  memcpy(buffer, buf.data() + pos, num);
62  pos += num;
63 }
64 
65 void CompressedFileAdapter::write(const void* /*buffer*/, size_t /*num*/)
66 {
67  throw FileException("Writing to compressed files not yet supported");
68 }
69 
71 {
72  decompress();
73  return { decompressed->buf.data(), decompressed->size };
74 }
75 
77 {
78  // nothing
79 }
80 
82 {
83  decompress();
84  return decompressed->size;
85 }
86 
87 void CompressedFileAdapter::seek(size_t newpos)
88 {
89  pos = newpos;
90 }
91 
93 {
94  return pos;
95 }
96 
97 void CompressedFileAdapter::truncate(size_t /*size*/)
98 {
99  throw FileException("Truncating compressed files not yet supported.");
100 }
101 
103 {
104  // nothing because writing is not supported
105 }
106 
108 {
109  return file ? file->getURL() : decompressed->cachedURL;
110 }
111 
113 {
114  decompress();
115  return decompressed->originalName;
116 }
117 
119 {
120  return true;
121 }
122 
124 {
125  return file ? file->getModificationDate()
126  : decompressed->cachedModificationDate;
127 }
128 
129 } // namespace openmsx
FileException.hh
xxhash.hh
openmsx::CompressedFileAdapter::seek
void seek(size_t pos) final override
Definition: CompressedFileAdapter.cc:87
openmsx::CompressedFileAdapter::isReadOnly
bool isReadOnly() const final override
Definition: CompressedFileAdapter.cc:118
openmsx::CompressedFileAdapter::decompress
virtual void decompress(FileBase &file, Decompressed &decompressed)=0
openmsx::CompressedFileAdapter::mmap
span< uint8_t > mmap() final override
Definition: CompressedFileAdapter.cc:70
XXHasher
Definition: xxhash.hh:153
hash_set.hh
span
Definition: span.hh:126
openmsx::CompressedFileAdapter::getPos
size_t getPos() final override
Definition: CompressedFileAdapter.cc:92
openmsx::CompressedFileAdapter::truncate
void truncate(size_t size) final override
Definition: CompressedFileAdapter.cc:97
openmsx::FileException
Definition: FileException.hh:9
CompressedFileAdapter.hh
openmsx::CompressedFileAdapter::getSize
size_t getSize() final override
Definition: CompressedFileAdapter.cc:81
openmsx::CompressedFileAdapter::munmap
void munmap() final override
Definition: CompressedFileAdapter.cc:76
openmsx::GetURLFromDecompressed::operator()
const string & operator()(const Ptr &p) const
Definition: CompressedFileAdapter.cc:12
openmsx::GetURLFromDecompressed
Definition: CompressedFileAdapter.cc:11
hash_set
Definition: hash_set.hh:266
openmsx::CompressedFileAdapter::write
void write(const void *buffer, size_t num) final override
Definition: CompressedFileAdapter.cc:65
openmsx::CompressedFileAdapter::getOriginalName
std::string getOriginalName() final override
Definition: CompressedFileAdapter.cc:112
openmsx::CompressedFileAdapter::~CompressedFileAdapter
~CompressedFileAdapter() override
Definition: CompressedFileAdapter.cc:25
openmsx::CompressedFileAdapter::getURL
std::string getURL() const final override
Definition: CompressedFileAdapter.cc:107
openmsx::CompressedFileAdapter::CompressedFileAdapter
CompressedFileAdapter(std::unique_ptr< FileBase > file)
Definition: CompressedFileAdapter.cc:20
openmsx::CompressedFileAdapter::getModificationDate
time_t getModificationDate() final override
Definition: CompressedFileAdapter.cc:123
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::CompressedFileAdapter::read
void read(void *buffer, size_t num) final override
Definition: CompressedFileAdapter.cc:54
openmsx::CompressedFileAdapter::flush
void flush() final override
Definition: CompressedFileAdapter.cc:102