Storage initial

This commit is contained in:
IlushaShurupov 2023-07-18 23:36:51 +03:00 committed by Ilya Shurupov
parent bd1a6a44db
commit d5dfb1c304
14 changed files with 551 additions and 2 deletions

View 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();
}

View file

@ -0,0 +1,9 @@
#include "Storage.hpp"
static tp::ModuleManifest* sModuleDependencies[] = {
&tp::gModuleStrings,
nullptr
};
tp::ModuleManifest tp::gModuleStorage = ModuleManifest("Storage", nullptr, nullptr, sModuleDependencies);

View 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);
}

View 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();
};
}