From ac39fc0efdee1a868df1b612ae8779694f1065e2 Mon Sep 17 00:00:00 2001 From: Kokomichrzan Date: Sat, 23 Dec 2023 18:58:35 +0100 Subject: [PATCH] Add Vector and Structure Change --- Data.cpp | 199 +++++++++++++++++++++++++++++++++++++++++++------------ Data.h | 50 ++++++++++---- 2 files changed, 193 insertions(+), 56 deletions(-) diff --git a/Data.cpp b/Data.cpp index 9ad2362..01d29a3 100644 --- a/Data.cpp +++ b/Data.cpp @@ -2,62 +2,173 @@ //############################################## Data ##############################################// -Data::Data(const int FileSize, const std::string FileName, const std::string FilePath) +namespace Data { - //Set Size - Size = FileSize; - Content = new std::string[Size]; + Array::Array(const std::string FilePath, const int FileSize) + { + //Set Size + Size = FileSize; + Content = new std::string[Size]; - //Create File Paths - Path = std::filesystem::current_path() / FilePath / FileName; + //Create File Paths + Path = std::filesystem::current_path() / FilePath / ""; - //Create File IF Not Exist - if(!std::filesystem::exists(Path)){ - std::filesystem::create_directory(Path.parent_path()); - std::fstream Create(Path, std::ios::out); - Create.close(); + //Create Dir IF Not Exist + if (!std::filesystem::exists(Path)) std::filesystem::create_directory(Path); + + } + + std::vector Array::List(){ + std::vector Temp; + for (const auto& entry : std::filesystem::directory_iterator(Path)) Temp.push_back(entry.path()); + return Temp; } - //Read File - std::fstream FileData(Path, std::ios::in); - std::string Line; - for(int Count = 0; Count < Size; Count++){ - std::getline(FileData, Line); - Content[Count] = Line; + void Array::Read(const std::string FileName){ + //Create File IF Not Exist + if (!std::filesystem::exists(Path / FileName)) { + std::fstream Create(Path / FileName, std::ios::out); + Create.close(); + } + + //Read File + std::fstream FileData(Path / FileName, std::ios::in); + std::string Line; + for (int Count = 0; Count < Size; Count++) { + std::getline(FileData, Line); + Content[Count] = Line; + } + FileData.close(); + } - FileData.close(); -} + void Array::Save(const std::string FileName) { + //Create File IF Not Exist + if (!std::filesystem::exists(Path / FileName)) { + std::fstream Create(Path / FileName, std::ios::out); + Create.close(); + } + + //Create Temp String + std::string Temp; + for (int Count = 0; Count < Size; Count++) { + Temp += Content[Count] + "\n"; + } + Temp += "\0"; + + //Save Data + std::fstream FileData(Path / FileName, std::ios::out); + FileData << Temp; + FileData.close(); -void Data::Save(){ - //Create Temp String - std::string Temp; - for(int Count = 0; Count < Size; Count++){ - Temp += Content[Count] + "\n"; } - Temp += "\0"; - //Save Data - std::fstream FileData(Path, std::ios::out); - FileData << Temp; - FileData.close(); + void Array::Remove(const std::string FileName){ + std::filesystem::remove(Path / FileName); + } -} + bool Array::Exist(const std::string FileName) { + std::fstream FileData(Path / FileName, std::ios::in); + std::string Check; + FileData >> Check; + FileData.close(); + if (Check == "") return false; + return true; + + } -void Data::Add(const int Slot, const std::string Element){ - Content[Slot] = Element; -} + Array::~Array() { + delete[] Content; + } + + //############################################## Vector ##############################################// + + Vector::Vector(const std::string FilePath, const int FileSize) + { + //Set Size + Content.reserve(FileSize); + + //Create File Paths + Path = std::filesystem::current_path() / FilePath / ""; + + //Create Dir IF Not Exist + if (!std::filesystem::exists(Path)) std::filesystem::create_directory(Path); + + } + + std::vector Vector::List(){ + std::vector Temp; + for (const auto& entry : std::filesystem::directory_iterator(Path)) Temp.push_back(entry.path()); + return Temp; + } -bool Data::Exist(){ - std::fstream FileData(Path, std::ios::in); - std::string Check; - FileData >> Check; - FileData.close(); - if(Check == "") return false; - return true; + void Vector::Read(const std::string FileName){ + //Create File IF Not Exist + if (!std::filesystem::exists(Path / FileName)) { + std::fstream Create(Path / FileName, std::ios::out); + Create.close(); + } + + //Read File + std::fstream FileData(Path / FileName, std::ios::in); + std::string Line; + while (!FileData.eof()) { + std::getline(FileData, Line); + Content.emplace_back(Line); + } + FileData.close(); + + } + + void Vector::Save(const std::string FileName) { + //Create File IF Not Exist + if (!std::filesystem::exists(Path / FileName)) { + std::fstream Create(Path / FileName, std::ios::out); + Create.close(); + } + + //Create Temp String + std::string Temp; + for (int Count = 0; Count < Content.size(); Count++) { + Temp += Content[Count] + "\n"; + } + Temp += "\0"; + + //Save Data + std::fstream FileData(Path / FileName, std::ios::out); + FileData << Temp; + FileData.close(); + + } + + std::string Vector::Get(const int Slot){ + if(Slot >= (Content.size() - 1)) return ""; + return Content[Slot]; + } + + void Vector::Add(const std::string Element){ + Content.emplace_back(Element); + } + + void Vector::Change(const int Slot, const std::string Element){ //Need be Seted + Content[Slot] = Element; + } + + void Vector::Clear(){ + Content.clear(); + } + + void Vector::Remove(const std::string FileName){ + std::filesystem::remove(Path / FileName); + } -} + bool Vector::Exist(const std::string FileName) { + std::fstream FileData(Path / FileName, std::ios::in); + std::string Check; + FileData >> Check; + FileData.close(); + if (Check == "") return false; + return true; -Data::~Data(){ - delete[] Content; -} + }; +}; \ No newline at end of file diff --git a/Data.h b/Data.h index d234146..9e60e6f 100644 --- a/Data.h +++ b/Data.h @@ -1,21 +1,47 @@ #pragma once -#include #include +#include +#include //############################################## Data ##############################################// -class Data +namespace Data { -public: - std::string* Content; - std::filesystem::path Path; - int Size = 0; + //############################################## Array Format ##############################################// + class Array { + public: + std::string* Content; + std::filesystem::path Path; + int Size = 0; + + public: + Array(const std::string FilePath = "Data/", const int FileSize = 0); + std::vector List(); + void Read(const std::string FileName); + void Save(const std::string FileName); + void Remove(const std::string FileName); + bool Exist(const std::string FileName); + ~Array(); + }; + + //############################################## Vector Format ##############################################// + + class Vector { + public: + std::vector Content; + std::filesystem::path Path; -public: - Data(const int FileSize, const std::string FileName, const std::string FilePath = "Data/"); - void Save(); - void Add(const int Slot, const std::string Element); - bool Exist(); - ~Data(); + public: + Vector(const std::string FilePath = "Data/", const int FileSize = 0); + std::vector List(); + void Read(const std::string FileName); + void Save(const std::string FileName); + std::string Get(const int Slot); + void Add(const std::string Element); + void Change(const int Slot, const std::string Element); + void Clear(); + void Remove(const std::string FileName); + bool Exist(const std::string FileName); + }; };