-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileprocessing.cpp
237 lines (190 loc) · 6.34 KB
/
fileprocessing.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "stdafx.h"
#include "fileprocessing.h"
namespace
{
const char* const fio_errors[] =
{
"Failed to open file." // 0
, "Failed to write a file." // 1
, "Failed to read a file." // 2
, "Failed to prepare a file for IO operation." // 3
};
};
namespace bpatch
{
using namespace std;
using namespace std::filesystem;
FileProcessing::FileProcessing(const char* fname, const char* mode)
{
#if !defined(__linux__) && !(defined(__APPLE__) && defined(__MACH__))
#pragma warning(disable: 4996) // I would like to use fopen instead of fopen_s, because
// 1. I would like to write fast code
// 2. MSVS compiler does not respect C++ standard and __STDC_LIB_EXT1__ define
#endif
if (stream_ = fopen(fname, mode); nullptr == stream_)
#if !defined(__linux__) && !(defined(__APPLE__) && defined(__MACH__))
#pragma warning(default: 4996)
#endif
{
throw filesystem_error(fio_errors[0], filesystem::path(fname), error_code());
}
buff_.resize(SZBUFF_FC);
if (setvbuf(stream_, buff_.data(), _IOFBF, SZBUFF_FC) != 0)
{
std::cout << coloredconsole::toconsole("Warning: buffering for file processing has not been initialized.") << std::endl;
}
}
FileProcessing::~FileProcessing()
{
if (stream_)
{
fflush(stream_);
fclose(stream_);
}
stream_ = nullptr;
}
ReadFileProcessing::ReadFileProcessing(const char* fname, const char* mode)
: FileProcessing(fname, mode)
{
}
span<char> ReadFileProcessing::ReadData(const span<char> place)
{
#ifdef __linux__
const size_t readed = fread_unlocked(place.data(), sizeof(*place.data()), place.size(), stream_);
#elif defined(__APPLE__) && defined(__MACH__)
const size_t readed = fread(place.data(), sizeof(*place.data()), place.size(), stream_);
#else
const size_t readed = _fread_nolock(place.data(), sizeof(*place.data()), place.size(), stream_);
#endif
readedAmount_ += readed;
eof_ = feof(stream_) != 0;
if (readed < place.size() && !eof_)
{
throw filesystem_error(fio_errors[2], error_code());
}
return span(place.data(), readed);
}
//------------------------------------------------------
WriteFileProcessing::WriteFileProcessing(const char* fname, const char* mode)
: FileProcessing(fname, mode)
, cache_(new FlexibleCache)
{
}
size_t WriteFileProcessing::WriteCharacter(const char toProcess, const bool aEod)
{
if (!aEod)
{
cache_->Accumulate(toProcess);
}
return WriteEverythingOrFullChunks(aEod);
}
size_t WriteFileProcessing::Written() const noexcept
{
return writeAt_;
}
size_t WriteFileProcessing::WriteAndThrowIfFail(const string_view sv)
{
#ifdef __linux__
const size_t written = fwrite_unlocked(sv.data(), sizeof(sv.data()[0]), sv.size(), stream_);
#elif defined(__APPLE__) && defined(__MACH__)
const size_t written = fwrite(sv.data(), sizeof(sv.data()[0]), sv.size(), stream_);
#else
const size_t written = _fwrite_nolock(sv.data(), sizeof(sv.data()[0]), sv.size(), stream_);
#endif
if (written != sv.size())
{
throw filesystem_error(fio_errors[1], error_code());
};
writeAt_ += written;
return written;
}
size_t WriteFileProcessing::WriteEverythingOrFullChunks(const bool aEod)
{
size_t written = 0;
while (aEod || cache_->RootChunkFull())
{
unique_ptr<FlexibleCache::Chunk> chunk;
const bool dataRemain = cache_->Next(chunk);
written += WriteAndThrowIfFail(string_view(chunk->data, chunk->accumulated));
if (!dataRemain) // no more data: leave the loop
break;
}
return written;
}
ReadWriteFileProcessing::ReadWriteFileProcessing(const char* fname, const char* mode)
: FileProcessing(fname, mode)
, ReadFileProcessing(fname, mode)
, WriteFileProcessing(fname, mode)
{
}
span<char> ReadWriteFileProcessing::ReadData(const span<char> place)
{
SeekSet(true);// reading
return ReadFileProcessing::ReadData(place);
}
size_t ReadWriteFileProcessing::WriteCharacter(const char toProcess, const bool aEod)
{
SeekSet(false); // writing
if (aEod)
{
// end of data - need to write everything
return WriteFileProcessing::WriteEverythingOrFullChunks(aEod);
}
bool chunkAccumulated = cache_->Accumulate(toProcess);
if (!chunkAccumulated)
{
return 0;
}
size_t writtenRet = 0;
size_t maxToWrite = FileReaded() ? SZBUFF_FC : readedAmount_ - Written();
while (maxToWrite >= SZBUFF_FC && chunkAccumulated)
{
unique_ptr<FlexibleCache::Chunk> chunk;
cache_->Next(chunk);
writtenRet += WriteAndThrowIfFail(string_view(chunk->data, chunk->accumulated));
// get status of next chunk if it is accumulted
chunkAccumulated = cache_->RootChunkFull();
maxToWrite = FileReaded() ? SZBUFF_FC : readedAmount_ - Written();
};
return writtenRet;
}
void ReadWriteFileProcessing::SeekSet(const bool bReading)
{
if (const int ret = fseek(stream_,
static_cast<long>(bReading ? readedAmount_ : Written()),
SEEK_SET); ret != 0)
{
throw filesystem_error(fio_errors[3], error_code());
};
}
bool ReadFullFile(std::vector<char>& readTo, const char* const fname, const std::filesystem::path& additionalPath)
{
namespace fs = std::filesystem;
using namespace std;
fs::path aName(fname);
error_code ec;
fs::file_status s = fs::status(aName, ec);
if (!fs::exists(s) || fs::is_directory(s))
{ // search file, not folder
aName = additionalPath / aName;
s = fs::status(aName, ec);
if (!fs::exists(s) || fs::is_directory(s)) // search file, not folder in the additional path
{
return false; // file not found
}
}
// get file size returns error
const size_t szFile = static_cast<size_t>(filesystem::file_size(aName, ec));
if (ec)
{ // error is not empty
cout << coloredconsole::toconsole("Warning: Detection of the size of a file '") << aName << "' returned error '"
<< ec.message() << "'" << endl;
}
// resize vector to accumulate data
readTo.resize(szFile);
ReadFileProcessing readFile(aName.string().c_str());
// read the data
const auto readedSpan = readFile.ReadData(span(readTo.data(), szFile));
return readedSpan.size() == szFile;
}
};// namespace bpatch