main's accounts list is now unordered_map and -a -v returns acc value
This commit is contained in:
parent
c2ada74e6b
commit
1d9c2010f9
@ -95,3 +95,7 @@ bool AccountData::isJsonCorrect() {
|
|||||||
Account *AccountData::getAccount() {
|
Account *AccountData::getAccount() {
|
||||||
return &account;
|
return &account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AccountData::AccountData() : Data("/dev/null") {
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -13,6 +13,10 @@ static const char *jsonTemplate = "";
|
|||||||
|
|
||||||
class AccountData : private Data<Account> {
|
class AccountData : private Data<Account> {
|
||||||
public:
|
public:
|
||||||
|
AccountData();
|
||||||
|
|
||||||
|
AccountData(const AccountData &) = default;
|
||||||
|
|
||||||
explicit AccountData(const std::string &file);
|
explicit AccountData(const std::string &file);
|
||||||
|
|
||||||
explicit AccountData(const std::string &file, const std::string &name);
|
explicit AccountData(const std::string &file, const std::string &name);
|
||||||
|
@ -17,6 +17,8 @@ public:
|
|||||||
|
|
||||||
virtual ~Data();
|
virtual ~Data();
|
||||||
|
|
||||||
|
Data(const Data &data);
|
||||||
|
|
||||||
explicit Data(std::string file);
|
explicit Data(std::string file);
|
||||||
|
|
||||||
virtual T createObject() = 0;
|
virtual T createObject() = 0;
|
||||||
|
@ -61,3 +61,24 @@ void Data<T>::deleteObject() {
|
|||||||
remove(getFilePath().c_str());
|
remove(getFilePath().c_str());
|
||||||
flush = false;
|
flush = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
Data<T>::Data(const Data &data) : fileName(data.fileName) {
|
||||||
|
// Create file if it doesnt exist
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
21
src/main.cpp
21
src/main.cpp
@ -11,7 +11,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <list>
|
#include <unordered_map>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace Budget;
|
using namespace Budget;
|
||||||
|
|
||||||
@ -37,10 +38,11 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
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::unordered_map<std::string, AccountData> accounts;
|
||||||
for (const auto &file : std::filesystem::directory_iterator(
|
for (const auto &file : std::filesystem::directory_iterator(
|
||||||
homeDirectory + "/.local/share/budget/accounts")) {
|
homeDirectory + "/.local/share/budget/accounts")) {
|
||||||
accounts.emplace_back(file.path());
|
AccountData account(file.path());
|
||||||
|
accounts.insert(std::pair(account.getAccount()->getName(), account));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse account options if main options tells us to.
|
// Parse account options if main options tells us to.
|
||||||
@ -55,8 +57,17 @@ int main(int argc, char *argv[]) {
|
|||||||
accountOptHandler.help();
|
accountOptHandler.help();
|
||||||
}
|
}
|
||||||
if (accountOptHandler.getSetOpts()->create) {
|
if (accountOptHandler.getSetOpts()->create) {
|
||||||
accounts.emplace_back(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount + ".json",
|
accounts.insert(std::pair(
|
||||||
accountOptHandler.getSetOpts()->createAccount);
|
accountOptHandler.getSetOpts()->createAccount,
|
||||||
|
(storageD + "accounts/" + accountOptHandler.getSetOpts()->createAccount +
|
||||||
|
".json", accountOptHandler.getSetOpts()->createAccount)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if (accountOptHandler.getSetOpts()->value) {
|
||||||
|
auto a = accounts.find(accountOptHandler.getSetOpts()->valueAccount);
|
||||||
|
if (a != accounts.end()) {
|
||||||
|
std::cout << "Account value: " << a->second.getAccount()->getValue() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -38,3 +38,7 @@ std::vector<DateMoney> Account::getTimeline() {
|
|||||||
|
|
||||||
return timeline;
|
return timeline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string &Account::getName() const {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
@ -23,6 +23,8 @@ public:
|
|||||||
|
|
||||||
std::vector<DateMoney> getTimeline();
|
std::vector<DateMoney> getTimeline();
|
||||||
|
|
||||||
|
const std::string &getName() const;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<Transaction> transactions;
|
std::list<Transaction> transactions;
|
||||||
|
@ -18,7 +18,7 @@ void AccountOptHandler::parse() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
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:", longOpts, nullptr);
|
||||||
|
|
||||||
if (opt == -1) {
|
if (opt == -1) {
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user