data can now flush to file
This commit is contained in:
parent
fcec8da6bf
commit
23b0abd4cb
@ -15,14 +15,13 @@ class Data {
|
||||
public:
|
||||
rapidjson::Document document;
|
||||
|
||||
|
||||
explicit Data(std::string file);
|
||||
|
||||
virtual T createObject() = 0;
|
||||
|
||||
std::string getFilePath();
|
||||
|
||||
|
||||
|
||||
void flushToFile();
|
||||
private:
|
||||
const std::string fileName;
|
||||
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
||||
|
@ -8,13 +8,24 @@
|
||||
#include <utility>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <rapidjson/stringbuffer.h>
|
||||
#include <rapidjson/writer.h>
|
||||
|
||||
|
||||
template<class T>
|
||||
Data<T>::Data(std::string file) : fileName(std::move(file)) {
|
||||
// Create file if it doesnt exist
|
||||
std::fstream fstream(getFilePath(), std::ios::out | std::ios::app);
|
||||
fstream.close();
|
||||
std::ifstream chkExistIfs;
|
||||
chkExistIfs.open(getFilePath());
|
||||
if (chkExistIfs) {
|
||||
// File exists, were not creating one
|
||||
chkExistIfs.close();
|
||||
}
|
||||
else {
|
||||
// File doesnt exist we need to create one
|
||||
chkExistIfs.close();
|
||||
|
||||
};
|
||||
|
||||
std::ifstream ifstream(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||
std::ifstream::pos_type fileSize = ifstream.tellg();
|
||||
@ -32,3 +43,14 @@ template<class T>
|
||||
std::string Data<T>::getFilePath() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Data<T>::flushToFile() {
|
||||
rapidjson::StringBuffer buffer;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
||||
document.Accept(writer);
|
||||
|
||||
std::ofstream file(getFilePath());
|
||||
file << buffer.GetString();
|
||||
file.close();
|
||||
}
|
||||
|
40
src/main.cpp
40
src/main.cpp
@ -12,37 +12,31 @@
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <list>
|
||||
#include <getopt.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Budget;
|
||||
|
||||
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
||||
const std::string configD = homeDirectory + "/.config/budget/";
|
||||
const std::string storageD = homeDirectory + "/.local/share/budget/";
|
||||
|
||||
void createRequiredFolders() {
|
||||
std::filesystem::create_directory(homeDirectory + "/.config/budget/");
|
||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget");
|
||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/accounts");
|
||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/receipts");
|
||||
std::filesystem::create_directory(configD);
|
||||
std::filesystem::create_directories(storageD);
|
||||
std::filesystem::create_directories(storageD + "accounts");
|
||||
std::filesystem::create_directories(storageD + "receipts");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::vector<char *> args(argv, argv + argc);
|
||||
std::cout << opterr;
|
||||
|
||||
// Parse main options (-ah)
|
||||
OptHandlers::MainOptHandler mainOptHandler(args);
|
||||
mainOptHandler.parse();
|
||||
if (mainOptHandler.getSetOpts()->help)
|
||||
mainOptHandler.help();
|
||||
|
||||
if (mainOptHandler.getSetOpts()->account) {
|
||||
std::vector<char *> vec{argv[0]};
|
||||
vec.insert(vec.end(), args.begin() + mainOptHandler.getSetOpts()->accountArgStart, args.end());
|
||||
OptHandlers::AccountOptHandler accountOptHandler(vec);
|
||||
accountOptHandler.parse();
|
||||
if (accountOptHandler.getSetOpts()->help) {
|
||||
accountOptHandler.help();
|
||||
}
|
||||
}
|
||||
|
||||
createRequiredFolders();
|
||||
// Read all accounts saved and store them in accounts
|
||||
std::list<AccountData> accounts;
|
||||
@ -50,5 +44,21 @@ int main(int argc, char *argv[]) {
|
||||
homeDirectory + "/.local/share/budget/accounts")) {
|
||||
accounts.emplace_back(AccountData(file.path()));
|
||||
}
|
||||
|
||||
// Parse account options if main options tells us to.
|
||||
if (mainOptHandler.getSetOpts()->account) {
|
||||
std::vector<char *> vec{argv[0]};
|
||||
vec.insert(vec.end(), args.begin() + mainOptHandler.getSetOpts()->accountArgStart, args.end());
|
||||
OptHandlers::AccountOptHandler accountOptHandler(vec);
|
||||
accountOptHandler.parse();
|
||||
|
||||
// Do what we need to do for parsed options
|
||||
if (accountOptHandler.getSetOpts()->help) {
|
||||
accountOptHandler.help();
|
||||
}
|
||||
if (accountOptHandler.getSetOpts()->create) {
|
||||
AccountData account(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -15,11 +15,10 @@ void AccountOptHandler::parse() {
|
||||
{"delete", required_argument, nullptr, 'd'},
|
||||
{"create", required_argument, nullptr, 'c'},
|
||||
{"value", required_argument, nullptr, 'v'},
|
||||
{nullptr}
|
||||
};
|
||||
|
||||
while (true) {
|
||||
int opt = getopt_long(getArgc(), getArgv(), "hl::d::c::va", longOpts, nullptr);
|
||||
int opt = getopt_long(getArgc(), getArgv(), "hld:c:v:a", longOpts, nullptr);
|
||||
|
||||
if (opt == -1) {
|
||||
break;
|
||||
|
@ -2,6 +2,7 @@
|
||||
// Created by quentin on 8/13/22.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "mainOptHandler.h"
|
||||
|
||||
using namespace Budget::OptHandlers;
|
||||
@ -13,7 +14,6 @@ void MainOptHandler::parse() {
|
||||
struct option longOpts[] = {
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"account", no_argument, nullptr, 'a'},
|
||||
{nullptr}
|
||||
};
|
||||
|
||||
while (true) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user