载入中...
搜索中...
未找到
File.cpp
浏览该文件的文档.
1
3
4#include <cstring>
5#include <utility>
6
7#include "common/Exception.h"
10
11namespace eve {
12namespace filesystem {
13
14static bool hack_setupWriteDirectory() {
15 if (auto* f = getModInst(filesystem,Filesystem))
16 return f->setupWriteDirectory();
17 return false;
18}
19
20namespace physfs {
21
22File::File(std::string filename)
23 : filename(std::move(filename)), file(nullptr), mode("c"), bufferMode("none"), bufferSize(0) {}
24
26 if (mode != "c") close();
27}
28
29bool File::open(std::string requestedMode) {
30 if (requestedMode == "c") return true;
31
32 // Love-style aliases → PhysFS binary modes.
33 if (requestedMode == "r") requestedMode = "rb";
34 else if (requestedMode == "w") requestedMode = "wb";
35 else if (requestedMode == "a") requestedMode = "ab";
36
37 if (!PHYSFS_isInit()) throw Exception("PhysFS is not initialized.");
38
39 // File must exist if read mode.
40 if ((requestedMode == "rb") && !PHYSFS_exists(filename.c_str()))
41 throw Exception("Could not open file %s. Does not exist.", filename.c_str());
42
43 // Check whether the write directory is set.
44 if ((requestedMode == "ab" || requestedMode == "wb") &&
45 (PHYSFS_getWriteDir() == nullptr) && !hack_setupWriteDirectory())
46 throw Exception("Could not set write directory.");
47
48 // File already open?
49 if (file != nullptr) return false;
50
51 PHYSFS_getLastErrorCode();
52 PHYSFS_File *handle = nullptr;
53
54 if (requestedMode == "rb") handle = PHYSFS_openRead(filename.c_str());
55 else if (requestedMode == "ab") handle = PHYSFS_openAppend(filename.c_str());
56 else if (requestedMode == "wb") handle = PHYSFS_openWrite(filename.c_str());
57 else
58 throw Exception("Invalid file mode %s.", requestedMode.c_str());
59
60 if (handle == nullptr) {
61 const char *err = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());
62 if (err == nullptr) err = "unknown error";
63 throw Exception("Could not open file %s (%s)", filename.c_str(), err);
64 }
65
66 file = handle;
67
68 this->mode = requestedMode;
69
70 if (file != nullptr && !setBuffer(bufferMode, bufferSize)) {
71 // Revert to buffer defaults if we don't successfully set the buffer.
72 bufferMode = "none";
73 bufferSize = 0;
74 }
75
76 return (file != nullptr);
77}
78
80 if (file == nullptr || !PHYSFS_close(file)) return false;
81
82 mode = "c";
83 file = nullptr;
84
85 return true;
86}
87
88bool File::isOpen() const { return mode != "c" && file != nullptr; }
89
90int64_t File::getSize() {
91 // If the file is closed, open it to
92 // check the size.
93 if (file == nullptr) {
94 open("rb");
95 int64_t size = (int64_t)PHYSFS_fileLength(file);
96 close();
97 return size;
98 }
99
100 return (int64_t)PHYSFS_fileLength(file);
101}
102
103int64_t File::read(void *dst, int64_t size) {
104 if (!file || mode != "rb") throw Exception("File is not opened for reading.");
105
106 int64_t max = (int64_t)PHYSFS_fileLength(file);
107 int64_t cur = (int64_t)PHYSFS_tell(file);
108 if (max < 0 || cur < 0) throw Exception("Could not read from file.");
109 if (cur > max) return 0;
110
111 int64_t remaining = max - cur;
112 size = (size == ALL) ? remaining : size;
113 if (size > remaining) size = remaining;
114
115 if (size < 0) throw Exception("Invalid read size.");
116
117 return PHYSFS_readBytes(file, dst, (PHYSFS_uint64)size);
118}
119
120bool File::write(const void *data, int64_t size) {
121 if (!file || (mode != "wb" && mode != "ab")) throw Exception("File is not opened for writing.");
122
123 if (size < 0) throw Exception("Invalid write size.");
124
125 // Try to write.
126 int64_t written = PHYSFS_writeBytes(file, data, (PHYSFS_uint64)size);
127
128 // Check that correct amount of data was written.
129 if (written != size) return false;
130
131 // Manually flush the buffer in BUFFER_LINE mode if we find a newline.
132 if (bufferMode == "newline" && bufferSize > size) {
133 if (memchr(data, '\n', (size_t)size) != nullptr) flush();
134 }
135
136 return true;
137}
138
140 if (!file || (mode != "wb" && mode != "ab")) throw Exception("File is not opened for writing.");
141
142 return PHYSFS_flush(file) != 0;
143}
144
145#ifdef EVENGINE_WINDOWS
146inline bool test_eof(File *f, PHYSFS_File *) {
147 int64_t pos = f->tell();
148 int64_t size = f->getSize();
149 return pos == -1 || size == -1 || pos >= size;
150}
151#else
152inline bool test_eof(File *, PHYSFS_File *file) { return PHYSFS_eof(file); }
153#endif
154
155bool File::isEOF() { return file == nullptr || test_eof(this, file); }
156
157int64_t File::tell() {
158 if (file == nullptr) return -1;
159
160 return (int64_t)PHYSFS_tell(file);
161}
162
163bool File::seek(uint64_t pos) { return file != nullptr && PHYSFS_seek(file, (PHYSFS_uint64)pos) != 0; }
164
165bool File::setBuffer(std::string bufmode, int64_t size) {
166 if (size < 0) return false;
167
168 // If the file isn't open, we'll make sure the buffer values are set in
169 // File::open.
170 if (!isOpen()) {
171 bufferMode = bufmode;
172 bufferSize = size;
173 return true;
174 }
175
176 int ret = 1;
177 if (bufferMode == "newline" || bufferMode == "full") {
178 ret = PHYSFS_setBuffer(file, size);
179 } else {
180 ret = PHYSFS_setBuffer(file, 0);
181 size = 0;
182 }
183
184 if (ret == 0) return false;
185
186 bufferMode = bufmode;
187 bufferSize = size;
188
189 return true;
190}
191
192std::string File::getBuffer(int64_t &size) const {
193 size = bufferSize;
194 return bufferMode;
195}
196
197std::string File::getFilename() const { return filename; }
198
199std::string File::getMode() const { return mode; }
200
201} // namespace physfs
202} // namespace filesystem
203} // namespace eve
#define getModInst(N, T)
Definition Module.h:40
float f
PrimitiveHandle handle
std::string file
float size
Definition TreeMesh.cpp:158
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
static const int64_t ALL
Used to indicate ALL data in a file.
Definition File.h:19
File public API.
Definition File.h:17
bool isEOF() override
True when eof.
Definition File.cpp:155
std::string getFilename() const override
Full filename including extension.
Definition File.cpp:197
std::string getMode() const override
Returns the mode.
Definition File.cpp:199
bool close() override
Closes close.
Definition File.cpp:79
bool open(std::string mode) override
Opens open.
Definition File.cpp:29
bool seek(uint64_t pos) override
Seeks seek.
Definition File.cpp:163
bool setBuffer(std::string bufmode, int64_t size) override
Sets the buffer.
Definition File.cpp:165
virtual ~File()
Releases File resources.
Definition File.cpp:25
File(std::string filename)
Constructs an File with the given ilename.
Definition File.cpp:22
bool write(const void *data, int64_t size) override
Writes write.
Definition File.cpp:120
int64_t read(void *dst, int64_t size) override
Reads read.
Definition File.cpp:103
bool flush() override
Flushes flush.
Definition File.cpp:139
int64_t getSize() override
Byte length of the owned buffer.
Definition File.cpp:90
int64_t tell() override
Returns the current tell.
Definition File.cpp:157
bool isOpen() const override
True when open.
Definition File.cpp:88
std::string getBuffer(int64_t &size) const override
Returns the buffer.
Definition File.cpp:192
bool test_eof(File *, PHYSFS_File *file)
Definition File.cpp:152
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16