Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (99 loc) · 2.9 KB
/
Copy pathmain.cpp
File metadata and controls
120 lines (99 loc) · 2.9 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <QCoreApplication>
#include <string>
#include <iostream>
#include <filesystem>
#include <fstream>
#include <thread>
#include <stdexcept>
#include <array>
#include <codecvt>
#include "SimpleTimer.hpp"
#include "toml11\\toml.hpp"
#include "FileMapper.hpp"
constexpr auto countOfThreads = 4;
auto FileSize(std::wstring path) {
return std::filesystem::file_size(path);
}
bool Read(std::wstring path, size_t speed = 1) {
SimpleTimer timer;
size_t pos = 0;
constexpr size_t bufferSize = 1024 * 1024;
size_t readSpeed = speed * bufferSize;
size_t size = FileSize(path);
FileMapper mapper(path, bufferSize);
if(mapper.GetInfoError() != 0)
return false;
std::vector<BYTE> buffer(bufferSize);
if(size <= readSpeed)
readSpeed = size;
timer.Start();
while (pos < size)
{
for(size_t cur = 0; (pos < size) && (cur < readSpeed); cur += bufferSize)
{
if(pos + bufferSize > size) {
if(!mapper.ReadFile(size - pos, buffer))
return false;
}
else {
if(!mapper.ReadFile(pos, buffer))
return false;
}
pos += bufferSize;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000 - timer.Duration()));
timer.ResetTime();
}
std::cout << std::filesystem::path(path).filename() << " has been read" << std::endl;
return true;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::array<std::thread, countOfThreads> threads;
size_t speed;
toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> arrayOfPath;
std::vector<std::wstring> paths;
std::string path;
try {
const auto file = toml::parse("..\\Info.toml");
const auto data = toml::find(file, "path");
arrayOfPath = toml::find(data, "directory");
speed = toml::find<size_t>(data, "speed");
for(size_t i = 0; i < arrayOfPath.size(); i++)
{
path = toml::find<std::string>(arrayOfPath, i);
if(!std::filesystem::is_directory(path) && std::filesystem::exists(path))
paths.push_back(converter.from_bytes(path));
}
}
catch(toml::exception& ex)
{
std::cout << ex.what() << std::endl;
return -1;
}
size_t size = paths.size();
size_t count = 0;
while(size/countOfThreads != 0)
{
for(auto& th : threads)
{
th = std::thread(Read, paths[count++], speed);
}
size -= countOfThreads;
}
if(count >= countOfThreads)
for(auto& x : threads) {
x.join();
}
for(size_t i = 0; i < size; i++)
{
threads[i] = std::thread(Read, paths[count++], speed);
}
for(auto& x : threads) {
if(x.joinable())
x.join();
}
return a.exec();
}