Derives of Data now flush there contents on death

This commit is contained in:
quentin 2022-09-06 11:01:14 -05:00
parent 23b0abd4cb
commit cd45dd848f
8 changed files with 32 additions and 29 deletions

View File

@ -9,9 +9,15 @@ AccountData::AccountData(const std::string &file) : Data(file) {
account = AccountData::createObject();
}
AccountData::AccountData(const std::string &file, const std::string &name) : Data(file) {
std::string json = R"({"name":")" + name + R"(","transactions":[],"earnings":[]})";
document.Parse(json.c_str());
account = AccountData::createObject();
std::cout << "Created account " << name << std::endl;
}
Account AccountData::createObject() {
if (isJsonCorrect()) {
bool withdrawable = document["withdrawable"].GetBool();
std::string name = document["name"].GetString();
std::list<Transaction> transactions;
@ -35,13 +41,13 @@ Account AccountData::createObject() {
earnings.emplace_back(value, tmTime);
}
return Account(transactions, earnings, withdrawable, name);
return Account(transactions, earnings, name);
}
std::string strAnswer;
bool answer;
while (true) {
std::cout << "Account " + getFilePath() + " not found, would you like to remove it from the database? (Y/n): ";
std::cout << "Account " + getFilePath() + " Is malformed, would you like to remove it? (Y/n): ";
std::cin >> strAnswer;
std::transform(strAnswer.begin(), strAnswer.end(), strAnswer.begin(), ::toupper);
if (strAnswer == "Y" || strAnswer == "YES") {
@ -60,7 +66,6 @@ Account AccountData::createObject() {
bool AccountData::isJsonCorrect() {
if (document.IsObject() &&
document.HasMember("withdrawable") && document["withdrawable"].IsBool() &&
document.HasMember("name") && document["name"].IsString() &&
document.HasMember("transactions") && document["transactions"].IsArray() &&
document.HasMember("earnings") && document["earnings"].IsArray()) {

View File

@ -15,8 +15,9 @@ class AccountData : private Data<Account> {
public:
explicit AccountData(const std::string &file);
Account *getAccount();
explicit AccountData(const std::string &file, const std::string &name);
Account *getAccount();
private:
Account account;

View File

@ -15,6 +15,8 @@ class Data {
public:
rapidjson::Document document;
virtual ~Data();
explicit Data(std::string file);
virtual T createObject() = 0;

View File

@ -15,28 +15,23 @@
template<class T>
Data<T>::Data(std::string file) : fileName(std::move(file)) {
// Create file if it doesnt exist
std::ifstream chkExistIfs;
chkExistIfs.open(getFilePath());
std::ifstream chkExistIfs(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate);
if (chkExistIfs) {
// File exists, were not creating one
std::ifstream::pos_type fileSize = chkExistIfs.tellg();
chkExistIfs.seekg(0, std::ios::beg);
std::vector<char> bytes(fileSize);
chkExistIfs.read(bytes.data(), fileSize);
document.Parse(std::string(bytes.data(), fileSize).c_str());
chkExistIfs.close();
}
else {
// File doesnt exist we need to create one
// This is the job of the derives constructor.
chkExistIfs.close();
};
std::ifstream ifstream(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate);
std::ifstream::pos_type fileSize = ifstream.tellg();
ifstream.seekg(0, std::ios::beg);
std::vector<char> bytes(fileSize);
ifstream.read(bytes.data(), fileSize);
document.Parse(std::string(bytes.data(), fileSize).c_str());
ifstream.close();
}
template<class T>
@ -54,3 +49,8 @@ void Data<T>::flushToFile() {
file << buffer.GetString();
file.close();
}
template<class T>
Data<T>::~Data() {
flushToFile();
}

View File

@ -29,7 +29,6 @@ void createRequiredFolders() {
int main(int argc, char *argv[]) {
std::vector<char *> args(argv, argv + argc);
std::cout << opterr;
// Parse main options (-ah)
OptHandlers::MainOptHandler mainOptHandler(args);
@ -42,7 +41,7 @@ int main(int argc, char *argv[]) {
std::list<AccountData> accounts;
for (const auto &file : std::filesystem::directory_iterator(
homeDirectory + "/.local/share/budget/accounts")) {
accounts.emplace_back(AccountData(file.path()));
accounts.emplace_back(file.path());
}
// Parse account options if main options tells us to.
@ -57,7 +56,7 @@ int main(int argc, char *argv[]) {
accountOptHandler.help();
}
if (accountOptHandler.getSetOpts()->create) {
AccountData account(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json");
AccountData account(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json", accountOptHandler.getSetOpts()->createAccount);
}
}
return 0;

View File

@ -6,13 +6,11 @@
#include <utility>
Account::Account(std::list<Transaction> transactions, std::list<Earning> earnings, bool withdrawable,
Account::Account(std::list<Transaction> transactions, std::list<Earning> earnings,
std::string name) : transactions(std::move(transactions)), earnings(std::move(earnings)),
withdrawable(withdrawable),
name(std::move(name)) {}
Account::Account() {
withdrawable = true;
name = "";
}

View File

@ -15,8 +15,7 @@
class Account {
public:
Account(std::list<Transaction> transactions, std::list<Earning> earnings, bool withdrawable,
std::string name);
Account(std::list<Transaction> transactions, std::list<Earning> earnings, std::string name);
Account();
@ -28,7 +27,6 @@ public:
private:
std::list<Transaction> transactions;
std::list<Earning> earnings;
bool withdrawable;
std::string name;
};

View File

@ -18,7 +18,7 @@ void AccountOptHandler::parse() {
};
while (true) {
int opt = getopt_long(getArgc(), getArgv(), "hld:c:v:a", longOpts, nullptr);
int opt = getopt_long(getArgc(), getArgv(), "hl:d:c:va", longOpts, nullptr);
if (opt == -1) {
break;