载入中...
搜索中...
未找到
File.cpp
浏览该文件的文档.
2
3#include <utility>
4
5#include "common/Exception.h"
6
7// Assume POSIX or Visual Studio.
8#include <sys/stat.h>
9#include <sys/types.h>
10
11#include "common/utf8.h"
12#ifdef EVENGINE_WINDOWS
13#include <wchar.h>
14#else
15#include <unistd.h> // POSIX.
16#endif
17
18namespace eve {
19namespace filesystem {
20namespace cppfs {
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 newmode) {
30 if (newmode == "c") return true;
31
32 // File already open?
33 if (file != nullptr) return false;
34
35#ifdef EVENGINE_WINDOWS
36 // make sure non-ASCII filenames work.
37 std::wstring wnewmode = to_widestr(newmode);
38 std::wstring wfilename = to_widestr(filename);
39
40 file = _wfopen(wfilename.c_str(), wnewmode.c_str());
41#else
42 file = fopen(filename.c_str(), newmode.c_str());
43#endif
44
45 if (newmode.find('c') != std::string::npos && file == nullptr)
46 throw Exception("Could not open file %s. Does not exist.", filename.c_str());
47
48 mode = newmode;
49
50 if (file != nullptr && !setBuffer(bufferMode, bufferSize)) {
51 // Revert to buffer defaults if we don't successfully set the buffer.
52 bufferMode = "none";
53 bufferSize = 0;
54 }
55
56 return file != nullptr;
57}
58
60 if (file == nullptr || fclose(file) != 0) return false;
61
62 mode = "c";
63 file = nullptr;
64
65 return true;
66}
67
68bool File::isOpen() const { return mode != "c" && file != nullptr; }
69
70int64_t File::getSize() {
71 int fd = file ? fileno(file) : -1;
72
73#ifdef EVENGINE_WINDOWS
74
75 struct _stat64 buf;
76
77 if (fd != -1) {
78 if (_fstat64(fd, &buf) != 0) return -1;
79 } else {
80 // make sure non-ASCII filenames work.
81 std::wstring wfilename = to_widestr(filename);
82
83 if (_wstat64(wfilename.c_str(), &buf) != 0) return -1;
84 }
85
86 return (int64_t)buf.st_size;
87
88#else
89
90 // Assume POSIX support...
91 struct stat buf;
92
93 if (fd != -1) {
94 if (fstat(fd, &buf) != 0) return -1;
95 } else if (stat(filename.c_str(), &buf) != 0)
96 return -1;
97
98 return (int64_t)buf.st_size;
99
100#endif
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 if (size < 0) throw Exception("Invalid read size.");
107
108 size_t read = fread(dst, 1, (size_t)size, file);
109
110 return (int64_t)read;
111}
112
113bool File::write(const void *data, int64_t size) {
114 if (!file || (mode != "wb" && mode != "ab")) throw Exception("File is not opened for writing.");
115
116 if (size < 0) throw Exception("Invalid write size.");
117
118 int64_t written = (int64_t)fwrite(data, 1, (size_t)size, file);
119
120 return written == size;
121}
122
124 if (!file || (mode != "wb" && mode != "ab")) throw Exception("File is not opened for writing.");
125
126 return fflush(file) == 0;
127}
128
129bool File::isEOF() { return file == nullptr || tell() >= getSize(); }
130
131int64_t File::tell() {
132 if (file == nullptr) return -1;
133
134#ifdef EVENGINE_WINDOWS
135 return (int64_t)_ftelli64(file);
136#else
137 return (int64_t)ftello(file);
138#endif
139}
140
141bool File::seek(uint64_t pos) {
142 if (file == nullptr) return false;
143
144#ifdef EVENGINE_WINDOWS
145 return _fseeki64(file, (int64_t)pos, SEEK_SET) == 0;
146#else
147 return fseeko(file, (off_t)pos, SEEK_SET) == 0;
148#endif
149}
150
151bool File::setBuffer(std::string bufmode, int64_t size) {
152 if (size < 0) return false;
153
154 if (bufmode == "none") size = 0;
155
156 // If the file isn't open, we'll make sure the buffer values are set in
157 // File::open.
158 if (!isOpen()) {
159 bufferMode = bufmode;
160 bufferSize = size;
161 return true;
162 }
163
164 int vbufmode;
165 vbufmode = _IONBF;
166 if (bufferMode == "newline") vbufmode = _IOLBF;
167 if (bufferMode == "full") vbufmode = _IOFBF;
168
169 if (setvbuf(file, nullptr, vbufmode, (size_t)size) != 0) return false;
170
171 bufferMode = bufmode;
172 bufferSize = size;
173
174 return true;
175}
176
177std::string File::getBuffer(int64_t &size) const {
178 size = bufferSize;
179 return bufferMode;
180}
181
182std::string File::getFilename() const { return filename; }
183std::string File::getMode() const { return mode; }
184
185} // namespace cppfs
186} // namespace filesystem
187} // namespace eve
std::function< Numbers(b3JointId)> read
std::string file
float size
Definition TreeMesh.cpp:158
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
File(std::string filename)
Constructs a File.
Definition File.cpp:22
int64_t read(void *dst, int64_t size) override
Reads read.
Definition File.cpp:103
bool write(const void *data, int64_t size) override
Writes write.
Definition File.cpp:113
int64_t getSize() override
Byte length of the owned buffer.
Definition File.cpp:70
bool seek(uint64_t pos) override
Seeks seek.
Definition File.cpp:141
bool setBuffer(std::string bufmode, int64_t size) override
Sets the buffer.
Definition File.cpp:151
virtual ~File()
Releases File resources.
Definition File.cpp:25
bool open(std::string newmode) override
Opens open.
Definition File.cpp:29
bool isOpen() const override
True when open.
Definition File.cpp:68
bool close() override
Closes close.
Definition File.cpp:59
int64_t tell() override
Returns the current tell.
Definition File.cpp:131
bool flush() override
Flushes flush.
Definition File.cpp:123
bool isEOF() override
True when eof.
Definition File.cpp:129
std::string getMode() const override
Returns the mode.
Definition File.cpp:183
std::string getFilename() const override
Full filename including extension.
Definition File.cpp:182
std::string getBuffer(int64_t &size) const override
Returns the buffer.
Definition File.cpp:177
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16