-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathread.h
65 lines (60 loc) · 1.33 KB
/
read.h
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
59
60
61
62
63
64
65
#ifndef FPZIP_READ_H
#define FPZIP_READ_H
#include "types.h"
#define subsize(T, n) (CHAR_BIT * sizeof(T) * (n) / 32)
// file reader for compressed data
#if FPZIP_BLOCK_SIZE > 1
class RCfiledecoder : public RCdecoder {
public:
RCfiledecoder(FILE* file) : RCdecoder(), file(file), count(0), index(0), size(0) {}
uint getbyte()
{
if (index == size) {
size = fread(buffer, 1, FPZIP_BLOCK_SIZE, file);
if (!size) {
size = 1;
error = true;
}
else
count += size;
index = 0;
}
return buffer[index++];
}
size_t bytes() const { return count; }
private:
FILE* file;
size_t count;
size_t index;
size_t size;
uchar buffer[FPZIP_BLOCK_SIZE];
};
#else
class RCfiledecoder : public RCdecoder {
public:
RCfiledecoder(FILE* file) : RCdecoder(), file(file), count(0) {}
uint getbyte()
{
int byte = fgetc(file);
if (byte == EOF)
error = true;
else
count++;
return byte;
}
size_t bytes() const { return count; }
private:
FILE* file;
size_t count;
};
#endif
class RCmemdecoder : public RCdecoder {
public:
RCmemdecoder(const void* buffer) : RCdecoder(), ptr((const uchar*)buffer), begin(ptr) {}
uint getbyte() { return *ptr++; }
size_t bytes() const { return ptr - begin; }
private:
const uchar* ptr;
const uchar* const begin;
};
#endif