-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataoperation_file.cpp
172 lines (151 loc) · 4.63 KB
/
dataoperation_file.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "dataoperation_file.h"
#include <fstream>
#include <dirent.h>
using namespace std;
DataHandlerLoadOperationFile::DataHandlerLoadOperationFile(std::string filename, int startOffset, int dataSize)
: m_Filename(std::move(filename))
, m_StartOffset(startOffset)
, m_DataSize(dataSize)
{
}
bool DataHandlerLoadOperationFile::DoLoad(std::vector<char>& outData)
{
ifstream ifile(m_Filename.c_str());
if(!ifile.is_open())
return false;
outData.resize(m_DataSize);
ifile.seekg(m_StartOffset, ifile.beg);
ifile.read(outData.data(), m_DataSize);
return true;
}
FileDataOperationManager::FileDataOperationManager(std::string path, std::vector<char> filePrefix, std::vector<char> filePosfix)
: m_FilePrefix(std::move(filePrefix))
, m_FilePostfix(std::move(filePosfix))
, m_DirPath(std::move(path))
{
}
std::vector<std::string> FileDataOperationManager::GetDataList(std::regex regex, bool checkNotation) const
{
vector<string> files;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(m_DirPath.c_str())) != NULL)
{
while ((ent = readdir(dir)) != NULL)
{
if(ent->d_type == DT_REG)
{
if(regex_match(ent->d_name, regex))
{
files.emplace_back(ent->d_name);
}
}
}
closedir(dir);
}
else
{
// TODO: add error/warning message here.
}
if(checkNotation)
{
for(int i = 0; i < files.size(); ++i)
{
auto& file = files[i];
while(!IsFileReadyToLoad(file) && i < files.size())
{
files[i] = files.back();
files.resize(files.size() - 1);
}
}
}
return files;
}
std::unique_ptr<DataHandler> FileDataOperationManager::GetDataHandler(std::string id) const
{
unique_ptr<DataHandler> fileDataResult;
std::string& filename = id;
int dataSize = GetFileDataSize(filename);
if(dataSize >= 0)
{
auto fileOperation = new DataHandlerLoadOperationFile(std::move(m_DirPath + filename), m_FilePrefix.size(), dataSize);
fileDataResult = make_unique<DataHandler>(fileOperation);
}
return fileDataResult;
}
int FileDataOperationManager::GetFileDataSize(const std::string& filename) const
{
ifstream file((m_DirPath + filename).c_str());
if(!file.is_open())
return -1;
const int prefixSize = m_FilePrefix.size();
const int postfixSize = m_FilePostfix.size();
int minFileSize = prefixSize + postfixSize;
int dataSize = 0;
if(minFileSize > 0)
{
file.seekg(0, file.end);
const int fileSize = (int)file.tellg();
if(minFileSize > fileSize)
return -1;
dataSize = fileSize - prefixSize - postfixSize;
}
if(prefixSize > 0)
{
std::vector<char> buffer(prefixSize);
file.seekg(0, file.beg);
file.read(buffer.data(), prefixSize);
if(buffer != m_FilePrefix)
return -1;
}
if(postfixSize > 0)
{
std::vector<char> buffer(postfixSize);
file.seekg(-postfixSize, file.end);
file.read(buffer.data(), postfixSize);
if(buffer != m_FilePostfix)
return -1;
}
return dataSize;
}
bool FileDataOperationManager::IsFileReadyToLoad(const std::string& filename) const
{
// GetFileDataSize will add the path
return GetFileDataSize(filename) >= 0;
}
std::unique_ptr<DataHandler> FileDataOperationManager::StoreData(std::string id, std::vector<char>&& data, bool forceOverwrite)
{
std::string filename = m_DirPath + id;
std::unique_ptr<DataHandler> result;
bool fileExisted = false;
{
ifstream ifile(filename.c_str());
fileExisted = ifile.is_open();
}
if(fileExisted && !forceOverwrite)
{
// TODO: warning file existed and not force overwrite
return result;
}
ofstream ofile(filename.c_str());
if(!ofile.is_open())
{
// TODO: error cannot write to file
return result;
}
ofile.write(m_FilePrefix.data(), m_FilePrefix.size());
ofile.write(data.data(), data.size());
ofile.write(m_FilePostfix.data(), m_FilePostfix.size());
ofile.flush();
auto fileOperation = new DataHandlerLoadOperationFile(std::move(filename), m_FilePrefix.size(), data.size());
result = make_unique<DataHandler>(fileOperation, std::move(data));
if(fileExisted)
{
// TODO: warn that existing file is overwritten
}
return result;
}
bool FileDataOperationManager::DeleteData(const std::string& id)
{
return std::remove((m_DirPath + id).c_str()) == 0;
}