-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.cpp
64 lines (55 loc) · 1.65 KB
/
reader.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
#include "reader.h"
#include "signature.h"
#include <iostream>
#include <cstring>
#include <sys/stat.h>
Reader::Reader(const string &filename, int64_t block_size):filename_(filename), block_size_(block_size)
{
}
int Reader::startReader(const string &outname)
{
in.open(filename_, ios::in | ios::binary);
if (!in) {
throw runtime_error("Can't open input file");
}
struct stat st;
if(stat(filename_.c_str(), &st) < 0) {
throw runtime_error("stat failed");
}
last_section_size_ = block_size_;
int64_t filesize = st.st_size;
cout << "Filesize: " << filesize << endl;
blocks_count_ = filesize/block_size_;
int64_t remaining_part = filesize % block_size_;
if(remaining_part) {
blocks_count_++;
last_section_size_ = remaining_part;
}
cout << "Blocks count: " << blocks_count_ << endl;
Signature signature(outname, blocks_count_);
try {
for(int64_t i = 0; i < blocks_count_; i++) {
SignatureData data(new block_data(block_size_, i));
readBlock(data->get_data(), data->get_block_num());
signature.appendData(move(data));
}
} catch (...) {
signature.join();
rethrow_exception(current_exception());
}
signature.join();
}
bool Reader::readBlock(char *buffer, int64_t block_num)
{
in.seekg(block_num*block_size_, ios::beg);
//check for last section
if(block_num+1 != blocks_count_) {
in.read(buffer, block_size_);
} else {
memset(buffer, 0, block_size_);
in.read(buffer, last_section_size_);
}
if (!in)
throw runtime_error("Error reading file");
return true;
}