-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlock.cpp
More file actions
83 lines (70 loc) · 1.6 KB
/
Block.cpp
File metadata and controls
83 lines (70 loc) · 1.6 KB
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
74
75
76
77
78
79
80
81
82
/*
* Block1.cpp
*
* Created on: Oct 8, 2011
* Author: august
*/
#include "Block.h"
#include "lzo/lzo1x.h"
#include "abfs.h"
#include "abfile.h"
#include <stdio.h>
#include <limits.h>
#include <string.h>
int Block::write() {
lzo_uint outsize;
#ifdef NOCOMP
size_t written=fwrite(data, dataLen, 1, f);
#else
lzo_byte wrkmem[LZO1X_1_MEM_COMPRESS];
byte *compbuffer = getCompBuffer();
lzo1x_1_compress(data, dataLen, compbuffer, &outsize, wrkmem);
FILE *f = mFile->getWriteBlockFile(mBlockNr);
size_t written=fwrite(compbuffer, outsize, 1, f);
returnCompBuffer(compbuffer);
#endif
fclose(f);
if (1!=written)
{
DiskException d;
char filename[PATH_MAX];
mFile->getBlockFile(mBlockNr,filename);
d.filename=filename;
d.error="Can't write to file";
throw d;
}
mDirty = false;
return BLOCKSIZE;
}
Block::~Block() {
delete[] data;
}
Block::Block(ABFile *file, int blocknr) :
mDirty(true), mBlockNr(blocknr), dataLen(0), mLastUse(0), mFile(file) {
data = new byte[BLOCKSIZE];
memset(data, 0, BLOCKSIZE);
}
int Block::read() {
if (dataLen) //already loaded
return dataLen;
char filename[PATH_MAX];
mFile->getBlockFile(mBlockNr, filename);
FILE *f = fopen(filename, "rb");
if (f) {
#ifdef NOCOMP
int read = fread(data,1, BLOCKSIZE + 1000, f);
#else
byte *compbuffer = getCompBuffer();
int read = fread(compbuffer, 1, BLOCKSIZE + 1000, f);
decompress(compbuffer, read);
returnCompBuffer(compbuffer);
#endif
fclose(f);
mDirty = false;
} else
mDirty = true;
return dataLen;
}
void Block::decompress(byte *src, int src_len) {
lzo1x_decompress(src, src_len, data, &dataLen, 0);
}