diff options
author | chai <chaifix@163.com> | 2019-03-29 22:51:04 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-29 22:51:04 +0800 |
commit | c302f5ae5f9e30a28e487e8a764d9cc31546bbea (patch) | |
tree | 7f18bedeece950600336ea7ced7c52c468552c98 /source/libs/asura-lib-utils/io/file.cpp | |
parent | 157530b8b6e11efc5573d5a0db8987a440197aa1 (diff) |
*rename
Diffstat (limited to 'source/libs/asura-lib-utils/io/file.cpp')
-rw-r--r-- | source/libs/asura-lib-utils/io/file.cpp | 292 |
1 files changed, 0 insertions, 292 deletions
diff --git a/source/libs/asura-lib-utils/io/file.cpp b/source/libs/asura-lib-utils/io/file.cpp deleted file mode 100644 index 0ff8c90..0000000 --- a/source/libs/asura-lib-utils/io/file.cpp +++ /dev/null @@ -1,292 +0,0 @@ -#include <physfs/physfs.h> - -#include <asura-lib-utils/exceptions/exception.h> - -#include "file.h" - -namespace AsuraEngine -{ - namespace IO - { - - File::File(const std::string& filename) - : mFileName(filename) - , mFileHandle(nullptr) - , mMode(FILE_MODE_CLOSED) - , mBufferMode(BUFFER_MODE_NONE) - , mBufferSize(0) - { - size_t dot = filename.rfind('.'); - if (dot != std::string::npos) - { - mExtension = filename.substr(dot + 1); - mName = filename.substr(0, dot); - } - else - mName = filename; - } - - File::~File() - { - if (mMode != 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(mFileName.c_str())) - throw Exception("Could NOT open file %s. Does not exist.", mFileName.c_str()); - - if (mode == FILE_MODE_APPEND || mode == FILE_MODE_WRITE) - { - if (!PHYSFS_getWriteDir()) - { - throw Exception("Could NOT set write directory."); - } - } - - // Ѿ֮ǰͲٴµhandle - if (mFileHandle != nullptr) - return true; - - PHYSFS_getLastErrorCode(); - - PHYSFS_File* handle = nullptr; - - switch (mode) - { - case FILE_MODE_READ: - handle = PHYSFS_openRead(mFileName.c_str()); - break; - case FILE_MODE_APPEND: - handle = PHYSFS_openAppend(mFileName.c_str()); - break; - case FILE_MODE_WRITE: - handle = PHYSFS_openWrite(mFileName.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)", mFileName.c_str(), err); - } - - mFileHandle = handle; - mMode = mode; - - if (mFileHandle && !SetBuffer(mBufferMode,mBufferSize)) - { - mBufferMode = BUFFER_MODE_NONE; - mBufferSize = 0; - } - - return mFileHandle != nullptr; - } - - bool File::Close() - { - if (mFileHandle == nullptr || !PHYSFS_close(mFileHandle)) - return false; - mMode = FILE_MODE_CLOSED; - mFileHandle = nullptr; - return true; - } - - bool File::IsOpen() - { - return mMode != FILE_MODE_CLOSED && mFileHandle != nullptr; - } - - size_t File::GetSize() - { - if (mFileHandle == nullptr) - { - Open(FILE_MODE_READ); - size_t size = PHYSFS_fileLength(mFileHandle); - Close(); - return size; - } - return PHYSFS_fileLength(mFileHandle); - } - - size_t File::Read(ASURA_OUT DataBuffer* dst, size_t length) - { - ASSERT(dst); - - if (dst->GetSize() < length) - throw Exception("Data buffer is too small compares to read length."); - - if (!mFileHandle || mMode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", mFileName); - - size_t max = PHYSFS_fileLength(mFileHandle); - length = (length > max) ? max : length; - - if (length < 0) - throw Exception("Invalid read size."); - - dst->Lock(); - size_t size = PHYSFS_readBytes(mFileHandle, dst->GetData(), length); - dst->Unlock(); - return size; - } - - size_t File::ReadAll(ASURA_OUT DataBuffer* dst) - { - ASSERT(dst); - - if (!mFileHandle || mMode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", mFileName); - - size_t length = PHYSFS_fileLength(mFileHandle); - - if (dst->GetSize() < length) - throw Exception("Data buffer is too small compares to file length."); - - dst->Lock(); - size_t size = PHYSFS_readBytes(mFileHandle, dst->GetData(), 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 mFileHandle == nullptr || test_eof(this, mFileHandle); - } - - size_t File::Tell() - { - if (!mFileHandle) - return - 1; - - return PHYSFS_tell(mFileHandle); - } - - bool File::Seek(size_t pos) - { - return mFileHandle != nullptr && PHYSFS_seek(mFileHandle, pos) != 0; - } - - bool File::Write(ASURA_REF DataBuffer* src) - { - if (!mFileHandle || (mMode != FILE_MODE_APPEND && mMode != 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(mFileHandle, data, size); - - if (written != src->GetSize()) - return false; - - // л - if (mBufferSize == BUFFER_MODE_LINE && mBufferSize > size) - { - if (memchr(data, '\n', size) != nullptr) - Flush(); - } - - return true; - } - - bool File::Flush() - { - if (!mFileHandle || (mMode != FILE_MODE_WRITE && mMode != FILE_MODE_APPEND)) - throw Exception("File is not opened for writing."); - - return PHYSFS_flush(mFileHandle) != 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()) - { - mBufferMode = mode; - mBufferSize = size; - return true; - } - - int ret = 1; - - switch (mode) - { - case BUFFER_MODE_NONE: - default: - ret = PHYSFS_setBuffer(mFileHandle, 0); - size = 0; - break; - case BUFFER_MODE_LINE: - case BUFFER_MODE_FULL: - ret = PHYSFS_setBuffer(mFileHandle, size); - break; - } - - if (ret == 0) - return false; - - mBufferMode = mode; - mBufferSize = size; - - return true; - } - - File::BufferMode File::GetBuffer(ASURA_OUT size_t& size) - { - size = mBufferSize; - return mBufferMode; - } - - const std::string& File::GetFileName() - { - return mFileName; - } - - const std::string& File::GetName() - { - return mName; - } - - const std::string& File::GetExtension() - { - return mExtension; - } - - File::FileMode File::GetMode() - { - return mMode; - } - - } -}
\ No newline at end of file |