10#ifdef EVENGINE_WINDOWS
20File::File(std::string filename) : filename(filename),
file(nullptr), mode(
"c"), bufferMode(
"none"), bufferSize(0) {}
23 if (mode !=
"c")
close();
27 if (newmode ==
"c")
return true;
30 if (file !=
nullptr)
return false;
32#ifdef EVENGINE_WINDOWS
34 std::wstring wnewmode = to_widestr(newmode);
35 std::wstring wfilename = to_widestr(filename);
37 file = _wfopen(wfilename.c_str(), wnewmode.c_str());
39 file = fopen(filename.c_str(), newmode.c_str());
42 if (newmode.find(
'c') != std::string::npos && file ==
nullptr)
43 throw Exception(
"Could not open file %s. Does not exist.", filename.c_str());
47 if (file !=
nullptr && !
setBuffer(bufferMode, bufferSize)) {
53 return file !=
nullptr;
57 if (file ==
nullptr || fclose(file) != 0)
return false;
65bool File::isOpen()
const {
return mode !=
"c" && file !=
nullptr; }
68 int fd = file ? fileno(file) : -1;
70#ifdef EVENGINE_WINDOWS
75 if (_fstat64(fd, &buf) != 0)
return -1;
78 std::wstring wfilename = to_widestr(filename);
80 if (_wstat64(wfilename.c_str(), &buf) != 0)
return -1;
83 return (int64_t)buf.st_size;
91 if (fstat(fd, &buf) != 0)
return -1;
92 }
else if (stat(filename.c_str(), &buf) != 0)
95 return (int64_t)buf.st_size;
101 if (!
file || mode !=
"rb")
throw Exception(
"File is not opened for reading.");
107 return (int64_t)
read;
111 if (!
file || (mode !=
"wb" && mode !=
"ab"))
throw Exception(
"File is not opened for writing.");
115 int64_t written = (int64_t)fwrite(data, 1, (
size_t)
size,
file);
117 return written ==
size;
121 if (!
file || (mode !=
"wb" && mode !=
"ab"))
throw Exception(
"File is not opened for writing.");
123 return fflush(
file) == 0;
129 if (
file ==
nullptr)
return -1;
131#ifdef EVENGINE_WINDOWS
132 return (int64_t)_ftelli64(
file);
134 return (int64_t)ftello(
file);
139 if (
file ==
nullptr)
return false;
141#ifdef EVENGINE_WINDOWS
142 return _fseeki64(
file, (int64_t)
pos, SEEK_SET) == 0;
144 return fseeko(
file, (off_t)
pos, SEEK_SET) == 0;
149 if (
size < 0)
return false;
151 if (bufmode ==
"none")
size = 0;
156 bufferMode = bufmode;
163 if (bufferMode ==
"newline") vbufmode = _IOLBF;
164 if (bufferMode ==
"full") vbufmode = _IOFBF;
166 if (setvbuf(
file,
nullptr, vbufmode, (
size_t)
size) != 0)
return false;
168 bufferMode = bufmode;
EVENGINE_API_FOUNDATION public API.
File(std::string filename)
Constructs a File.
int64_t read(void *dst, int64_t size) override
Reads read.
bool write(const void *data, int64_t size) override
Writes write.
int64_t getSize() override
Byte length of the owned buffer.
bool seek(uint64_t pos) override
Seeks seek.
bool setBuffer(std::string bufmode, int64_t size) override
Sets the buffer.
virtual ~File()
Releases File resources.
bool open(std::string newmode) override
Opens open.
bool isOpen() const override
True when open.
bool close() override
Closes close.
int64_t tell() override
Returns the current tell.
bool flush() override
Flushes flush.
bool isEOF() override
True when eof.
std::string getMode() const override
Returns the mode.
std::string getFilename() const override
Full filename including extension.
std::string getBuffer(int64_t &size) const override
Returns the buffer.
Build metadata (engine git commit, build time, third-party version).