diff options
Diffstat (limited to 'source/modules/asura-utils/io')
-rw-r--r-- | source/modules/asura-utils/io/binding/_compressor.cpp | 0 | ||||
-rw-r--r-- | source/modules/asura-utils/io/binding/_data_buffer.cpp | 132 | ||||
-rw-r--r-- | source/modules/asura-utils/io/binding/_file.cpp | 223 | ||||
-rw-r--r-- | source/modules/asura-utils/io/binding/_file_data.cpp | 60 | ||||
-rw-r--r-- | source/modules/asura-utils/io/binding/_file_system.cpp | 265 | ||||
-rw-r--r-- | source/modules/asura-utils/io/binding/_io_task.cpp | 46 | ||||
-rw-r--r-- | source/modules/asura-utils/io/compressor.cpp | 11 | ||||
-rw-r--r-- | source/modules/asura-utils/io/compressor.h | 28 | ||||
-rw-r--r-- | source/modules/asura-utils/io/file.cpp | 294 | ||||
-rw-r--r-- | source/modules/asura-utils/io/file.h | 146 | ||||
-rw-r--r-- | source/modules/asura-utils/io/renewable.h | 29 |
11 files changed, 0 insertions, 1234 deletions
diff --git a/source/modules/asura-utils/io/binding/_compressor.cpp b/source/modules/asura-utils/io/binding/_compressor.cpp deleted file mode 100644 index e69de29..0000000 --- a/source/modules/asura-utils/io/binding/_compressor.cpp +++ /dev/null diff --git a/source/modules/asura-utils/io/binding/_data_buffer.cpp b/source/modules/asura-utils/io/binding/_data_buffer.cpp deleted file mode 100644 index 9d3f3a0..0000000 --- a/source/modules/asura-utils/io/binding/_data_buffer.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include "../DataBuffer.h" - -using namespace Luax; - -namespace AsuraEngine -{ - namespace IO - { - - LUAX_REGISTRY(DataBuffer) - { - LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "GetData", _GetData }, - { "GetSize", _GetSize }, - { "GetCapacity", _GetCapacity }, - { "Refactor", _Refactor }, - { "Load", _Load }, - { "Clear", _Clear } - ); - } - - LUAX_POSTPROCESS(DataBuffer) - { - } - - // databuffer = DataBuffer.New(lstring) - // databuffer = DataBuffer.New(capacity) - LUAX_IMPL_METHOD(DataBuffer, _New) - { - LUAX_STATE(L); - - if (state.IsType(1, LUA_TSTRING)) - { - size_t size; - const byte* bytes = lua_tolstring(L, 1, &size); - DataBuffer* buffer = new DataBuffer(bytes, size); - buffer->PushLuaxUserdata(state); - return 1; - } - else if (state.IsType(1, LUA_TNUMBER)) - { - size_t capacity = lua_tonumber(L, 1); - DataBuffer* buffer = new DataBuffer(capacity); - buffer->PushLuaxUserdata(state); - return 1; - } - else - { - return state.ErrorType(1, "number or string"); - } - } - - // lsting, len = databuffer:GetData() - LUAX_IMPL_METHOD(DataBuffer, _GetData) - { - LUAX_SETUP(L, "U"); - - DataBuffer* self = state.GetUserdata<DataBuffer>(1); - lua_pushlstring(L, self->GetData(), self->GetSize()); - return 1; - } - - // length = databuffer:GetSize() - LUAX_IMPL_METHOD(DataBuffer, _GetSize) - { - LUAX_SETUP(L, "U"); - - DataBuffer* self = state.GetUserdata<DataBuffer>(1); - lua_pushinteger(L, self->GetSize()); - return 1; - } - - // capacity = databuffer:GetCapacity() - LUAX_IMPL_METHOD(DataBuffer, _GetCapacity) - { - LUAX_SETUP(L, "U"); - - DataBuffer* self = state.GetUserdata<DataBuffer>(1); - lua_pushinteger(L, self->GetCapacity()); - return 1; - } - - // databuffer:Refactor(capacity) - LUAX_IMPL_METHOD(DataBuffer, _Refactor) - { - LUAX_PREPARE(L, DataBuffer); - - size_t capacity = state.CheckValue<int>(2); - self->Refactor(capacity); - return 0; - } - - // size = databuffer:Load(lstring) - // size = databuffer:Load(src) - LUAX_IMPL_METHOD(DataBuffer, _Load) - { - LUAX_STATE(L); - - DataBuffer* buffer = state.GetUserdata<DataBuffer>(1); - const byte* data; - size_t size; - if (state.IsType(2, LUA_TSTRING)) - { - data = lua_tolstring(L, 2, &size); - buffer->Load(data, size); - return 0; - } - else if(state.IsType(2, LUA_TUSERDATA)) - { - DataBuffer* src = state.CheckUserdata<DataBuffer>(2); - buffer->Load(*src); - return 0; - } - else - { - return state.ErrorType(1, "lstring or DataBuffer"); - } - } - - // databuffer:Clear() - LUAX_IMPL_METHOD(DataBuffer, _Clear) - { - LUAX_SETUP(L, "U"); - - DataBuffer* self = state.GetUserdata<DataBuffer>(1); - self->Clear(); - return 0; - } - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/binding/_file.cpp b/source/modules/asura-utils/io/binding/_file.cpp deleted file mode 100644 index c44bc90..0000000 --- a/source/modules/asura-utils/io/binding/_file.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include "../file.h" - -namespace AsuraEngine -{ - namespace IO - { - - LUAX_REGISTRY(File) - { - LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "Open", _Open }, - { "Close", _Close }, - { "IsOpen", _IsOpen }, - { "GetMode", _GetMode }, - { "GetSize", _GetSize }, - { "Read", _Read }, - { "IsEOF", _IsEOF }, - { "Write", _Write }, - { "Flush", _Flush }, - { "Tell", _Tell }, - { "Seek", _Seek }, - { "SetBuffer", _SetBuffer }, - { "GetBuffer", _GetBuffer }, - { "GetFileName", _GetFileName }, - { "GetExtension", _GetExtension }, - { "GetName", _GetName } - ); - } - - LUAX_POSTPROCESS(File) - { - LUAX_REGISTER_ENUM(state, "EFileMode", - { "CLOSED", FILE_MODE_CLOSED }, - { "READ", FILE_MODE_READ }, - { "WRITE", FILE_MODE_WRITE }, - { "APPEND", FILE_MODE_APPEND } - ); - - LUAX_REGISTER_ENUM(state, "EBufferMode", - { "NONE", BUFFER_MODE_NONE}, - { "LINE", BUFFER_MODE_LINE}, - { "FULL", BUFFER_MODE_FULL} - ); - } - - // file = File.New(name) - LUAX_IMPL_METHOD(File, _New) - { - LUAX_STATE(L); - - cc8* name = state.CheckValue<cc8*>(1); - File* file = new File(name); - file->PushLuaxUserdata(state); - return 1; - } - - // successsed = file:Open(mode) - LUAX_IMPL_METHOD(File, _Open) - { - LUAX_PREPARE(L, File); - - File::FileMode mode = (File::FileMode)state.CheckValue<int>(2); - state.Push(self->Open(mode)); - return 1; - } - - // successed = file:Close() - LUAX_IMPL_METHOD(File, _Close) - { - LUAX_PREPARE(L, File); - - state.Push(self->Close()); - return 1; - } - - // opened = file:IsOpen() - LUAX_IMPL_METHOD(File, _IsOpen) - { - LUAX_PREPARE(L, File); - - state.Push(self->IsOpen()); - return 1; - } - - // mode = file:GetMode() - LUAX_IMPL_METHOD(File, _GetMode) - { - LUAX_PREPARE(L, File); - - File::FileMode mode = self->GetMode(); - state.Push((int)mode); - return 1; - } - - // size = file:GetSize() - LUAX_IMPL_METHOD(File, _GetSize) - { - LUAX_PREPARE(L, File); - - state.Push(self->GetSize()); - return 1; - } - - // size = file:Read(dst, len) - // returns: - // size ʵʶĴС - // params: - // self ļ - // dst Ŀ껺 - // len ĴС - LUAX_IMPL_METHOD(File, _Read) - { - LUAX_PREPARE(L, File); - - DataBuffer* db = state.CheckUserdata<DataBuffer>(2); - if (!db) return state.ErrorType(2, "DataBuffer"); - int len = state.CheckValue<int>(3); - int size = self->Read(db, len); - state.Push(size); - return 1; - } - - // isEOF = file:IsEOF() - LUAX_IMPL_METHOD(File, _IsEOF) - { - LUAX_PREPARE(L, File); - - state.Push(self->IsEOF()); - return 1; - } - - // isWrite = file:Write(data buffer[, size]) - LUAX_IMPL_METHOD(File, _Write) - { - LUAX_PREPARE(L, File); - - DataBuffer* db = state.CheckUserdata<DataBuffer>(2); - if (!db) return state.ErrorType(2, "DataBuffer"); - state.Push(self->Write(db)); - return 1; - } - - // isFlushed = file:Flush() - LUAX_IMPL_METHOD(File, _Flush) - { - LUAX_PREPARE(L, File); - - state.Push(self->Flush()); - return 1; - } - - // pos = file:Tell() - LUAX_IMPL_METHOD(File, _Tell) - { - LUAX_PREPARE(L, File); - - state.Push(self->Tell()); - return 1; - } - - // isSeek = file:Seek(pos) - LUAX_IMPL_METHOD(File, _Seek) - { - LUAX_PREPARE(L, File); - - int pos = state.CheckValue<int>(2); - state.Push(self->Seek(pos)); - return 1; - } - - // isSetted = file:SetBuffer(mode, size) - LUAX_IMPL_METHOD(File, _SetBuffer) - { - LUAX_PREPARE(L, File); - - BufferMode mode = (BufferMode)state.CheckValue<int>(2); - int size = state.CheckValue<int>(3); - state.Push(self->SetBuffer(mode, size)); - return 1; - } - - // size, mode = file:GetBuffer() - LUAX_IMPL_METHOD(File, _GetBuffer) - { - LUAX_PREPARE(L, File); - - size_t size = 0; - BufferMode mode = self->GetBuffer(ASURA_OUT size); - state.Push((int)size); - state.Push((int)mode); - return 2; - } - - // name = file:GetFileName() - LUAX_IMPL_METHOD(File, _GetFileName) - { - LUAX_PREPARE(L, File); - - state.Push(self->GetFileName()); - return 1; - } - - // name = file:GetExtension() - LUAX_IMPL_METHOD(File, _GetExtension) - { - LUAX_PREPARE(L, File); - - state.Push(self->GetExtension()); - return 1; - } - - // name = file:GetName() - LUAX_IMPL_METHOD(File, _GetName) - { - LUAX_PREPARE(L, File); - - state.Push(self->GetName()); - return 1; - } - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/binding/_file_data.cpp b/source/modules/asura-utils/io/binding/_file_data.cpp deleted file mode 100644 index 55cbc8b..0000000 --- a/source/modules/asura-utils/io/binding/_file_data.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "../FileData.h" - -using namespace std; - -namespace AsuraEngine -{ - namespace IO - { - - LUAX_REGISTRY(FileData) - { - LUAX_REGISTER_METHODS(state, - { "GetFileName", _GetFileName }, - { "GetExtension", _GetExtension }, - { "GetName", _GetName }, - { "GetDataBuffer", _GetDataBuffer } - ); - } - - LUAX_POSTPROCESS(FileData) - { - } - - // filename = filedata:GetFileName() - LUAX_IMPL_METHOD(FileData, _GetFileName) - { - LUAX_PREPARE(L, FileData); - string filename = self->GetFileName(); - state.Push(filename); - return 1; - } - - // extension = filedata:GetExtension() - LUAX_IMPL_METHOD(FileData, _GetExtension) - { - LUAX_PREPARE(L, FileData); - string extension = self->GetExtension(); - state.Push(extension); - return 1; - } - - // name = filedata:GetName() - LUAX_IMPL_METHOD(FileData, _GetName) - { - LUAX_PREPARE(L, FileData); - string extension = self->GetName(); - state.Push(extension); - return 1; - } - - // databuffer = filedata:GetDataBuffer() - LUAX_IMPL_METHOD(FileData, _GetDataBuffer) - { - LUAX_PREPARE(L, FileData); - self->PushLuaxMemberRef(state, self->m_DataRef); - return 1; - } - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/binding/_file_system.cpp b/source/modules/asura-utils/io/binding/_file_system.cpp deleted file mode 100644 index ace3c5f..0000000 --- a/source/modules/asura-utils/io/binding/_file_system.cpp +++ /dev/null @@ -1,265 +0,0 @@ -#include "../FileSystem.h" - -using namespace Luax; - -namespace AsuraEngine -{ - namespace IO - { - -#define PREPARE(l) \ - LUAX_STATE(l); \ - Filesystem* fs = Filesystem::Get(); - - LUAX_REGISTRY(Filesystem) - { - LUAX_REGISTER_METHODS(state, - { "Init", _Init }, - { "Mount", _Mount }, - { "Unmount", _Unmount }, - { "GetMountPoint", _GetMountPoint }, - { "SetWriteDirectory", _SetWriteDirectory }, - { "GetWriteDirectory", _GetWriteDirectory }, - { "CreateFile", _CreateFile }, - { "CreateDirectory", _CreateDirectory }, - { "Write", _Write }, - { "Append", _Append }, - { "Remove", _Remove }, - { "Read", _Read }, - { "GetFileInfo", _GetFileInfo }, - { "GetDirectoryItems", _GetDirectoryItems } - ); - } - - LUAX_POSTPROCESS(Filesystem) - { - LUAX_REGISTER_ENUM(state, "EFileType", - { "FILE", FILE_TYPE_FILE }, - { "DIRECTORY", FILE_TYPE_DIRECTORY }, - { "SYMLINK", FILE_TYPE_SYMLINK }, - { "OTHER", FILE_TYPE_OTHER } - ); - } - - // Filesystem.Init(arg0) - LUAX_IMPL_METHOD(Filesystem, _Init) - { - PREPARE(L); - - const char* arg0 = state.CheckValue<const char*>(1); - fs->Init(arg0); - return 0; - } - - // successed = Filesystem.Mount(path, mountpoint[, prepend = false]) - // successed = Filesystem.Mount(data buffer, archievename, mountpoint[, prepend = false]) - LUAX_IMPL_METHOD(Filesystem, _Mount) - { - PREPARE(L); - bool mounted = false; - - if (state.IsType(1, LUA_TSTRING)) - { - cc8* path = state.GetValue<cc8*>(1, ""); - cc8* moutpoint = state.GetValue<cc8*>(2, "/"); - bool prepend = state.GetValue<bool>(3, false); - mounted = fs->Mount(path, moutpoint, prepend); - } - else if (state.IsType(1, LUA_TUSERDATA)) - { - DataBuffer* db = state.CheckUserdata<DataBuffer>(1); - if (!db) - return state.ErrorType(1, "Data Buffer"); - cc8* arcname = state.GetValue<cc8*>(2, ""); - cc8* mountpoint = state.GetValue<cc8*>(3, "/"); - bool prepend = state.GetValue<bool>(4, false); - mounted = fs->Mount(db, arcname, mountpoint, prepend); - // retain - fs->LuaxRetain<DataBuffer>(state, db); - } - state.Push(mounted); - return 1; - } - - // successed = Filesystem.Unmount(path) - // successed = Filesystem.Unmount(data buffer) - LUAX_IMPL_METHOD(Filesystem, _Unmount) - { - PREPARE(L); - bool unmounted = false; - - if (state.IsType(1, LUA_TSTRING)) - { - cc8* path = state.GetValue<cc8*>(1, ""); - unmounted = fs->Unmount(path); - } - else if (state.IsType(1, LUA_TUSERDATA)) - { - DataBuffer* db = state.CheckUserdata<DataBuffer>(1); - if (!db) - return state.ErrorType(1, "Data Buffer"); - unmounted = fs->Unmount(db); - if (unmounted) - fs->LuaxRelease<DataBuffer>(state, db); - } - state.Push(unmounted); - return 1; - } - - // moutpoint = Filesystem.GetMountPoint(path) - LUAX_IMPL_METHOD(Filesystem, _GetMountPoint) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - std::string mp; - if (fs->GetMountPoint(path, ASURA_OUT mp)) - state.Push(mp); - else - state.PushNil(); - - return 1; - } - - // Filesystem.SetWriteDirectory(dir) - LUAX_IMPL_METHOD(Filesystem, _SetWriteDirectory) - { - PREPARE(L); - - cc8* dir = state.CheckValue<cc8*>(1); - fs->SetWriteDirectory(dir); - return 0; - } - - // dir = Filesystem.GetWriteDirectory() - LUAX_IMPL_METHOD(Filesystem, _GetWriteDirectory) - { - PREPARE(L); - - std::string dir = fs->GetWriteDirectory(); - state.Push(dir); - return 1; - } - - // file = Filesystem.CreateFile(name) - LUAX_IMPL_METHOD(Filesystem, _CreateFile) - { - PREPARE(L); - - cc8* name = state.CheckValue<cc8*>(1); - File* file = fs->NewFile(name); - if (file) - file->PushLuaxUserdata(state); - else - state.PushNil(); - return 1; - } - - // successed = Filesystem.CreateDirectory(name) - LUAX_IMPL_METHOD(Filesystem, _CreateDirectory) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - state.Push(fs->NewDirectory(path)); - return 1; - } - - // successed = Filesystem.Write(path, data buffer) - LUAX_IMPL_METHOD(Filesystem, _Write) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - DataBuffer* db = state.CheckUserdata<DataBuffer>(2); - state.Push(fs->Write(path, db)); - return 1; - } - - // successed = Filesystem.Append(path, data buffer) - LUAX_IMPL_METHOD(Filesystem, _Append) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - DataBuffer* db = state.CheckUserdata<DataBuffer>(2); - state.Push(fs->Append(path, db)); - return 1; - } - - // successed = Filesystem.Remove(path) - LUAX_IMPL_METHOD(Filesystem, _Remove) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - state.Push(fs->Remove(path)); - return 1; - } - - // filedata = Filesystem.Read(path) - LUAX_IMPL_METHOD(Filesystem, _Read) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - FileData* fd = fs->Read(path); - if (fd) - { - fd->m_Data->PushLuaxUserdata(state); - fd->SetLuaxMemberRef(state, fd->m_DataRef, -1); // fd->m_DataRef = data buffer - state.Pop(1); // data buffer - fd->PushLuaxUserdata(state); - } - else - { - state.PushNil(); - } - return 1; - } - - // fileinfo = Filesystem.GetFileInfo(path) - LUAX_IMPL_METHOD(Filesystem, _GetFileInfo) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - FileInfo info; - if (fs->GetFileInfo(path, &info)) - { - lua_newtable(L); // info table - state.SetField(-1, "size", info.size); - state.SetField(-1, "modtime", info.modtime); - state.SetField(-1, "type", info.type); - } - else - { - state.PushNil(); - } - return 1; - } - - // items = Filesystem.GetDirectoryItems(path) - LUAX_IMPL_METHOD(Filesystem, _GetDirectoryItems) - { - PREPARE(L); - - cc8* path = state.CheckValue<cc8*>(1); - std::vector<std::string> items; - if(fs->GetDirectoryItems(path, ASURA_OUT items)) - { - lua_newtable(L); // item list - for (int i = 0; i < items.size(); ++i) - { - state.SetFieldByIndex(-1, i + 1, items[i]); - } - } - else - { - state.PushNil(); - } - return 1; - } - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/binding/_io_task.cpp b/source/modules/asura-utils/io/binding/_io_task.cpp deleted file mode 100644 index 058f4fd..0000000 --- a/source/modules/asura-utils/io/binding/_io_task.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "../IOTask.h" - -using namespace std; - -namespace AsuraEngine -{ - namespace IO - { - - LUAX_REGISTRY(IOTask) - { - LUAX_REGISTER_METHODS(state, - { "New", _New } - ); - } - - LUAX_POSTPROCESS(IOTask) - { - LUAX_REGISTER_ENUM(state, "EIOTaskType", - { "READ", IOTASK_TYPE_READ }, - { "WRITE", IOTASK_TYPE_WRITE }, - { "APPEND", IOTASK_TYPE_APPEND } - ); - - } - - // task = IOTask.New(path, buffer, type, callback) - LUAX_IMPL_METHOD(IOTask, _New) - { - LUAX_STATE(L); - - cc8* path = state.CheckValue<cc8*>(1); - DataBuffer* db = state.CheckUserdata<DataBuffer>(2); - IOTaskType type = (IOTaskType)state.CheckValue<int>(3); - bool cbk = state.GetTop() >= 4 && state.IsType(4, LUA_TFUNCTION); - - IOTask* task = new IOTask(path, db, type); - task->SetLuaxMemberRef(state, task->m_BufferRef, 2); - if(cbk) - task->SetLuaxMemberRef(state, task->m_Callback, 4); - task->PushLuaxUserdata(state); - return 1; - } - - } -} diff --git a/source/modules/asura-utils/io/compressor.cpp b/source/modules/asura-utils/io/compressor.cpp deleted file mode 100644 index 4202263..0000000 --- a/source/modules/asura-utils/io/compressor.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "Compressor.h" - -namespace AsuraEngine -{ - namespace IO - { - - - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/compressor.h b/source/modules/asura-utils/io/compressor.h deleted file mode 100644 index dc25e2a..0000000 --- a/source/modules/asura-utils/io/compressor.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _ASURA_COMPRESSOR_H_ -#define _ASURA_COMPRESSOR_H_ - -#include "../Scripting/Portable.hpp" - -namespace AsuraEngine -{ - namespace IO - { - - class Compressor ASURA_FINAL - : public AEScripting::Portable<Compressor> - { - public: - - LUAX_DECL_SINGLETON(Compressor); - - private: - - LUAX_DECL_METHOD(_Compress); - LUAX_DECL_METHOD(_Decompress); - - }; - - } -} - -#endif
\ No newline at end of file diff --git a/source/modules/asura-utils/io/file.cpp b/source/modules/asura-utils/io/file.cpp deleted file mode 100644 index 6d5f4eb..0000000 --- a/source/modules/asura-utils/io/file.cpp +++ /dev/null @@ -1,294 +0,0 @@ -#include <physfs/physfs.h> - -#include <asura-utils/Exceptions/Exception.h> - -#include "File.h" - -namespace AsuraEngine -{ - namespace IO - { - - File::File(const std::string& filename) - : m_FileName(filename) - , m_FileHandle(nullptr) - , m_Mode(FILE_MODE_CLOSED) - , m_BufferMode(BUFFER_MODE_NONE) - , m_BufferSize(0) - { - size_t dot = filename.rfind('.'); - if (dot != std::string::npos) - { - m_Extension = filename.substr(dot + 1); - m_Name = filename.substr(0, dot); - } - else - m_Name = filename; - } - - File::~File() - { - if (m_Mode != FILE_MODE_CLOSED) - Close(); - } - - bool File::Open(FileMode mode) - { - if (!PHYSFS_isInit()) - throw Exception("Physfs is NOT initialized."); - - if (mode == FILE_MODE_CLOSED) - return false; - - if (mode == FILE_MODE_READ && !PHYSFS_exists(m_FileName.c_str())) - throw Exception("Could NOT open file %s. Does not exist.", m_FileName.c_str()); - - if (mode == FILE_MODE_APPEND || mode == FILE_MODE_WRITE) - { - if (!PHYSFS_getWriteDir()) - { - throw Exception("Could NOT set write directory."); - } - } - - // Ѿ֮ǰͲٴµhandle - if (m_FileHandle != nullptr) - return true; - - PHYSFS_getLastErrorCode(); - - PHYSFS_File* handle = nullptr; - - switch (mode) - { - case FILE_MODE_READ: - handle = PHYSFS_openRead(m_FileName.c_str()); - break; - case FILE_MODE_APPEND: - handle = PHYSFS_openAppend(m_FileName.c_str()); - break; - case FILE_MODE_WRITE: - handle = PHYSFS_openWrite(m_FileName.c_str()); - break; - } - - if (handle == nullptr) - { - const char *err = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); - if (err == nullptr) - err = "unknown error"; - throw Exception("Could not open file %s (%s)", m_FileName.c_str(), err); - } - - m_FileHandle = handle; - m_Mode = mode; - - if (m_FileHandle && !SetBuffer(m_BufferMode,m_BufferSize)) - { - m_BufferMode = BUFFER_MODE_NONE; - m_BufferSize = 0; - } - - return m_FileHandle != nullptr; - } - - bool File::Close() - { - if (m_FileHandle == nullptr || !PHYSFS_close(m_FileHandle)) - return false; - m_Mode = FILE_MODE_CLOSED; - m_FileHandle = nullptr; - return true; - } - - bool File::IsOpen() - { - return m_Mode != FILE_MODE_CLOSED && m_FileHandle != nullptr; - } - - size_t File::GetSize() - { - if (m_FileHandle == nullptr) - { - Open(FILE_MODE_READ); - size_t size = PHYSFS_fileLength(m_FileHandle); - Close(); - return size; - } - return PHYSFS_fileLength(m_FileHandle); - } - - size_t File::Read(ASURA_OUT DataBuffer* dst, size_t length) - { - ASSERT(dst); - - if (dst->GetCapacity() < length) - throw Exception("Data buffer is too small compares to read length."); - - if (!m_FileHandle || m_Mode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", m_FileName); - - size_t max = PHYSFS_fileLength(m_FileHandle); - length = (length > max) ? max : length; - - if (length < 0) - throw Exception("Invalid read size."); - - dst->Lock(); - size_t size = PHYSFS_readBytes(m_FileHandle, dst->GetData(), length); - dst->Unlock(); - return size; - } - - size_t File::ReadAll(ASURA_OUT DataBuffer* dst) - { - ASSERT(dst); - - if (!m_FileHandle || m_Mode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", m_FileName); - - size_t length = PHYSFS_fileLength(m_FileHandle); - - if (dst->GetCapacity() < length) - throw Exception("Data buffer is too small compares to file length."); - - dst->Lock(); - byte* data = dst->GetData(); - size_t size = PHYSFS_readBytes(m_FileHandle, data, length); - dst->Move(data, length); - dst->Unlock(); - return size; - } - -#ifdef ASURA_WINDOWS - inline bool test_eof(File *that, PHYSFS_File *) - { - int64 pos = that->Tell(); - int64 size = that->GetSize(); - return pos == -1 || size == -1 || pos >= size; - } -#else - inline bool test_eof(File *, PHYSFS_File *file) - { - return PHYSFS_eof(file); - } -#endif - - bool File::IsEOF() - { - return m_FileHandle == nullptr || test_eof(this, m_FileHandle); - } - - size_t File::Tell() - { - if (!m_FileHandle) - return - 1; - - return PHYSFS_tell(m_FileHandle); - } - - bool File::Seek(size_t pos) - { - return m_FileHandle != nullptr && PHYSFS_seek(m_FileHandle, pos) != 0; - } - - bool File::Write(ASURA_REF DataBuffer* src) - { - if (!m_FileHandle || (m_Mode != FILE_MODE_APPEND && m_Mode != FILE_MODE_WRITE)) - throw Exception("File is not opened for writing."); - - byte* data = src->GetData(); - int size = src->GetSize(); - - if (size < 0) - throw Exception("Invalid write size."); - - size_t written = PHYSFS_writeBytes(m_FileHandle, data, size); - - if (written != src->GetSize()) - return false; - - // л - if (m_BufferSize == BUFFER_MODE_LINE && m_BufferSize > size) - { - if (memchr(data, '\n', size) != nullptr) - Flush(); - } - - return true; - } - - bool File::Flush() - { - if (!m_FileHandle || (m_Mode != FILE_MODE_WRITE && m_Mode != FILE_MODE_APPEND)) - throw Exception("File is not opened for writing."); - - return PHYSFS_flush(m_FileHandle) != 0; - } - - bool File::SetBuffer(BufferMode mode, size_t size) - { - if (size < 0) - return false; - - // If the file isn't open, we'll make sure the buffer values are set in - // File::open. - if (!IsOpen()) - { - m_BufferMode = mode; - m_BufferSize = size; - return true; - } - - int ret = 1; - - switch (mode) - { - case BUFFER_MODE_NONE: - default: - ret = PHYSFS_setBuffer(m_FileHandle, 0); - size = 0; - break; - case BUFFER_MODE_LINE: - case BUFFER_MODE_FULL: - ret = PHYSFS_setBuffer(m_FileHandle, size); - break; - } - - if (ret == 0) - return false; - - m_BufferMode = mode; - m_BufferSize = size; - - return true; - } - - File::BufferMode File::GetBuffer(ASURA_OUT size_t& size) - { - size = m_BufferSize; - return m_BufferMode; - } - - const std::string& File::GetFileName() - { - return m_FileName; - } - - const std::string& File::GetName() - { - return m_Name; - } - - const std::string& File::GetExtension() - { - return m_Extension; - } - - File::FileMode File::GetMode() - { - return m_Mode; - } - - } -}
\ No newline at end of file diff --git a/source/modules/asura-utils/io/file.h b/source/modules/asura-utils/io/file.h deleted file mode 100644 index d11fa4f..0000000 --- a/source/modules/asura-utils/io/file.h +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef _ASURA_ENGINE_FILE_H_ -#define _ASURA_ENGINE_FILE_H_ - -#include "physfs/physfs.h" - -#include "../Scripting/Portable.hpp" -#include "../Threads/Thread.h" - -#include "FileData.h" - -namespace AsuraEngine -{ - namespace IO - { - - /// - /// ʽļָд㡢Сʹȡʱʹñ࣬ʹFilesystem.read()ֱӶȡļȫ - /// ݣһFileData - /// - class File ASURA_FINAL - : public AEScripting::Portable<File> - { - public: - - LUAX_DECL_FACTORY(File); - - /// - /// ļдģʽ - /// - enum FileMode - { - FILE_MODE_CLOSED, - FILE_MODE_READ, - FILE_MODE_WRITE, - FILE_MODE_APPEND, - }; - - /// - /// ļдʱΪ - /// - enum BufferMode - { - BUFFER_MODE_NONE, ///< ʹû壬дļ - BUFFER_MODE_LINE, ///< л壬зߴﵽСʱдļ - BUFFER_MODE_FULL, ///< ȫ壬ʱдļ - }; - - File(const std::string& filename); - ~File(); - - bool Open(FileMode mode); - bool Close(); - bool IsOpen(); - FileMode GetMode(); - size_t GetSize(); - - /// - /// ȡdata bufferض - /// - size_t Read(ASURA_OUT DataBuffer* dst, size_t length); - size_t ReadAll(ASURA_OUT DataBuffer* dst); - size_t ReadAsync(ASURA_OUT DataBuffer* dst); - - /// - /// Ƿļβ - /// - bool IsEOF(); - - /// - /// data bufferед룬Ƿɹ - /// - bool Write(ASURA_REF DataBuffer* src); - - /// - /// 첽дļдļtaskthreadĶС - /// - bool WriteAsync(ASURA_REF DataBuffer* src, AEThreading::Thread* thread); - - /// - /// ˻壬ǿջдļ - /// - bool Flush(); - - /// - /// صǰдλ - /// - size_t Tell(); - - /// - /// Ӧλ - /// - bool Seek(size_t pos); - - /// - /// ûСģʽ - /// - bool SetBuffer(BufferMode mode, size_t size); - - /// - /// ȡСģʽ - /// - BufferMode GetBuffer(ASURA_OUT size_t& size); - - const std::string& GetFileName(); - const std::string& GetName(); - const std::string& GetExtension(); - - private: - - PHYSFS_File* m_FileHandle; ///< physfs ļ - std::string m_FileName; ///< ļ - std::string m_Extension; ///< չ - std::string m_Name; ///< չļ - FileMode m_Mode; ///< ļģʽ - BufferMode m_BufferMode; ///< д뻺ģʽ - size_t m_BufferSize; ///< д뻺С - - LUAX_DECL_ENUM(FileMode); - LUAX_DECL_ENUM(BufferMode); - - LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_Open); - LUAX_DECL_METHOD(_Close); - LUAX_DECL_METHOD(_IsOpen); - LUAX_DECL_METHOD(_GetMode); - LUAX_DECL_METHOD(_GetSize); - LUAX_DECL_METHOD(_Read); - LUAX_DECL_METHOD(_Write); - LUAX_DECL_METHOD(_ReadAsync); - LUAX_DECL_METHOD(_WriteAsync); - LUAX_DECL_METHOD(_IsEOF); - LUAX_DECL_METHOD(_Flush); - LUAX_DECL_METHOD(_Tell); - LUAX_DECL_METHOD(_Seek); - LUAX_DECL_METHOD(_SetBuffer); - LUAX_DECL_METHOD(_GetBuffer); - LUAX_DECL_METHOD(_GetFileName); - LUAX_DECL_METHOD(_GetExtension); - LUAX_DECL_METHOD(_GetName); - - }; - - } -} - -#endif
\ No newline at end of file diff --git a/source/modules/asura-utils/io/renewable.h b/source/modules/asura-utils/io/renewable.h deleted file mode 100644 index 90867f2..0000000 --- a/source/modules/asura-utils/io/renewable.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __ASURA_ENGINE_RENEWABLE_H__ -#define __ASURA_ENGINE_RENEWABLE_H__ - -#include "../scripting/portable.hpp" - -#include "DecodedData.h" - -namespace AsuraEngine -{ - namespace IO - { - - /// - /// ¹ݽṹͼƬƵ֣ӽݿֱӹڱ༭ - /// ¹handleֵı䲻߱ƻԣڲıhandleԴ - /// - ASURA_ABSTRACT class Renewable - { - public: - Renewable() {}; - virtual ~Renewable() {}; - }; - - } -} - -namespace AEIO = AsuraEngine::IO; - -#endif
\ No newline at end of file |