-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathFile.cpp
More file actions
46 lines (36 loc) · 932 Bytes
/
File.cpp
File metadata and controls
46 lines (36 loc) · 932 Bytes
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
// Copyright (C) Mihai Preda
#include "File.h"
#include <filesystem>
#include <system_error>
using namespace std;
File::File(const std::filesystem::path& path, const string& mode, bool throwOnError)
: readOnly{mode == "rb"}, name{path.string()} {
assert(readOnly || throwOnError);
f = fopen(name.c_str(), mode.c_str());
if (!f && throwOnError) {
log("Can't open '%s' (mode '%s')\n", name.c_str(), mode.c_str());
throw(fs::filesystem_error("can't open file"s, path, {}));
}
if (mode == "ab") {
assert(f);
#if HAS_SETLINEBUF
setlinebuf(f);
#endif
}
}
File::~File() {
if (!f) { return; }
if (!readOnly) { datasync(); }
fclose(f);
f = nullptr;
}
i64 File::size(const fs::path &name) {
error_code dummy;
return filesystem::file_size(name, dummy);
}
File& File::operator=(File&& other) {
assert(this != &other);
this->~File();
new (this) File(std::move(other));
return *this;
}