-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwrite.h
73 lines (68 loc) · 1.5 KB
/
write.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
66
67
68
69
70
71
72
73
#ifndef FPZIP_WRITE_H
#define FPZIP_WRITE_H
#include "types.h"
#define subsize(T, n) (CHAR_BIT * sizeof(T) * (n) / 32)
// file writer for compressed data
#if FPZIP_BLOCK_SIZE > 1
class RCfileencoder : public RCencoder {
public:
RCfileencoder(FILE* file) : RCencoder(), file(file), count(0), size(0) {}
~RCfileencoder() { flush(); }
void putbyte(uint byte)
{
if (size == FPZIP_BLOCK_SIZE)
flush();
buffer[size++] = (uchar)byte;
}
void flush()
{
if (fwrite(buffer, 1, size, file) != size)
error = true;
else
count += size;
size = 0;
}
size_t bytes() const { return count; }
private:
FILE* file;
size_t count;
size_t size;
uchar buffer[FPZIP_BLOCK_SIZE];
};
#else
class RCfileencoder : public RCencoder {
public:
RCfileencoder(FILE* file) : RCencoder(), file(file), count(0) {}
void putbyte(uint byte)
{
if (fputc(byte, file) == EOF)
error = true;
else
count++;
}
size_t bytes() const { return count; }
private:
FILE* file;
size_t count;
};
#endif
// memory writer for compressed data
class RCmemencoder : public RCencoder {
public:
RCmemencoder(void* buffer, size_t size) : RCencoder(), ptr((uchar*)buffer), begin(ptr), end(ptr + size) {}
void putbyte(uint byte)
{
if (ptr == end) {
error = true;
fpzip_errno = fpzipErrorBufferOverflow;
}
else
*ptr++ = (uchar)byte;
}
size_t bytes() const { return ptr - begin; }
private:
uchar* ptr;
const uchar* const begin;
const uchar* const end;
};
#endif