-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.h
More file actions
275 lines (218 loc) · 8.33 KB
/
File.h
File metadata and controls
275 lines (218 loc) · 8.33 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef JUL_FILE_H
#define JUL_FILE_H
/*
MIT License
Copyright(c) 2019 Julian Steigerwald
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright noticeand this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
#include <vector>
namespace jul
{
// -------------------------------------------------------------------------------------
// Copy a file. This function will not throw like std::filesystem::copy_file but just
// return 'false' on a missing file / directory.
// Depending on the local std::ifstream/ofstream implementation this function should
// be non-blocking on the file to copy (fingers crossed)
// -------------------------------------------------------------------------------------
inline bool copy_file(const std::string& src_name, const std::string& dst_name)
{
assert(!src_name.empty());
assert(!dst_name.empty());
std::ifstream src(src_name, std::ios::binary);
if (!src.is_open()) { return false; }
std::ofstream dst(dst_name, std::ios::binary);
if (!dst.is_open()) { return false; }
dst << src.rdbuf();
return true;
}
// -------------------------------------------------------------------------------------
// Parse a file line by line. Fails silent on missing files by just returning an empty vector
// -------------------------------------------------------------------------------------
inline std::vector<std::string> file_to_lines(const std::string& file_name)
{
assert(!file_name.empty());
std::ifstream file(file_name);
std::vector<std::string> lines{};
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
return lines;
}
// -------------------------------------------------------------------------------------
// Parse a file line by line. Apply a 'Function' to each line.
// -------------------------------------------------------------------------------------
template <class Function>
void for_each_line(const std::string& file_name, Function&& fn)
{
assert(!file_name.empty());
std::ifstream file(file_name);
std::string line;
while (std::getline(file, line)) {
fn(line);
}
}
// -------------------------------------------------------------------------------------
// Does a file exist?
// -------------------------------------------------------------------------------------
inline bool file_exists(const char* file_name)
{
std::FILE* f = std::fopen(file_name, "r");
if (f != nullptr) {
std::fclose(f);
return true;
}
return false;
}
// -------------------------------------------------------------------------------------
// File (class): Wrapper class for c file functions.
// -------------------------------------------------------------------------------------
class File {
public:
using Bytes = std::size_t;
enum class Mode {
Read, // Open a file for reading from start, fails on non exisiting file.
Write, // Create or overwrite file.
Append, // Append to a file or create new if the file doesn't exist.
Read_extended, // Open a file for read and write, fail on non existing file.
Write_extended, // Create a file for read and write (or overwrite).
Append_extended // Append to a file or create new if the file doesn't exist. File is open for read/write
};
enum class Position {
Beginning = SEEK_SET, // File beginning.
Current = SEEK_CUR, // Current stream position.
End = SEEK_END // File end.
};
File() {}
~File() { close(); }
// no copy & move
File(File&&) = delete;
File(const File&) = delete;
File& operator=(File&&) = delete;
File& operator=(const File&) = delete;
bool open(const char* file_name, Mode mode)
{
const auto m = mode_to_string(mode);
m_handle = std::fopen(file_name, m);
return m_handle != nullptr;
}
std::size_t read(void* buffer, Bytes element_size, std::size_t element_count)
{
assert(m_handle);
assert(buffer);
assert(element_size > 0);
return std::fread(buffer, element_size, element_count, m_handle);
}
template <class T>
std::size_t read(std::vector<T>& buffer)
{
assert(m_handle);
assert(std::size(buffer) > 0);
return std::fread(&buffer[0], sizeof(T), std::size(buffer), m_handle);
}
std::size_t write(const void* buffer, Bytes element_size, std::size_t element_count)
{
assert(m_handle);
assert(buffer);
assert(element_size > 0);
return std::fwrite(buffer, element_size, element_count, m_handle);
}
template <class T>
std::size_t write(const std::vector<T>& buffer)
{
assert(m_handle);
assert(std::size(buffer) > 0);
return std::fwrite(buffer.data(), sizeof(T), std::size(buffer), m_handle);
}
int error() const
{
assert(m_handle);
return std::ferror(m_handle);
}
// reads the file size in bytes, will set the stream position to the beginning!
Bytes file_size()
{
seek(0, Position::End);
Bytes size = tell();
seek(0, Position::Beginning);
return size;
}
bool seek(long offset, Position pos)
{
assert(m_handle);
return std::fseek(m_handle, offset, (int) pos) == 0;
}
long tell()
{
assert(m_handle);
return std::ftell(m_handle);
}
// Moves the file position indicator to the beginning of the given file stream.
// The function is equivalent to std::fseek(stream, 0, SEEK_SET);, except that end - of - file and error indicators are cleared.
void rewind()
{
assert(m_handle);
::rewind(m_handle);
}
void close()
{
if (m_handle) {
std::fclose(m_handle);
m_handle = nullptr;
}
}
bool eof() const
{
assert(m_handle);
return std::feof(m_handle) != 0;
}
bool flush() const
{
assert(m_handle);
return std::fflush(m_handle) == 0;
}
std::FILE* handle() { return m_handle; }
private:
std::FILE* m_handle = nullptr;
constexpr const char* mode_to_string(Mode mode)
{
switch (mode)
{
case Mode::Read:
return "r";
case Mode::Write:
return "w";
case Mode::Append:
return "a";
case Mode::Read_extended:
return "r+";
case Mode::Write_extended:
return "w+";
case Mode::Append_extended:
return "a+";
default:
assert(false);
}
return "";
}
};
}
#endif // JUL_FILE_H