-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
89 lines (69 loc) · 1.63 KB
/
Copy pathFileSystem.cpp
File metadata and controls
89 lines (69 loc) · 1.63 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
#include "FileSystem.h"
#include "Logger.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <sstream>
using namespace std;
std::time_t FileSystem::getLastWriteTime(
const std::string& path)
{
try
{
auto ftime = std::filesystem::last_write_time(path);
auto sctp =
std::chrono::time_point_cast<
std::chrono::system_clock::duration
>(
ftime
- std::filesystem::file_time_type::clock::now()
+ std::chrono::system_clock::now()
);
return std::chrono::system_clock::to_time_t(sctp);
}
catch(...)
{
Logger::error("Read file failed: " + path);
return 0;
}
}
string FileSystem::readRange(
const string& path,
long long start,
long long length)
{
ifstream file(path, ios::binary);
if(!file.is_open())
{
Logger::error("Read file failed: " + path);
return "";
}
file.seekg(start);
string buffer(length, '\0');
file.read(&buffer[0], length);
buffer.resize(file.gcount());
return buffer;
}
long long FileSystem::getFileSize(
const string& path)
{
ifstream file(path, ios::binary | ios::ate);
if(!file.is_open())
{
Logger::error("Read file failed: " + path);
return -1;
}
return file.tellg();
}
string FileSystem::readFile(const string& path)
{
ifstream file(path, ios::binary);
if (!file.is_open())
{
Logger::error("Read file failed: " + path);
return "";
}
stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}