summaryrefslogtreecommitdiff
path: root/source/libs/asura-lib-utils/io
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-26 09:09:02 +0800
committerchai <chaifix@163.com>2019-03-26 09:09:02 +0800
commitd9041d6e12ded456c17622f7f2e7bbacb9e99b1a (patch)
tree6fd4febeb79b9b5efb5341ea352e05cd7752f5e8 /source/libs/asura-lib-utils/io
parent70f8aa8d1a3c15bd1eee3cdd88b9b9ce8970fae5 (diff)
*misc
Diffstat (limited to 'source/libs/asura-lib-utils/io')
-rw-r--r--source/libs/asura-lib-utils/io/binding/_data_buffer.cpp38
-rw-r--r--source/libs/asura-lib-utils/io/binding/_file.cpp50
-rw-r--r--source/libs/asura-lib-utils/io/binding/_file_data.cpp14
-rw-r--r--source/libs/asura-lib-utils/io/binding/_file_system.cpp12
-rw-r--r--source/libs/asura-lib-utils/io/data_buffer.cpp22
-rw-r--r--source/libs/asura-lib-utils/io/data_buffer.h6
-rw-r--r--source/libs/asura-lib-utils/io/decoded_data.cpp7
-rw-r--r--source/libs/asura-lib-utils/io/file.cpp6
-rw-r--r--source/libs/asura-lib-utils/io/file.h2
-rw-r--r--source/libs/asura-lib-utils/io/file_data.cpp5
-rw-r--r--source/libs/asura-lib-utils/io/file_data.h3
-rw-r--r--source/libs/asura-lib-utils/io/file_system.h4
12 files changed, 103 insertions, 66 deletions
diff --git a/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp b/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp
index d9eca36..c42888c 100644
--- a/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp
+++ b/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp
@@ -10,11 +10,12 @@ namespace AsuraEngine
LUAX_REGISTRY(DataBuffer)
{
LUAX_REGISTER_METHODS(state,
- { "New", _New },
- { "GetBuffer", _GetData },
- { "GetSize", _GetSize },
- { "Load", _Load },
- { "Clear", _Clear }
+ { "New", _New },
+ { "GetBuffer", _GetData },
+ { "GetSize", _GetSize },
+ { "Refactor", _Refactor },
+ { "Load", _Load },
+ { "Clear", _Clear }
);
}
@@ -30,9 +31,8 @@ namespace AsuraEngine
if (state.IsType(1, LUA_TSTRING))
{
- byte* bytes;
size_t size;
- lua_tolstring(L, 1, &size);
+ const byte* bytes = lua_tolstring(L, 1, &size);
DataBuffer* buffer = new DataBuffer(bytes, size);
buffer->PushLuaxUserdata(state);
return 1;
@@ -69,8 +69,18 @@ namespace AsuraEngine
return 1;
}
- // databuffer:Load(lstring)
- // databuffer:Load(src)
+ // databuffer:Refactor(size)
+ LUAX_IMPL_METHOD(DataBuffer, _Refactor)
+ {
+ LUAX_PREPARE(L, DataBuffer);
+
+ size_t size = state.CheckParam<int>(2);
+ self->Refactor(size);
+ return 0;
+ }
+
+ // size = databuffer:Load(lstring)
+ // size = databuffer:Load(src)
LUAX_IMPL_METHOD(DataBuffer, _Load)
{
LUAX_STATE(L);
@@ -81,14 +91,16 @@ namespace AsuraEngine
if (state.IsType(2, LUA_TSTRING))
{
data = lua_tolstring(L, 2, &size);
- buffer->Load(data, size);
- return 0;
+ size_t len = buffer->Load(data, size);
+ state.Push(len);
+ return 1;
}
else if(state.IsType(2, LUA_TUSERDATA))
{
DataBuffer* src = state.CheckUserdata<DataBuffer>(2);
- buffer->Load(*src);
- return 0;
+ size_t len = buffer->Load(*src);
+ state.Push(len);
+ return 1;
}
else
{
diff --git a/source/libs/asura-lib-utils/io/binding/_file.cpp b/source/libs/asura-lib-utils/io/binding/_file.cpp
index 5c4c628..0baffd5 100644
--- a/source/libs/asura-lib-utils/io/binding/_file.cpp
+++ b/source/libs/asura-lib-utils/io/binding/_file.cpp
@@ -8,21 +8,23 @@ namespace AsuraEngine
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 }
+ { "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 }
);
}
@@ -199,5 +201,23 @@ namespace AsuraEngine
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/libs/asura-lib-utils/io/binding/_file_data.cpp b/source/libs/asura-lib-utils/io/binding/_file_data.cpp
index 56f65f7..09a0643 100644
--- a/source/libs/asura-lib-utils/io/binding/_file_data.cpp
+++ b/source/libs/asura-lib-utils/io/binding/_file_data.cpp
@@ -7,10 +7,6 @@ namespace AsuraEngine
namespace IO
{
-#define PREPARE(L) \
- LUAX_STATE(L); \
- FileData* self = state.GetUserdata<FileData>(1);
-
LUAX_REGISTRY(FileData)
{
LUAX_REGISTER_METHODS(state,
@@ -28,7 +24,7 @@ namespace AsuraEngine
// filename = filedata:GetFileName()
LUAX_IMPL_METHOD(FileData, _GetFileName)
{
- PREPARE(L);
+ LUAX_PREPARE(L, FileData);
string filename = self->GetFileName();
state.Push(filename);
return 1;
@@ -37,7 +33,7 @@ namespace AsuraEngine
// extension = filedata:GetExtension()
LUAX_IMPL_METHOD(FileData, _GetExtension)
{
- PREPARE(L);
+ LUAX_PREPARE(L, FileData);
string extension = self->GetExtension();
state.Push(extension);
return 1;
@@ -46,7 +42,7 @@ namespace AsuraEngine
// name = filedata:GetName()
LUAX_IMPL_METHOD(FileData, _GetName)
{
- PREPARE(L);
+ LUAX_PREPARE(L, FileData);
string extension = self->GetName();
state.Push(extension);
return 1;
@@ -55,10 +51,10 @@ namespace AsuraEngine
// databuffer = filedata:GetDataBuffer()
LUAX_IMPL_METHOD(FileData, _GetDataBuffer)
{
- PREPARE(L);
+ LUAX_PREPARE(L, FileData);
self->PushLuaxMemberRef(state, self->mDataRef);
return 1;
}
}
-}
+} \ No newline at end of file
diff --git a/source/libs/asura-lib-utils/io/binding/_file_system.cpp b/source/libs/asura-lib-utils/io/binding/_file_system.cpp
index e790888..7132456 100644
--- a/source/libs/asura-lib-utils/io/binding/_file_system.cpp
+++ b/source/libs/asura-lib-utils/io/binding/_file_system.cpp
@@ -51,8 +51,8 @@ namespace AsuraEngine
return 0;
}
- // successed = Filesystem.Mount(path, mountpoint, prepend)
- // successed = Filesystem.Mount(data buffer, archievename, mountpoint, prepend)
+ // successed = Filesystem.Mount(path, mountpoint[, prepend = false])
+ // successed = Filesystem.Mount(data buffer, archievename, mountpoint[, prepend = false])
LUAX_IMPL_METHOD(Filesystem, _Mount)
{
PREPARE(L);
@@ -177,7 +177,7 @@ namespace AsuraEngine
}
// successed = Filesystem.Append(path, data buffer)
- LUAX_IMPL_METHOD(Filesystem, _Write)
+ LUAX_IMPL_METHOD(Filesystem, _Append)
{
PREPARE(L);
@@ -219,8 +219,6 @@ namespace AsuraEngine
}
// fileinfo = Filesystem.GetFileInfo(path)
- // fileinfoĸʽ:
- // {size = , modtime = , type =}
LUAX_IMPL_METHOD(Filesystem, _GetFileInfo)
{
PREPARE(L);
@@ -231,7 +229,7 @@ namespace AsuraEngine
{
lua_newtable(L); // info table
state.SetField(-1, "size", info.size);
- state.SetField(-1, "modetime", info.modtime);
+ state.SetField(-1, "modtime", info.modtime);
state.SetField(-1, "type", info.type);
}
else
@@ -264,4 +262,4 @@ namespace AsuraEngine
}
}
-}
+} \ No newline at end of file
diff --git a/source/libs/asura-lib-utils/io/data_buffer.cpp b/source/libs/asura-lib-utils/io/data_buffer.cpp
index 98675e9..3c0100b 100644
--- a/source/libs/asura-lib-utils/io/data_buffer.cpp
+++ b/source/libs/asura-lib-utils/io/data_buffer.cpp
@@ -32,19 +32,27 @@ namespace AsuraEngine
delete[] mBytes;
}
- void DataBuffer::Load(DataBuffer& db)
- {
- Load(db.GetData(), db.GetSize());
- }
-
- void DataBuffer::Load(const void* data, std::size_t size)
+ void DataBuffer::Refactor(size_t size)
{
if (!mBytes || mSize != size)
{
delete[] mBytes;
mBytes = new byte[size];
+ mSize = size;
}
- memcpy(mBytes, data, size);
+ memset(mBytes, 0, size * sizeof(byte));
+ }
+
+ size_t DataBuffer::Load(DataBuffer& db)
+ {
+ return Load(db.GetData(), db.GetSize());
+ }
+
+ size_t DataBuffer::Load(const void* data, std::size_t size)
+ {
+ size_t len = mSize > size ? size : mSize;
+ memcpy(mBytes, data, len);
+ return len;
}
void DataBuffer::Move(void* bytes, std::size_t size)
diff --git a/source/libs/asura-lib-utils/io/data_buffer.h b/source/libs/asura-lib-utils/io/data_buffer.h
index c7383fa..c63a248 100644
--- a/source/libs/asura-lib-utils/io/data_buffer.h
+++ b/source/libs/asura-lib-utils/io/data_buffer.h
@@ -28,8 +28,9 @@ namespace AsuraEngine
byte* GetData();
size_t GetSize();
- void Load(DataBuffer& db);
- void Load(const void* bytes, std::size_t size);
+ void Refactor(size_t size);
+ size_t Load(DataBuffer& db);
+ size_t Load(const void* bytes, std::size_t size);
void Move(void* bytes, std::size_t size);
void Clear();
@@ -41,6 +42,7 @@ namespace AsuraEngine
LUAX_DECL_METHOD(_New);
LUAX_DECL_METHOD(_GetData);
LUAX_DECL_METHOD(_GetSize);
+ LUAX_DECL_METHOD(_Refactor);
LUAX_DECL_METHOD(_Load);
LUAX_DECL_METHOD(_Clear);
diff --git a/source/libs/asura-lib-utils/io/decoded_data.cpp b/source/libs/asura-lib-utils/io/decoded_data.cpp
index b208455..358a7a5 100644
--- a/source/libs/asura-lib-utils/io/decoded_data.cpp
+++ b/source/libs/asura-lib-utils/io/decoded_data.cpp
@@ -1,12 +1,13 @@
-#include "DecodedData.h"
-#include "Exceptions/Exception.h"
+#include "../exceptions/exception.h"
+
+#include "decoded_data.h"
namespace AsuraEngine
{
namespace IO
{
- DecodedData::DecodedData(const DataBuffer* databuffer)
+ DecodedData::DecodedData(const DataBuffer& databuffer)
{
Decode(databuffer);
}
diff --git a/source/libs/asura-lib-utils/io/file.cpp b/source/libs/asura-lib-utils/io/file.cpp
index 092a90d..976203d 100644
--- a/source/libs/asura-lib-utils/io/file.cpp
+++ b/source/libs/asura-lib-utils/io/file.cpp
@@ -38,7 +38,7 @@ namespace AsuraEngine
throw Exception("Physfs is NOT initialized.");
if (mode == FILE_MODE_CLOSED)
- return;
+ return false;
if (mode == FILE_MODE_READ && !PHYSFS_exists(mFileName.c_str()))
throw Exception("Could NOT open file %s. Does not exist.", mFileName.c_str());
@@ -53,7 +53,7 @@ namespace AsuraEngine
// Ѿ֮ǰ򿪹Ͳٴµhandle
if (mFileHandle != nullptr)
- return;
+ return true;
PHYSFS_getLastErrorCode();
@@ -83,7 +83,7 @@ namespace AsuraEngine
mFileHandle = handle;
mMode = mode;
- if (mFileHandle != nullptr && !SetBuffer(mBufferMode,mBufferSize))
+ if (mFileHandle && !SetBuffer(mBufferMode,mBufferSize))
{
mBufferMode = BUFFER_MODE_NONE;
mBufferSize = 0;
diff --git a/source/libs/asura-lib-utils/io/file.h b/source/libs/asura-lib-utils/io/file.h
index e8f676e..b09eaaa 100644
--- a/source/libs/asura-lib-utils/io/file.h
+++ b/source/libs/asura-lib-utils/io/file.h
@@ -123,6 +123,8 @@ namespace AsuraEngine
LUAX_DECL_METHOD(_SetBuffer);
LUAX_DECL_METHOD(_GetBuffer);
LUAX_DECL_METHOD(_GetFileName);
+ LUAX_DECL_METHOD(_GetExtension);
+ LUAX_DECL_METHOD(_GetName);
};
diff --git a/source/libs/asura-lib-utils/io/file_data.cpp b/source/libs/asura-lib-utils/io/file_data.cpp
index 47d9095..92333cf 100644
--- a/source/libs/asura-lib-utils/io/file_data.cpp
+++ b/source/libs/asura-lib-utils/io/file_data.cpp
@@ -23,11 +23,6 @@ namespace AsuraEngine
{
}
- void FileData::BindData(DataBuffer* buffer)
- {
- mData = buffer;
- }
-
const std::string& FileData::GetFileName()
{
return mFileName;
diff --git a/source/libs/asura-lib-utils/io/file_data.h b/source/libs/asura-lib-utils/io/file_data.h
index e20f41b..106e068 100644
--- a/source/libs/asura-lib-utils/io/file_data.h
+++ b/source/libs/asura-lib-utils/io/file_data.h
@@ -24,6 +24,8 @@ namespace AsuraEngine
LUAX_DECL_FACTORY(FileData);
+ ~FileData();
+
///
/// ļݣͨDatabufferݺʹСڲӿڶData bufferΪҲdata buffer
///
@@ -38,7 +40,6 @@ namespace AsuraEngine
friend class Filesystem;
FileData(const std::string& name);
- ~FileData();
///
/// data buffer
diff --git a/source/libs/asura-lib-utils/io/file_system.h b/source/libs/asura-lib-utils/io/file_system.h
index 1af0292..849cbb6 100644
--- a/source/libs/asura-lib-utils/io/file_system.h
+++ b/source/libs/asura-lib-utils/io/file_system.h
@@ -44,6 +44,8 @@ namespace AsuraEngine
LUAX_DECL_SINGLETON(Filesystem);
+ ~Filesystem();
+
void Init(const char* arg0);
///
@@ -70,7 +72,7 @@ namespace AsuraEngine
FileData* Read(const std::string& path);
bool GetFileInfo(const std::string& path, ASURA_OUT FileInfo* info);
- bool GetDirectoryItems(const std::string& path, ASURA_OUT std::vector<std::string>& items);
+ bool GetDirectoryItems(const std::string& path, ASURA_OUT std::vector<std::string>& items) { return false; };
private: