-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgl_filesystem.cpp
More file actions
179 lines (153 loc) · 3.23 KB
/
Copy pathvgl_filesystem.cpp
File metadata and controls
179 lines (153 loc) · 3.23 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
#include "vgl_filesystem.hpp"
#include "vgl_assert.hpp"
#include <iostream>
namespace vgl
{
path::FileDescriptor::FileDescriptor(const char* fname, const char* mode) :
m_fd(fopen(fname, mode))
{
if(!m_fd)
{
std::cerr << "path::fopen(): could not open '" << to_string(*this) << "' using mode '" << mode << "'\n";
}
}
path::FileDescriptor::~FileDescriptor()
{
if(m_fd)
{
fclose(m_fd);
}
}
optional<char> path::FileDescriptor::readChar() const
{
VGL_ASSERT(m_fd);
char cc = static_cast<char>(fgetc(m_fd));
if(feof(m_fd))
{
return nullopt;
}
return cc;
}
optional<uint8_t> path::FileDescriptor::readUnsigned() const
{
VGL_ASSERT(m_fd);
uint8_t cc = static_cast<uint8_t>(fgetc(m_fd));
if(feof(m_fd))
{
return nullopt;
}
return cc;
}
size_t path::FileDescriptor::write(const void* data, size_t len)
{
VGL_ASSERT(m_fd);
return fwrite(data, 1u, len, m_fd);
}
optional<string> path::readToString() const
{
auto fd = openFile("rb");
if(!fd)
{
return nullopt;
}
vector<char> ret;
for(;;)
{
optional<char> cc = fd.readChar();
if(!cc)
{
break;
}
ret.push_back(*cc);
}
return string(ret.data(), ret.size());
}
optional<vector<uint8_t>> path::readToVector() const
{
auto fd = openFile("rb");
if(!fd)
{
return nullopt;
}
vector<uint8_t> ret;
for(;;)
{
optional<uint8_t> cc = fd.readUnsigned();
if(!cc)
{
break;
}
ret.push_back(*cc);
}
return ret;
}
bool path::write(string_view contents) const
{
auto fd = openFile("wb");
if(!fd)
{
return false;
}
size_t bytes_written = fd.write(contents.data(), contents.length());
bool ret = (bytes_written == contents.length());
if(!ret)
{
std::cerr << "path::write(): could only write " << bytes_written << " out of " << contents.length() <<
"bytes requested to '" << to_string(*this) << "'\n";
}
return ret;
}
path find_file(const path& fname)
{
if(fname.exists())
{
return fname;
}
{
path attempt = path("..") / fname;
if(attempt.exists())
{
return attempt;
}
}
{
path attempt = path("src") / fname;
if(attempt.exists())
{
return attempt;
}
attempt = path("..") / attempt;
if(attempt.exists())
{
return attempt;
}
}
{
path attempt = path("rel") / fname;
if(attempt.exists())
{
return attempt;
}
attempt = path("..") / attempt;
if(attempt.exists())
{
return attempt;
}
}
return path();
}
string read_file_locate(string_view fname)
{
path real_path = find_file(fname);
if(real_path.empty())
{
VGL_THROW_RUNTIME_ERROR("read_file_locate(): '" + string(fname) + "' not found");
}
optional<string> contents = real_path.readToString();
if(!contents)
{
VGL_THROW_RUNTIME_ERROR("read_file_locate(): failure reading '" + string(fname) + "'");
}
return *contents;
}
}