载入中...
搜索中...
未找到
EveFileSystem.cpp
浏览该文件的文档.
2
3#include "filesystem/File.h"
5
6#include <cstdio>
7
8namespace eve {
9namespace model3d {
10
11namespace {
12
13struct FileHandle {
14 filesystem::File *file = nullptr;
15};
16
17} // namespace
18
20
21medialoader::FileHandle *EveFileSystem::open(const char *path, const char *mode) {
22 if (!fs_ || !path || !mode)
23 return nullptr;
24 try {
25 filesystem::File *file = fs_->newFile(path);
26 if (!file)
27 return nullptr;
28 if (!file->open(mode)) {
29 delete file;
30 return nullptr;
31 }
32 auto *h = new FileHandle();
33 h->file = file;
34 return reinterpret_cast<medialoader::FileHandle *>(h);
35 } catch (...) {
36 return nullptr;
37 }
38}
39
40size_t EveFileSystem::read(medialoader::FileHandle *h, void *buf, size_t size) {
41 auto *fh = reinterpret_cast<FileHandle *>(h);
42 if (!fh || !fh->file || !buf || size == 0)
43 return 0;
44 try {
45 int64_t n = fh->file->read(buf, static_cast<int64_t>(size));
46 return n > 0 ? static_cast<size_t>(n) : 0;
47 } catch (...) {
48 return 0;
49 }
50}
51
52bool EveFileSystem::seek(medialoader::FileHandle *h, int64_t offset, int whence) {
53 auto *fh = reinterpret_cast<FileHandle *>(h);
54 if (!fh || !fh->file)
55 return false;
56 try {
57 int64_t pos = 0;
58 if (whence == SEEK_SET)
59 pos = offset;
60 else if (whence == SEEK_CUR)
61 pos = fh->file->tell() + offset;
62 else if (whence == SEEK_END)
63 pos = fh->file->getSize() + offset;
64 else
65 return false;
66 if (pos < 0)
67 return false;
68 return fh->file->seek(static_cast<uint64_t>(pos));
69 } catch (...) {
70 return false;
71 }
72}
73
74int64_t EveFileSystem::tell(medialoader::FileHandle *h) {
75 auto *fh = reinterpret_cast<FileHandle *>(h);
76 if (!fh || !fh->file)
77 return -1;
78 try {
79 return fh->file->tell();
80 } catch (...) {
81 return -1;
82 }
83}
84
85int64_t EveFileSystem::size(medialoader::FileHandle *h) {
86 auto *fh = reinterpret_cast<FileHandle *>(h);
87 if (!fh || !fh->file)
88 return -1;
89 try {
90 return fh->file->getSize();
91 } catch (...) {
92 return -1;
93 }
94}
95
96void EveFileSystem::close(medialoader::FileHandle *h) {
97 auto *fh = reinterpret_cast<FileHandle *>(h);
98 if (!fh)
99 return;
100 if (fh->file) {
101 try {
102 if (fh->file->isOpen())
103 fh->file->close();
104 } catch (...) {
105 }
106 delete fh->file;
107 fh->file = nullptr;
108 }
109 delete fh;
110}
111
112bool EveFileSystem::exists(const char *path) const {
113 if (!fs_ || !path)
114 return false;
115 try {
117 return fs_->getInfo(path, info);
118 } catch (...) {
119 return false;
120 }
121}
122
123} // namespace model3d
124} // namespace eve
filesystem::File * file
glm::vec3 n
Definition Grass.cpp:64
int h
A File interface, providing generic means of reading from and writing to files.
Definition File.h:14
virtual bool getInfo(std::string filepath, Info &info) const =0
Gets information about the item at the specified filepath. Returns false if nothing exists at the pat...
virtual File * newFile(std::string filename) const =0
Creates a new file.
int64_t size(medialoader::FileHandle *h) override
bool exists(const char *path) const override
size_t read(medialoader::FileHandle *h, void *buf, size_t size) override
EveFileSystem(filesystem::Filesystem *fs)
int64_t tell(medialoader::FileHandle *h) override
bool seek(medialoader::FileHandle *h, int64_t offset, int whence) override
void close(medialoader::FileHandle *h) override
medialoader::FileHandle * open(const char *path, const char *mode) override
Definition Build.cpp:11