载入中...
搜索中...
未找到
File.cpp
浏览该文件的文档.
1#include "File.h"
2#include <cstring>
3
4namespace eve {
5namespace filesystem {
6
8
9FileData *File::read(int64_t size) {
10 bool isopen = isOpen();
11
12 // PhysFS backend expects binary modes ("rb"); plain "r" never opens a handle.
13 if (!isopen && !open("rb")) throw eve::Exception("Could not read file %s.", getFilename().c_str());
14
15 int64_t max = getSize();
16 int64_t cur = tell();
17 size = (size == ALL) ? max : size;
18
19 if (size < 0) throw eve::Exception("Invalid read size.");
20 if (cur + size > max) size = max - cur;
21
22 FileData *fileData = new FileData(getFilename(), size);
23 int64_t bytesRead = read(fileData->getData(), size);
24
25 if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size)) {
26 delete fileData;
27 throw eve::Exception("Could not read from file.");
28 }
29
30 if (bytesRead < size) {
31 FileData *tmpFileData = new FileData(getFilename(), bytesRead);
32 memcpy(tmpFileData->getData(), fileData->getData(), (size_t)bytesRead);
33 delete fileData;
34 fileData = tmpFileData;
35 }
36
37 if (!isopen) close();
38
39 return fileData;
40}
41
42bool File::write(const Data *data, int64_t size) {
43 return write(data->getData(), (size == ALL) ? data->getSize() : size);
44}
45
46std::string File::getExtension() const {
47 const std::string & filename = getFilename();
48 std::string::size_type idx = filename.rfind('.');
49
50 if (idx != std::string::npos)
51 return filename.substr(idx + 1);
52 else
53 return std::string();
54}
55
56} // namespace filesystem
57} // namespace eve
int idx
Light2D::Data * data
Data buffer paired with a filename (used for type identification).
Definition FileData.h:12
void * getData() const
Gets a pointer to the data. This pointer will obviously not be valid if the Data object is destroyed.
Definition FileData.h:23
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:46
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:7
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:9
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.
Definition Build.cpp:11