Storage initial
This commit is contained in:
parent
90998e2279
commit
df3767df29
14 changed files with 551 additions and 2 deletions
76
Storage/private/LocalStorage.cpp
Normal file
76
Storage/private/LocalStorage.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "LocalStorage.hpp"
|
||||
|
||||
#include "SystemHandle.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
bool File::connect(const FileLocation& location, const FileConnectionType& connectionInfo) {
|
||||
DEBUG_ASSERT(!mStatus.isOpened());
|
||||
if (mStatus.isOpened()) return false;
|
||||
|
||||
auto handle = new FileSystemHandle();
|
||||
|
||||
switch (connectionInfo.getType()) {
|
||||
case FileConnectionType::READ:
|
||||
handle->open(location.getLocation().read(), true);
|
||||
break;
|
||||
|
||||
case FileConnectionType::WRITE:
|
||||
handle->open(location.getLocation().read(), false);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
if (!handle->isOpen()) {
|
||||
mStatus.setStatus(FileConnectionStatus::DENIED);
|
||||
delete handle;
|
||||
return false;
|
||||
}
|
||||
|
||||
mStatus.setStatus(FileConnectionStatus::OPENED);
|
||||
mHandle = handle;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::disconnect() {
|
||||
DEBUG_ASSERT(mStatus.isOpened());
|
||||
if (!mStatus.isOpened() || !mHandle) return false;
|
||||
mHandle->close();
|
||||
delete mHandle;
|
||||
mStatus.setStatus(FileConnectionStatus::CLOSED);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::setPointer(BytePointer pointer) {
|
||||
DEBUG_ASSERT(mStatus.isOpened());
|
||||
if (!mStatus.isOpened()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::readBytes(Byte* bytes, SizeBytes size) {
|
||||
DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isWrite());
|
||||
if (!mStatus.isOpened() || !mConnectionType.isWrite()) return false;
|
||||
|
||||
mHandle->seekp(mPointer);
|
||||
mHandle->read(bytes, size);
|
||||
mPointer += size;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool File::writeBytes(const Byte* bytes, SizeBytes size) {
|
||||
DEBUG_ASSERT(mStatus.isOpened() && mConnectionType.isRead());
|
||||
if (!mStatus.isOpened() || !mConnectionType.isRead()) return false;
|
||||
|
||||
mHandle->seekp(mPointer);
|
||||
mHandle->write(bytes, size);
|
||||
mPointer += size;
|
||||
return true;
|
||||
}
|
||||
|
||||
File::SizeBytes File::size() {
|
||||
DEBUG_ASSERT(mStatus.isOpened());
|
||||
if (!mStatus.isOpened() || !mHandle) return 0;
|
||||
return mHandle->size();
|
||||
}
|
||||
9
Storage/private/Storage.cpp
Normal file
9
Storage/private/Storage.cpp
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
#include "Storage.hpp"
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
&tp::gModuleStrings,
|
||||
nullptr
|
||||
};
|
||||
|
||||
tp::ModuleManifest tp::gModuleStorage = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies);
|
||||
57
Storage/private/SystemHandle.cpp
Normal file
57
Storage/private/SystemHandle.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "SystemHandle.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
ualni tp::FileSystemHandle::size() {
|
||||
auto strm = (std::fstream*) stream;
|
||||
ualni out = 0;
|
||||
strm->seekg(0, std::ios::beg);
|
||||
out = strm->tellg();
|
||||
strm->seekg(0, std::ios::end);
|
||||
out = (ualni) strm->tellg() - out;
|
||||
return out;
|
||||
}
|
||||
|
||||
FileSystemHandle::FileSystemHandle() {
|
||||
stream = new std::fstream();
|
||||
}
|
||||
|
||||
FileSystemHandle::~FileSystemHandle() {
|
||||
auto strm = (std::fstream*) stream;
|
||||
delete strm;
|
||||
}
|
||||
|
||||
bool FileSystemHandle::isOpen() {
|
||||
auto strm = (std::fstream*) stream;
|
||||
return strm->is_open();
|
||||
}
|
||||
|
||||
void FileSystemHandle::close() {
|
||||
auto strm = (std::fstream*) stream;
|
||||
strm->close();
|
||||
}
|
||||
|
||||
void FileSystemHandle::seekp(ualni in) {
|
||||
auto strm = (std::fstream*) stream;
|
||||
strm->seekp(in);
|
||||
}
|
||||
|
||||
void FileSystemHandle::read(int1* in, ualni size) {
|
||||
auto strm = (std::fstream*) stream;
|
||||
strm->write(in, size);
|
||||
}
|
||||
|
||||
void FileSystemHandle::write(const int1* in, ualni size) {
|
||||
auto strm = (std::fstream*) stream;
|
||||
strm->write(in, size);
|
||||
}
|
||||
|
||||
void FileSystemHandle::open(const char* path, bool read) {
|
||||
auto strm = (std::fstream*) stream;
|
||||
if (read)
|
||||
strm->open(path, std::ios::in | std::ios::binary | std::ios::app);
|
||||
else
|
||||
strm->open(path, std::ios::out | std::ios::binary | std::ios::trunc);
|
||||
}
|
||||
19
Storage/private/SystemHandle.hpp
Normal file
19
Storage/private/SystemHandle.hpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include "Common.hpp"
|
||||
|
||||
namespace tp {
|
||||
class FileSystemHandle {
|
||||
void* stream;
|
||||
public:
|
||||
FileSystemHandle();
|
||||
~FileSystemHandle();
|
||||
void open(const char*, bool);
|
||||
bool isOpen();
|
||||
void close();
|
||||
void seekp(ualni);
|
||||
void read(int1*, ualni);
|
||||
void write(const int1*, ualni);
|
||||
ualni size();
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue