aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Filesystem
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Filesystem')
-rw-r--r--src/libjin/Filesystem/je_asset_database.cpp35
-rw-r--r--src/libjin/Filesystem/je_asset_database.h71
-rw-r--r--src/libjin/Filesystem/je_buffer.h156
3 files changed, 214 insertions, 48 deletions
diff --git a/src/libjin/Filesystem/je_asset_database.cpp b/src/libjin/Filesystem/je_asset_database.cpp
index c945d6a..edc3661 100644
--- a/src/libjin/Filesystem/je_asset_database.cpp
+++ b/src/libjin/Filesystem/je_asset_database.cpp
@@ -9,21 +9,21 @@ namespace JinEngine
namespace Filesystem
{
- AssetDatabase* AssetDatabase::fs = 0;
+ AssetDatabase* AssetDatabase::mAssetDatabase = 0;
AssetDatabase::AssetDatabase()
{
- S = smtnewshared();
+ mSmt = smtnewshared();
}
AssetDatabase* AssetDatabase::get()
{
- return fs ? fs : (fs = new AssetDatabase());
+ return mAssetDatabase ? mAssetDatabase : (mAssetDatabase = new AssetDatabase());
}
void AssetDatabase::mount(const char * path)
{
- int err = smtmount(S, path);
+ int err = smtmount(mSmt, path);
if (err)
{
printf("%s mounted path %s", smterrstr(err), path);
@@ -31,37 +31,44 @@ namespace JinEngine
}
}
- int AssetDatabase::read(const char* path, Buffer* buffer)
+ bool AssetDatabase::read(const char* path, Buffer& buffer)
{
- buffer->data = smtread(S, path, &buffer->size);
- if (buffer->data == 0)
- return 0;
- return 1;
+ size_t size;
+ byte* data = (byte*)smtread(mSmt, path, &size);
+ if (data == nullptr)
+ return false;
+ buffer.bind(data, size);
+ return true;
}
+ Buffer* read(const char* path)
+ {
+
+ }
+
void* AssetDatabase::read(const char* path, unsigned int* len)
{
- return smtread(S, path, len);
+ return smtread(mSmt, path, len);
}
const char* AssetDatabase::getFull(const char* path)
{
- return smtfullpath(S, path);
+ return smtfullpath(mSmt, path);
}
bool AssetDatabase::isDir(const char* path)
{
- return smtisdir(S, path);
+ return smtisdir(mSmt, path);
}
bool AssetDatabase::isFile(const char* path)
{
- return smtisreg(S, path);
+ return smtisreg(mSmt, path);
}
bool AssetDatabase::exists(const char* path)
{
- return smtexists(S, path) == 0;
+ return smtexists(mSmt, path) == 0;
}
} // namespace Filesystem
diff --git a/src/libjin/Filesystem/je_asset_database.h b/src/libjin/Filesystem/je_asset_database.h
index b8f96f6..81cce64 100644
--- a/src/libjin/Filesystem/je_asset_database.h
+++ b/src/libjin/Filesystem/je_asset_database.h
@@ -8,25 +8,84 @@ namespace JinEngine
{
namespace Filesystem
{
- /* Դ */
- class AssetDatabase
+
+ ///
+ /// Assets managment.
+ ///
+ class AssetDatabase
{
public:
+ ///
+ /// Get asset database singleton.
+ ///
+ /// @param Singleton of asset database.
+ ///
static AssetDatabase* get();
+ ///
+ /// Asset database constructor.
+ ///
AssetDatabase();
+ ///
+ /// Check if the path is directory.
+ ///
+ /// @param path Path under asset folder.
+ /// @return True if the given path is directory, otherwise return false.
+ ///
bool isDir(const char* path);
+
+ ///
+ /// Check if the path is file.
+ ///
+ /// @param path Path under asset folder.
+ /// @return True if the given path is file, otherwise return false.
+ ///
bool isFile(const char* path);
+
+ ///
+ /// Check if the path exists.
+ /// @param path Given path.
+ /// @return True if path exists, otherwise return false.
+ ///
bool exists(const char* path);
- int read(const char* path, Buffer* buffer);
- void* read(const char* path, unsigned int* len);
+
+ ///
+ /// Read file into a buffer.
+ ///
+ /// @param path Path of file.
+ /// @param buffer Buffer to fill.
+ /// @return True if read sucessful, otherwise return false.
+ ///
+ bool read(const char* path, Buffer& buffer);
+
+ ///
+ /// Read file and return data content.
+ ///
+ /// @param path Path of file.
+ /// @param length Length of data.
+ /// @return Data if read sucessful, otherwise return null.
+ ///
+ void* read(const char* path, unsigned int* length);
+
+ ///
+ /// Set asset root folder.
+ ///
+ /// @param root Root folder of assets.
+ ///
void mount(const char* root);
+
+ ///
+ /// Get full path of asset.
+ ///
+ /// @param path Path of asset.
+ /// @return Full path of asset.
+ ///
const char* getFull(const char* path);
private:
- static AssetDatabase* fs;
- smtShared* S;
+ static AssetDatabase* mAssetDatabase;
+ smtShared* mSmt;
};
diff --git a/src/libjin/Filesystem/je_buffer.h b/src/libjin/Filesystem/je_buffer.h
index 11faf79..18a381d 100644
--- a/src/libjin/Filesystem/je_buffer.h
+++ b/src/libjin/Filesystem/je_buffer.h
@@ -4,61 +4,161 @@
#include <string.h>
#include <stdlib.h>
+#include "../common/je_types.h"
+
namespace JinEngine
{
namespace Filesystem
{
- /**
- * ڶϷָռbuffer
- */
+ ///
+ /// Data buffer allocated on heap.
+ ///
class Buffer
{
public:
- Buffer() : data(0), size(0) {}
+ ///
+ /// Buffer constructor.
+ ///
+ Buffer()
+ : mData(0)
+ , mSize(0)
+ {
+ }
+
+ ///
+ /// Copy constructor.
+ ///
+ /// @param src Buffer source.
+ ///
Buffer(const Buffer& src)
{
- delete[] data;
- size = src.size;
- data = new char[size];
- memcpy(data, src.data, size);
+ delete[] mData;
+ mSize = src.mSize;
+ mData = new byte[mSize];
+ memcpy(mData, src.mData, mSize);
}
- Buffer(void* d, int s)
+
+ ///
+ /// Buffer constructor.
+ ///
+ /// @param data Buffer data.
+ /// @param size Size of buffer.
+ ///
+ Buffer(void* data, int size)
{
- data = new char[size];
- memcpy(data, d, size);
- size = s;
+ mSize = size;
+ mData = new byte[mSize];
+ memcpy(mData, data, mSize);
}
- Buffer(size_t s)
+
+ ///
+ /// Buffer constructor.
+ ///
+ /// @param size Size of data.
+ ///
+ Buffer(size_t size)
{
- data = new char[s];
- memset(data, 0, s);
- size = s;
+ mData = new byte[size];
+ memset(mData, 0, size);
+ mSize = size;
}
+
+ ///
+ /// Buffer destructor.
+ ///
~Buffer()
{
- delete[] data;
- size = 0;
+ delete[] mData;
+ mSize = 0;
}
+
+ ///
+ /// Set buffer data.
+ ///
+ /// @param data Buffer data.
+ /// @param size Size of data.
+ ///
+ void set(byte* data, size_t size)
+ {
+ if (data == nullptr)
+ return;
+ if (mSize != size)
+ {
+ delete mData;
+ mData = new byte[size];
+ }
+ mSize = size;
+ memcpy(mData, data, size);
+ }
+
+ ///
+ /// Bind buffer data.
+ ///
+ /// @param data Buffer data.
+ /// @param size Size of buffer.
+ ///
+ void bind(byte* data, size_t size)
+ {
+ if (mData != nullptr)
+ delete mData;
+ mSize = size;
+ mData = data;
+ }
+
+ ///
+ /// Buffer assignment.
+ ///
+ /// @param buffer Buffer to copy.
+ ///
void operator = (const Buffer& buffer)
{
- delete[] data;
- size = buffer.size;
- data = new char[size];
- memcpy(data, buffer.data, size);
+ delete[] mData;
+ mSize = buffer.mSize;
+ mData = new byte[mSize];
+ memcpy(mData, buffer.mData, mSize);
}
+ ///
+ /// Get data addresss.
+ ///
+ /// @return Data address.
+ ///
+ const byte* operator &()
+ {
+ return mData;
+ }
+
+ ///
+ /// Get data size.
+ ///
+ /// @return Size of data.
+ ///
+ const inline size_t size()
+ {
+ return mSize;
+ }
+
+ ///
+ /// Clear data.
+ ///
void clear()
{
- if (data == nullptr)
+ if (mData == nullptr)
return;
- free(data);
- data = nullptr;
+ delete mData;
+ mData = nullptr;
+ mSize = 0;
}
- void* data;
- unsigned int size;
-
+ private:
+ byte* mData;
+ size_t mSize;
+
+ // diasble new and delete
+ void* operator new(size_t t);
+ void operator delete(void*);
+
};
} // namespace Filesystem