-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.hpp
222 lines (190 loc) · 4.25 KB
/
File.hpp
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
#pragma once
#include <cstdio>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
#if __unix__ || __linux__ || __APPLE__
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#else
#include <io.h>
#include <windows.h>
#endif
#if !defined(_POSIX_VERSION)
#include <direct.h>
#endif
namespace sys {
struct Path {
const std::string p;
inline explicit Path(const std::string &&p):
p(p)
{}
inline explicit Path(const std::string &p):
p(p)
{}
#if defined _WIN32 || defined __CYGWIN__
static constexpr char separator = '/';//'\\';
#else
static constexpr char separator = '/';
#endif
inline operator std::string() const noexcept {
return p;
}
Path operator/(const Path &pp) const {
std::string s = p;
std::string ss = pp;
if(s.length() && s.back() == separator) {
return Path(s + ss);
}
return Path(s + separator + ss);
}
};
std::string get_cwd() {
#if defined(_POSIX_VERSION)
char buf[PATH_MAX + 1];
getcwd(buf, PATH_MAX);
#else
char buf[MAX_PATH + 1];
_getcwd(buf, MAX_PATH);
#endif
return buf;
}
std::string get_executable_directory(int argc, char *argv[]) {
#ifdef __linux__
std::vector<char> buf(PATH_MAX);
readlink("/proc/self/exe", buf.data(), buf.size());
const std::string exec(buf.begin(), buf.end());
#elif !defined(_POSIX_VERSION)
// windows
std::vector<char> buf(MAX_PATH);
GetModuleFileName(nullptr, buf.data(), MAX_PATH);
const std::string exec(buf.begin(), buf.end());
#else
std::string x = "";
if(argv[0][0] == '/') {
x = argv[0];
}
const std::string exec = x;
#endif
#if 0
#if defined _WIN32 || defined __CYGWIN__
#define FILE_SEPARATOR '\\'
#else
#define FILE_SEPARATOR '/'
#endif
#endif
std::string::size_type n = exec.rfind('/');
std::string folder;
if(n == std::string::npos) {
folder = "./";
} else {
folder = exec.substr(0, n + 1);
}
return folder;
}
class File {
std::string filename;
public:
class Lock {
int fd;
bool dropped = true;
public:
Lock(FILE *file):
#if defined(_POSIX_VERSION)
fd(fileno(file))
#else
fd(0)
#endif
{
#if defined(_POSIX_VERSION)
assert(File::is_open(fd));
if(flock(fd, LOCK_EX) < 0) {
perror("flock[ex]:");
abort();
}
#endif
dropped = false;
}
void drop() noexcept {
#if defined(_POSIX_VERSION)
if(flock(fd, LOCK_UN) < 0) {
perror("flock[un]:");
abort();
}
#endif
dropped = true;
}
~Lock() {
if(!dropped) {
drop();
}
}
};
public:
explicit File(const char *filename):
filename(filename)
{}
size_t length() {
/* struct stat st; */
/* stat(filename.c_str(), &st); */
/* return st.st_size; */
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
const std::string &name() const {
return filename;
}
std::string name() {
return filename;
}
#if defined(_POSIX_VERSION)
static bool is_open(FILE *file) {
return sys::File::is_open(fileno(file));
}
static bool is_open(int fd) {
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
#endif
bool is_ext(const std::string &&ext) {
if(ext.length() > filename.length())
return false;
size_t f=filename.length(),e=ext.length();
return filename.substr(f-e, e) == ext;
}
bool exists() {
/* return access(filename.c_str(), F_OK) != -1; */
return bool(std::ifstream(filename));
}
static void truncate(const std::string &filename) {
fclose(fopen(filename.c_str(), "w+"));
}
std::string load_text() {
size_t size = length() + 1;
std::string text;
text.resize(size);
FILE *file = fopen(filename.c_str(), "r");
if(file == nullptr) {
fprintf(stderr, "unable to open file '%s' for reading\n", filename.c_str());
abort();
}
#if defined(_POSIX_VERSION)
assert(sys::File::is_open(file));
#endif
sys::File::Lock fl(file);
int i = 0;
while((text[i] = fgetc(file)) != EOF)
++i;
text[i] = '\0';
fl.drop();
fclose(file);
return text;
}
inline operator Path() const noexcept {
return Path(filename);
}
};
} // namespace sys