aboutsummaryrefslogtreecommitdiff
path: root/src/istream/buffer.h
blob: 91bb53fd52e2fef0a739b409193b6d52660f9e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once

#include <cstdio>
#include <iostream>
#include <streambuf>
#include <vector>

namespace dictzip {

// Warning: The inherent statefulness of stream buffers interferes with
// multithreaded access. Users of this class should perform appropriate
// locking, since they are in a better position to do so.
class IstreamBuf : public std::streambuf {
private:
	struct Chunk {
		Chunk(size_t offset, size_t size):
			offset(offset),
			size(size) {};

		const size_t offset;
		const size_t size;
	};

	FILE* dictzip_file_;

	std::vector<unsigned char> buffer_;
	std::vector<unsigned char> header_;
	std::vector<Chunk>  chunks_;

	size_t chunk_length_;
	long data_offset_;
	long curr_chunk_;

	void readChunk(long n);
	void readHeader();
	void readExtra();
	void skipOptional();

protected:
	int underflow();
	std::streamsize xsgetn(char *dest, std::streamsize n);

public:
	using pos_type = std::streambuf::pos_type;
	using off_type = std::streambuf::off_type;

	typedef std::ios::seekdir  seekdir;
	typedef std::ios::openmode openmode;

	IstreamBuf(char const* filename);
	~IstreamBuf();

	pos_type seekoff(off_type off, seekdir dir, openmode);
	pos_type seekpos(pos_type off, openmode mode);

};

}