-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
64 lines (54 loc) · 2.18 KB
/
encode.cpp
File metadata and controls
64 lines (54 loc) · 2.18 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
#include <stdexcept>
#include "encode.h"
#include "bzipthread.h"
#include "definitions.h"
using namespace std;
void Encode::afterOpeningFiles()
{
// Если при архивации применять заданный размер блока, то может случиться, что последний блок
// окажется очень маленьким, что плохо скажется на сжатии. Поэтому в этом случае
// мы изменим немого размер блока так, чтобы все блоки стали примерно одинаковой длины.
int delta = _inFileSize % _blockSize;
if (delta < _blockSize / 2 && _inFileSize > _blockSize)
_blockSize += delta / (int)(_inFileSize / (double)_blockSize) + 1;
}
void Encode::threadRun()
{
_threads[_threadNum] = thread(BZIPEncodeThread, ref(_args[_threadNum]), ref(_vars[_threadNum]));
}
void Encode::readData()
{
if (!_inFileSize) {_end = true; return;}
try
{
if (_inFileSize < _blockSize)
_blockSize = _inFileSize;
_args[_threadNum].in = new byte[_blockSize];
size_t readResult;
readResult = fread(_args[_threadNum].in, sizeof(byte), _blockSize, _inFile);
if (readResult != (size_t)_blockSize) throw length_error(_inFileName);
_args[_threadNum].inSize = _blockSize;
_args[_threadNum].codesLengths = new byte[ALPHABET];
_inFileSize -= _blockSize;
if (!_inFileSize) _end = true;
}
catch(...)
{
_error = true;
_pExc = current_exception();
deleteData();
}
}
void Encode::writeData()
{
if (_args[_threadNum].outSize)
{
fwrite(&_args[_threadNum].outSize, sizeof _args[_threadNum].outSize, 1, _outFile);
fwrite(&_args[_threadNum].inSize, sizeof _args[_threadNum].inSize, 1, _outFile);
fwrite(&_args[_threadNum].lastBytePosition, sizeof _args[_threadNum].lastBytePosition, 1, _outFile);
fwrite(_args[_threadNum].codesLengths, sizeof(byte), ALPHABET, _outFile);
fwrite(_args[_threadNum].out, sizeof(byte), _args[_threadNum].outSize, _outFile);
}
deleteData();
}
Encode::~Encode() {}