-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.cpp
66 lines (55 loc) · 2.13 KB
/
decode.cpp
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
#include <stdexcept>
#include "decode.h"
#include "bzipthread.h"
#include "definitions.h"
using namespace std;
void Decode::afterOpeningFiles()
{
// Ничего не делает.
}
void Decode::threadRun()
{
_threads[_threadNum] = thread(BZIPDecodeThread, ref(_args[_threadNum]), ref(_vars[_threadNum]));
}
void Decode::readData()
{
if (!_inFileSize) {_end = true; return;}
try
{
size_t readResult;
readResult = fread(&_args[_threadNum].inSize, sizeof _args[_threadNum].inSize, 1, _inFile);
if (readResult != 1) throw length_error(_inFileName);
readResult = fread(&_args[_threadNum].outSize, sizeof _args[_threadNum].outSize, 1, _inFile);
if (readResult != 1) throw length_error(_inFileName);
readResult = fread(&_args[_threadNum].lastBytePosition, sizeof _args[_threadNum].lastBytePosition, 1, _inFile);
if (readResult != 1) throw length_error(_inFileName);
if (_args[_threadNum].inSize && _args[_threadNum].outSize)
{
_args[_threadNum].codesLengths = new byte[ALPHABET];
readResult = fread(_args[_threadNum].codesLengths, sizeof(byte), ALPHABET, _inFile);
if (readResult != ALPHABET) throw length_error(_inFileName);
_args[_threadNum].in = new byte[_args[_threadNum].inSize];
readResult = fread(_args[_threadNum].in, sizeof(byte), _args[_threadNum].inSize, _inFile);
if (readResult != (size_t)_args[_threadNum].inSize) throw length_error(_inFileName);
_args[_threadNum].out = new byte[_args[_threadNum].outSize];
}
_inFileSize -= sizeof _args[_threadNum].inSize + sizeof _args[_threadNum].outSize
+ sizeof _args[_threadNum].lastBytePosition + ALPHABET*sizeof(byte) + _args[_threadNum].inSize*sizeof(byte);
if (!_inFileSize) _end = true;
}
catch(...)
{
_error = true;
_pExc = current_exception();
deleteData();
}
}
void Decode::writeData()
{
if (_args[_threadNum].outSize)
{
fwrite(_args[_threadNum].out, sizeof(byte), _args[_threadNum].outSize, _outFile);
}
deleteData();
}
Decode::~Decode() {}