载入中...
搜索中...
未找到
File.cpp
浏览该文件的文档.
1#include "File.h"
2#include <cstring>
3#include <iostream>
4#include <limits>
5#include "FileData.h"
6
7// File.cpp
8namespace eve {
9namespace filesystem {
10
12
14 bool isopen = isOpen();
15
16 // PhysFS backend expects binary modes ("rb"); plain "r" never opens a handle.
17 if (!isopen && !open("rb")) throw eve::Exception("Could not read file %s.", getFilename().c_str());
18
19 int64_t max = getSize();
20 int64_t cur = tell();
21 size = (size == ALL) ? max : size;
22
23 if (size < 0) throw eve::Exception("Invalid read size.");
24 if (cur + size > max) size = max - cur;
25
26 FileData *fileData = new FileData(getFilename(), size);
27 int64_t bytesRead = read(fileData->getData(), size);
28
29 if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size)) {
30 delete fileData;
31 throw eve::Exception("Could not read from file.");
32 }
33
34 if (bytesRead < size) {
35 FileData *tmpFileData = new FileData(getFilename(), bytesRead);
36 memcpy(tmpFileData->getData(), fileData->getData(), (size_t)bytesRead);
37 delete fileData;
38 fileData = tmpFileData;
39 }
40
41 if (!isopen) close();
42
43 return fileData;
44}
45
46bool File::write(const Data *data, int64_t size) {
47 return write(data->getData(), (size == ALL) ? data->getSize() : size);
48}
49
50std::string File::getExtension() const {
51 const std::string &filename = getFilename();
52 std::string::size_type idx = filename.rfind('.');
53
54 if (idx != std::string::npos)
55 return filename.substr(idx + 1);
56 else
57 return std::string();
58}
59
60} // namespace filesystem
61} // namespace eve
62
63// FileData.cpp
64namespace eve {
65namespace filesystem {
66
67FileData::FileData(const std::string &filename, uint64_t size) : data(nullptr), size((size_t)size), filename(filename) {
68 try {
69 data = new uint8_t[size];
70 } catch (std::bad_alloc &) {
71 throw eve::Exception("Out of memory.");
72 }
73
74 size_t dotpos = filename.rfind('.');
75
76 if (dotpos != std::string::npos) {
77 extension = filename.substr(dotpos + 1);
78 name = filename.substr(0, dotpos);
79 } else
80 name = filename;
81}
82
84 : data(nullptr), size(c.size), filename(c.filename), extension(c.extension), name(c.name) {
85 try {
86 data = new uint8_t[size];
87 } catch (std::bad_alloc &) {
88 throw eve::Exception("Out of memory.");
89 }
90 memcpy(data, c.data, size);
91}
92
93FileData::~FileData() { delete[] data; }
94
95FileData *FileData::clone() const { return new FileData(*this); }
96
97
98} // namespace filesystem
99} // namespace eve
std::string name
Definition ECS.cpp:154
std::int32_t c
std::function< void(b3JointId, const Numbers &)> write
std::function< Numbers(b3JointId)> read
int idx
float size
Definition TreeMesh.cpp:158
Data public API.
Definition Data.h:8
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Data buffer paired with a filename (used for type identification).
Definition FileData.h:14
FileData(const std::string &filename, uint64_t size)
Allocates a buffer of the given size for the named file.
Definition File.cpp:67
void * getData() const
Pointer to the owned byte buffer.
Definition FileData.h:27
virtual ~FileData()
File data.
Definition File.cpp:93
FileData * clone() const
Implements eve::Data.
Definition File.cpp:95
virtual int64_t getSize()=0
Gets the size of the file.
virtual std::string getExtension() const
Gets the file extension for this File, or empty string if none.
Definition File.cpp:50
virtual int64_t tell()=0
Gets the current position in the File.
virtual bool isOpen() const =0
Gets whether the file is open.
static const int64_t ALL
Used to indicate ALL data in a file.
Definition File.h:19
virtual ~File()
Destructor.
Definition File.cpp:11
virtual bool open(std::string mode)=0
Opens the file in a certain mode.
virtual FileData * read(int64_t size=ALL)
Reads data from the file and allocates a Data object.
Definition File.cpp:13
virtual bool write(const void *data, int64_t size)=0
Writes data into the File.
virtual std::string getFilename() const =0
Gets the filename for this File, or empty string if none.
virtual bool close()=0
Closes the file.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16