data can now flush to file
This commit is contained in:
parent
fcec8da6bf
commit
23b0abd4cb
@ -15,14 +15,13 @@ class Data {
|
|||||||
public:
|
public:
|
||||||
rapidjson::Document document;
|
rapidjson::Document document;
|
||||||
|
|
||||||
|
|
||||||
explicit Data(std::string file);
|
explicit Data(std::string file);
|
||||||
|
|
||||||
virtual T createObject() = 0;
|
virtual T createObject() = 0;
|
||||||
|
|
||||||
std::string getFilePath();
|
std::string getFilePath();
|
||||||
|
|
||||||
|
void flushToFile();
|
||||||
private:
|
private:
|
||||||
const std::string fileName;
|
const std::string fileName;
|
||||||
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
||||||
|
@ -8,13 +8,24 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <rapidjson/stringbuffer.h>
|
||||||
|
#include <rapidjson/writer.h>
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
Data<T>::Data(std::string file) : fileName(std::move(file)) {
|
Data<T>::Data(std::string file) : fileName(std::move(file)) {
|
||||||
// Create file if it doesnt exist
|
// Create file if it doesnt exist
|
||||||
std::fstream fstream(getFilePath(), std::ios::out | std::ios::app);
|
std::ifstream chkExistIfs;
|
||||||
fstream.close();
|
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 ifstream(getFilePath(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
std::ifstream::pos_type fileSize = ifstream.tellg();
|
std::ifstream::pos_type fileSize = ifstream.tellg();
|
||||||
@ -32,3 +43,14 @@ template<class T>
|
|||||||
std::string Data<T>::getFilePath() {
|
std::string Data<T>::getFilePath() {
|
||||||
return fileName;
|
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 <string>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <getopt.h>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace Budget;
|
using namespace Budget;
|
||||||
|
|
||||||
const std::string homeDirectory = getpwuid(getuid())->pw_dir;
|
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() {
|
void createRequiredFolders() {
|
||||||
std::filesystem::create_directory(homeDirectory + "/.config/budget/");
|
std::filesystem::create_directory(configD);
|
||||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget");
|
std::filesystem::create_directories(storageD);
|
||||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/accounts");
|
std::filesystem::create_directories(storageD + "accounts");
|
||||||
std::filesystem::create_directories(homeDirectory + "/.local/share/budget/receipts");
|
std::filesystem::create_directories(storageD + "receipts");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
std::vector<char *> args(argv, argv + argc);
|
std::vector<char *> args(argv, argv + argc);
|
||||||
|
std::cout << opterr;
|
||||||
|
|
||||||
|
// Parse main options (-ah)
|
||||||
OptHandlers::MainOptHandler mainOptHandler(args);
|
OptHandlers::MainOptHandler mainOptHandler(args);
|
||||||
mainOptHandler.parse();
|
mainOptHandler.parse();
|
||||||
if (mainOptHandler.getSetOpts()->help)
|
if (mainOptHandler.getSetOpts()->help)
|
||||||
mainOptHandler.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();
|
createRequiredFolders();
|
||||||
// Read all accounts saved and store them in accounts
|
// Read all accounts saved and store them in accounts
|
||||||
std::list<AccountData> accounts;
|
std::list<AccountData> accounts;
|
||||||
@ -50,5 +44,21 @@ int main(int argc, char *argv[]) {
|
|||||||
homeDirectory + "/.local/share/budget/accounts")) {
|
homeDirectory + "/.local/share/budget/accounts")) {
|
||||||
accounts.emplace_back(AccountData(file.path()));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,10 @@ void AccountOptHandler::parse() {
|
|||||||
{"delete", required_argument, nullptr, 'd'},
|
{"delete", required_argument, nullptr, 'd'},
|
||||||
{"create", required_argument, nullptr, 'c'},
|
{"create", required_argument, nullptr, 'c'},
|
||||||
{"value", required_argument, nullptr, 'v'},
|
{"value", required_argument, nullptr, 'v'},
|
||||||
{nullptr}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
while (true) {
|
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) {
|
if (opt == -1) {
|
||||||
break;
|
break;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// Created by quentin on 8/13/22.
|
// Created by quentin on 8/13/22.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include "mainOptHandler.h"
|
#include "mainOptHandler.h"
|
||||||
|
|
||||||
using namespace Budget::OptHandlers;
|
using namespace Budget::OptHandlers;
|
||||||
@ -13,7 +14,6 @@ void MainOptHandler::parse() {
|
|||||||
struct option longOpts[] = {
|
struct option longOpts[] = {
|
||||||
{"help", no_argument, nullptr, 'h'},
|
{"help", no_argument, nullptr, 'h'},
|
||||||
{"account", no_argument, nullptr, 'a'},
|
{"account", no_argument, nullptr, 'a'},
|
||||||
{nullptr}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user