-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileProcessor.cpp
62 lines (57 loc) · 1.77 KB
/
FileProcessor.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
#include "FileProcessor.h"
#include "ThreadPool.h"
#include <fstream>
#include <string>
#include <boost/format.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using boost::posix_time::ptime;
using boost::mutex;
using boost::wformat;
using boost::bind;
static wstring humanReadableSize(const int64_t& size)
{
static const int64_t kib = 1024;
static const int64_t mib = kib * kib;
static const int64_t gib = mib * kib;
static const int64_t tib = gib * kib;
if (size < kib)
return str(wformat(L"%1%") % size);
else if (size < mib)
return str(wformat(L"%.2f KB") % (double(size) / kib));
else if (size < gib)
return str(wformat(L"%.2f MB") % (double(size) / mib));
else if (size < tib)
return str(wformat(L"%.2f GB") % (double(size) / gib));
else
return str(wformat(L"%.2f TB") % (double(size) / tib));
}
FileProcessor::FileProcessor(wostream & sink, bool timestamps): m_nextIndex(0), m_logger(sink, timestamps) {}
void FileProcessor::processFileList(std::list<FileItem> & files)
{
files.sort();
m_logger(L"Selection processing started");
ThreadPool pool;
for (auto & file : files)
{
pool.addTask(bind(&FileProcessor::processFile, this, cref(file), m_nextIndex) );
m_nextIndex++;
}
pool.join();
m_logger(L"Selection processing finished");
}
void FileProcessor::processFile(const FileItem & file, size_t number)
{
uint32_t checkSum = calcCheckSum(file);
m_logger(str(wformat(L"File processed\n\tName: %1%\n\tSize: %2%\n\tCreation data: %3%\n\tChecksum: %4%")
% file.name % humanReadableSize(file.size) % file.created_at % checkSum), number);
}
uint32_t FileProcessor::calcCheckSum(const FileItem & fi)
{
uint32_t sum = 0;
ifstream in(fi.full_name, ios::binary);
char c;
while (in.get(c)) sum += static_cast<unsigned char>(c);
return sum;
}