From 39a8e3e56a50331ccb3b47daddd073519b81584c Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Fri, 11 Aug 2023 08:22:02 +0200 Subject: [PATCH] clang-tidy --- include/currency.hpp | 4 +- include/date.hpp | 2 +- include/debts.hpp | 2 +- include/earnings.hpp | 2 +- include/expenses.hpp | 2 +- include/fortune.hpp | 2 +- include/incomes.hpp | 4 +- include/liabilities.hpp | 9 +++-- include/objectives.hpp | 2 +- include/wishes.hpp | 2 +- src/config.cpp | 3 +- src/console.cpp | 29 +++++++------- src/console_writer.cpp | 24 ++++++------ src/currency.cpp | 39 +++++++++++-------- src/data.cpp | 26 ++++++------- src/data_cache.cpp | 13 +++---- src/date.cpp | 20 +++++----- src/debts.cpp | 16 ++++---- src/earnings.cpp | 10 ++--- src/expenses.cpp | 12 +++--- src/fortune.cpp | 39 +++++++++---------- src/incomes.cpp | 18 ++++----- src/liabilities.cpp | 27 +++++++------ src/money.cpp | 17 +++++---- src/objectives.cpp | 16 ++++---- src/overview.cpp | 84 ++++++++++++++++++++--------------------- src/versioning.cpp | 4 +- src/wishes.cpp | 19 +++++----- 28 files changed, 224 insertions(+), 223 deletions(-) diff --git a/include/currency.hpp b/include/currency.hpp index 6fd85f50..5b80dea4 100644 --- a/include/currency.hpp +++ b/include/currency.hpp @@ -14,9 +14,9 @@ namespace budget { struct date; double exchange_rate(const std::string& from); -double exchange_rate(const std::string& from, budget::date d); +double exchange_rate(const std::string& from, const budget::date& d); double exchange_rate(const std::string& from, const std::string& to); -double exchange_rate(const std::string& from, const std::string& to, budget::date d); +double exchange_rate(const std::string& from, const std::string& to, const budget::date& d); void load_currency_cache(); void save_currency_cache(); diff --git a/include/date.hpp b/include/date.hpp index dd955c42..f84d3aef 100644 --- a/include/date.hpp +++ b/include/date.hpp @@ -478,7 +478,7 @@ date local_day(); date date_from_string(std::string_view str); -std::string date_to_string(date date); +std::string date_to_string(const date& date); template<> inline std::string to_string(budget::date date){ diff --git a/include/debts.hpp b/include/debts.hpp index 9e893607..aaa4d4d2 100644 --- a/include/debts.hpp +++ b/include/debts.hpp @@ -46,7 +46,7 @@ struct debt { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; void load_debts(); diff --git a/include/earnings.hpp b/include/earnings.hpp index 40c05361..63121a93 100644 --- a/include/earnings.hpp +++ b/include/earnings.hpp @@ -45,7 +45,7 @@ struct earning { std::map get_params() const ; void load(data_reader& reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; void load_earnings(); diff --git a/include/expenses.hpp b/include/expenses.hpp index 3edc84a0..9c3afa19 100644 --- a/include/expenses.hpp +++ b/include/expenses.hpp @@ -47,7 +47,7 @@ struct expense { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; void load_expenses(); diff --git a/include/fortune.hpp b/include/fortune.hpp index 22c4fd86..4ea6587e 100644 --- a/include/fortune.hpp +++ b/include/fortune.hpp @@ -42,7 +42,7 @@ struct fortune { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; budget::money current_fortune(); diff --git a/include/incomes.hpp b/include/incomes.hpp index db220c4d..16df02f1 100644 --- a/include/incomes.hpp +++ b/include/incomes.hpp @@ -43,7 +43,7 @@ struct income { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; void load_incomes(); @@ -70,7 +70,7 @@ void show_incomes(budget::writer& w); struct data_cache; budget::money get_base_income(data_cache & cache); -budget::money get_base_income(data_cache & cache, budget::date d); +budget::money get_base_income(data_cache& cache, const budget::date& d); budget::income new_income(budget::money amount, bool print); diff --git a/include/liabilities.hpp b/include/liabilities.hpp index ebcf93e7..b0988454 100644 --- a/include/liabilities.hpp +++ b/include/liabilities.hpp @@ -68,7 +68,7 @@ void save_liabilities(); void show_liabilities(budget::writer& w); budget::liability get_liability(size_t id); -budget::liability get_liability(std::string name); +budget::liability get_liability(const std::string& name); std::vector all_liabilities(); @@ -90,15 +90,16 @@ void migrate_liabilities_6_to_7(); // The value of a liability in its own currency budget::money get_liability_value(budget::liability & liability, data_cache & cache); -budget::money get_liability_value(budget::liability & liability, budget::date d, data_cache & cache); +budget::money get_liability_value(budget::liability& liability, const budget::date& d, data_cache& cache); // The value of a liability in the default currency budget::money get_liability_value_conv(budget::liability & liability, data_cache & cache); -budget::money get_liability_value_conv(budget::liability & liability, budget::date d, data_cache & cache); +budget::money get_liability_value_conv(budget::liability& liability, const budget::date& d, data_cache& cache); // The value of a liability in a specific currency budget::money get_liability_value_conv(budget::liability & liability, const std::string& currency, data_cache & cache); -budget::money get_liability_value_conv(budget::liability & liability, budget::date d, const std::string& currency, data_cache & cache); +budget::money get_liability_value_conv(budget::liability& liability, const budget::date& d, const std::string& currency, + data_cache& cache); // Utilities for liabilities void update_asset_class_allocation(budget::liability& liability, budget::asset_class & clas, budget::money alloc); diff --git a/include/objectives.hpp b/include/objectives.hpp index e2652e5d..0cbb276d 100644 --- a/include/objectives.hpp +++ b/include/objectives.hpp @@ -50,7 +50,7 @@ struct objective { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer& writer) const; }; void yearly_objective_status(budget::writer& w, bool lines, bool full_align); diff --git a/include/wishes.hpp b/include/wishes.hpp index 25b5f749..6f0503d2 100644 --- a/include/wishes.hpp +++ b/include/wishes.hpp @@ -47,7 +47,7 @@ struct wish { std::map get_params() const ; void load(data_reader & reader); - void save(data_writer & writer); + void save(data_writer & writer) const; }; void load_wishes(); diff --git a/src/config.cpp b/src/config.cpp index 0579e4e0..8c8eb258 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -131,9 +131,8 @@ bool verify_folder(){ return false; - } else { - return false; } + return false; } return true; diff --git a/src/console.cpp b/src/console.cpp index 6a31f9b0..47f7d9d4 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -29,21 +29,25 @@ std::string budget::format_reset() { std::string budget::format_money(const budget::money& m) { if (m.positive()) { return "::green" + budget::to_string(m); - } else if (m.negative()) { + } + + if (m.negative()) { return "::red" + budget::to_string(m); - } else { - return budget::to_string(m); } + + return budget::to_string(m); } std::string budget::format_money_reverse(const budget::money& m) { if (m.positive()) { return "::red" + budget::to_string(m); - } else if (m.negative()) { + } + + if (m.negative()) { return "::green" + budget::to_string(m); - } else { - return budget::to_string(m); } + + return budget::to_string(m); } size_t budget::rsize(const std::string& value) { @@ -113,7 +117,7 @@ namespace { char getch() { char buf = 0; - struct termios old; + struct termios old{}; fflush(stdout); if (tcgetattr(0, &old) < 0) { @@ -156,17 +160,14 @@ std::string budget::get_string_complete(const std::vector& choices) size_t index = 0; while (true) { - char c = getch(); + const char c = getch(); if (+c == 127) { if (!answer.empty()) { std::cout << "\b \b"; answer.pop_back(); } - } else if (c == '\r') { - std::cout << std::endl; - return answer; - } else if (c == '\n') { + } else if (c == '\r' || c == '\n') { std::cout << std::endl; return answer; } else if (c == '\t') { @@ -174,7 +175,7 @@ std::string budget::get_string_complete(const std::vector& choices) size_t count = 0; std::string valid; - for (auto& choice : choices) { + for (const auto& choice : choices) { if (choice.size() > answer.size()) { if (choice.substr(0, answer.size()) == answer) { ++count; @@ -191,7 +192,7 @@ std::string budget::get_string_complete(const std::vector& choices) } else if (c == '\033') { getch(); - char cc = getch(); + const char cc = getch(); if (cc == 'A') { for (size_t i = 0; i < answer.size(); ++i) { diff --git a/src/console_writer.cpp b/src/console_writer.cpp index 839628f6..3cfd198e 100644 --- a/src/console_writer.cpp +++ b/src/console_writer.cpp @@ -30,7 +30,7 @@ std::string success_to_string(int success) { ss << "%\033[0m "; success = std::min(success, 109); - size_t good = success == 0 ? 0 : (success / 10) + 1; + const size_t good = success == 0 ? 0 : (success / 10) + 1; for (size_t i = 0; i < good; ++i) { ss << "\033[1;42m \033[0m"; @@ -48,7 +48,8 @@ std::string format(const std::string& v) { auto value = v.substr(5); return "\033[0;31m" + value + budget::format_reset(); - } else if (v.substr(0, 7) == "::green") { + } + if (v.substr(0, 7) == "::green") { auto value = v.substr(7); return "\033[0;32m" + value + budget::format_reset(); @@ -57,7 +58,7 @@ std::string format(const std::string& v) { return "\033[0;33m" + value + budget::format_reset(); } else if (v.substr(0, 9) == "::success") { - auto value = v.substr(9); + auto value = v.substr(9); auto success = budget::to_number(value); return success_to_string(success); } @@ -70,7 +71,8 @@ std::string underline_format(const std::string& v) { auto value = v.substr(5); return "\033[4;31m" + value + budget::format_reset(); - } else if (v.substr(0, 7) == "::green") { + } + if (v.substr(0, 7) == "::green") { auto value = v.substr(7); return "\033[4;32m" + value + budget::format_reset(); @@ -79,7 +81,7 @@ std::string underline_format(const std::string& v) { return "\033[4;33m" + value + budget::format_reset(); } else if (v.substr(0, 9) == "::success") { - auto value = v.substr(9); + auto value = v.substr(9); auto success = budget::to_number(value); return success_to_string(success); } @@ -151,10 +153,10 @@ budget::writer& budget::console_writer::operator<<(const budget::title_end_t&) { void budget::console_writer::display_table(std::vector& columns, std::vector>& contents, size_t groups, std::vector lines, size_t left, [[maybe_unused]] size_t foot) { cpp_assert(groups > 0, "There must be at least 1 group"); - cpp_assert(contents.size() || columns.size(), "There must be at least some columns or contents"); + cpp_assert(!contents.empty() || !columns.empty(), "There must be at least some columns or contents"); // Remove the Edit column, if necessary - if (columns.size() && columns.back() == "Edit") { + if (!columns.empty() && columns.back() == "Edit") { columns.pop_back(); for (auto& row : contents) { @@ -171,7 +173,7 @@ void budget::console_writer::display_table(std::vector& columns, st std::vector widths; std::vector header_widths; - if (!contents.size()) { + if (contents.empty()) { for (auto& column : columns) { widths.push_back(rsize(column)); } @@ -187,7 +189,7 @@ void budget::console_writer::display_table(std::vector& columns, st } } - cpp_assert(columns.size() == 0 || widths.size() == groups * columns.size(), "Widths incorrectly computed"); + cpp_assert(columns.empty() || widths.size() == groups * columns.size(), "Widths incorrectly computed"); // Display the header @@ -241,7 +243,7 @@ void budget::console_writer::display_table(std::vector& columns, st auto& row = contents[i]; - bool underline = std::find(lines.begin(), lines.end(), i) != lines.end(); + const bool underline = std::find(lines.begin(), lines.end(), i) != lines.end(); for (size_t j = 0; j < row.size(); j += groups) { size_t acc_width = 0; @@ -250,7 +252,7 @@ void budget::console_writer::display_table(std::vector& columns, st for (size_t k = 0; k < groups - 1; ++k) { auto column = j + k; - std::string value = format(row[column]); + std::string const value = format(row[column]); acc_width += widths[column]; diff --git a/src/currency.cpp b/src/currency.cpp index c4a83b0e..1be468a7 100644 --- a/src/currency.cpp +++ b/src/currency.cpp @@ -26,7 +26,8 @@ struct currency_cache_key { std::string from; std::string to; - currency_cache_key(budget::date date, std::string_view from, std::string_view to) : date(date), from(from), to(to) {} + currency_cache_key(const budget::date& date, std::string_view from, std::string_view to) + : date(date), from(from), to(to) {} friend bool operator<=>(const currency_cache_key & lhs, const currency_cache_key & rhs) = default; }; @@ -83,24 +84,28 @@ currency_cache_value get_rate_v2(const std::string& from, const std::string& to, LOG_F(ERROR, "Currency(v2): URL is {}", url); return {1.0, false}; - } else if (res->status != 200) { - LOG_F(ERROR, "Currency(v2): Error Response {}, setting exchange between {} to {} to 1/1", res->status, from, to); + } + if (res->status != 200) { + LOG_F(ERROR, "Currency(v2): Error Response {}, setting exchange between {} to {} to 1/1", res->status, from, + to); LOG_F(ERROR, "Currency(v2): URL is {}", url); LOG_F(ERROR, "Currency(v2): Response is {}", res->body); return {1.0, false}; } else { auto& buffer = res->body; - auto index = "\"" + to + "\":"; + auto index = "\"" + to + "\":"; if (buffer.find(index) == std::string::npos || buffer.find('}') == std::string::npos) { - LOG_F(ERROR, "Currency(v2): Error parsing exchange rates, setting exchange between {} to {} to 1/1", from, to); + LOG_F(ERROR, "Currency(v2): Error parsing exchange rates, setting exchange between {} to {} to 1/1", from, + to); LOG_F(ERROR, "Currency(v2): URL is {}", url); LOG_F(ERROR, "Currency(v2): Response is {}", res->body); return {1.0, false}; } else { - std::string ratio_result(buffer.begin() + buffer.find(index) + index.size(), buffer.begin() + buffer.find('}')); + std::string ratio_result(buffer.begin() + buffer.find(index) + index.size(), + buffer.begin() + buffer.find('}')); return {atof(ratio_result.c_str()), true}; } @@ -110,7 +115,7 @@ currency_cache_value get_rate_v2(const std::string& from, const std::string& to, } // end of anonymous namespace void budget::load_currency_cache(){ - std::string file_path = budget::path_to_budget_file("currency.cache"); + std::string const file_path = budget::path_to_budget_file("currency.cache"); std::ifstream file(file_path); if (!file.is_open() || !file.good()){ @@ -126,7 +131,7 @@ void budget::load_currency_cache(){ auto parts = splitv(line, ':'); - currency_cache_key key(date_from_string(parts[0]), parts[1], parts[2]); + const currency_cache_key key(date_from_string(parts[0]), parts[1], parts[2]); exchanges[key] = {budget::to_number(parts[3]), true}; } @@ -135,7 +140,7 @@ void budget::load_currency_cache(){ } void budget::save_currency_cache() { - std::string file_path = budget::path_to_budget_file("currency.cache"); + std::string const file_path = budget::path_to_budget_file("currency.cache"); std::ofstream file(file_path); if (!file.is_open() || !file.good()){ @@ -144,7 +149,7 @@ void budget::save_currency_cache() { } { - server_lock_guard l(exchanges_lock); + const server_lock_guard l(exchanges_lock); for (auto & [key, value] : exchanges) { // We only write down valid values @@ -162,7 +167,7 @@ void budget::refresh_currency_cache(){ std::unordered_map copy; { - server_lock_guard l(exchanges_lock); + const server_lock_guard l(exchanges_lock); copy = exchanges; } @@ -184,16 +189,17 @@ double budget::exchange_rate(const std::string& from, const std::string& to){ return exchange_rate(from, to, budget::local_day()); } -double budget::exchange_rate(const std::string& from, budget::date d){ +double budget::exchange_rate(const std::string& from, const budget::date& d) { return exchange_rate(from, get_default_currency(), d); } -double budget::exchange_rate(const std::string& from, const std::string& to, budget::date d){ +double budget::exchange_rate(const std::string& from, const std::string& to, const budget::date& d) { assert(from != "DESIRED" && to != "DESIRED"); if (from == to) { return 1.0; - } else if (d > budget::local_day()) { + } + if (d > budget::local_day()) { return exchange_rate(from, to, budget::local_day()); } else { currency_cache_key key(d, from, to); @@ -211,7 +217,8 @@ double budget::exchange_rate(const std::string& from, const std::string& to, bud auto rate = get_rate_v2(from, to, date_to_string(d)); - LOG_F(INFO, "Price: Currency Rate ({}) from {} to {} = {} (valid: {})", budget::to_string(d), from, to, budget::to_string(rate.value), rate.valid); + LOG_F(INFO, "Price: Currency Rate ({}) from {} to {} = {} (valid: {})", budget::to_string(d), from, to, + budget::to_string(rate.value), rate.valid); // Update the cache and the reverse cache with the lock @@ -220,7 +227,7 @@ double budget::exchange_rate(const std::string& from, const std::string& to, bud { server_lock_guard l(exchanges_lock); - exchanges[key] = rate; + exchanges[key] = rate; exchanges[reverse_key] = {1.0 / rate.value, rate.valid}; } diff --git a/src/data.cpp b/src/data.cpp index 197e456c..ca1a9d4c 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -35,7 +35,7 @@ std::string parse_output(const std::vector& parts) { for (auto part : parts) { size_t start_pos = 0; - while ((start_pos = part.find(":", start_pos)) != std::string::npos) { + while ((start_pos = part.find(':', start_pos)) != std::string::npos) { part.replace(start_pos, 1, "\\x3A"); start_pos += 4; } @@ -62,7 +62,7 @@ void budget::data_reader::parse(const std::string& data) { budget::data_reader& budget::data_reader::operator>>(bool& value) { const auto & part = parts.at(current); - size_t temp; + size_t temp = 0; if (auto [p, ec] = std::from_chars(part.data(), part.data() + part.size(), temp); ec != std::errc() || p != part.data() + part.size()) { throw budget::budget_exception("\"" + parts.at(current) + "\" is not a valid bool"); } @@ -152,49 +152,45 @@ void budget::data_reader::skip() { // data_writer budget::data_writer& budget::data_writer::operator<<(const bool& value){ - size_t temp = value; + const size_t temp = value; - std::array buffer; + std::array buffer{}; if (auto [p, ec] = std::to_chars(buffer.begin(), buffer.end(), temp); ec == std::errc()) { parts.emplace_back(buffer.begin(), p); return *this; - } else { - throw budget::budget_exception("\"" + std::to_string(temp) + "\" cant' be converted to string"); } + throw budget::budget_exception("\"" + std::to_string(temp) + "\" cant' be converted to string"); } budget::data_writer& budget::data_writer::operator<<(const size_t& value){ - std::array buffer; + std::array buffer{}; if (auto [p, ec] = std::to_chars(buffer.begin(), buffer.end(), value); ec == std::errc()) { parts.emplace_back(buffer.begin(), p); return *this; - } else { - throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } + throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } budget::data_writer& budget::data_writer::operator<<(const int64_t& value){ - std::array buffer; + std::array buffer{}; if (auto [p, ec] = std::to_chars(buffer.begin(), buffer.end(), value); ec == std::errc()) { parts.emplace_back(buffer.begin(), p); return *this; - } else { - throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } + throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } budget::data_writer& budget::data_writer::operator<<(const int32_t& value){ - std::array buffer; + std::array buffer{}; if (auto [p, ec] = std::to_chars(buffer.begin(), buffer.end(), value); ec == std::errc()) { parts.emplace_back(buffer.begin(), p); return *this; - } else { - throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } + throw budget::budget_exception("\"" + std::to_string(value) + "\" cant' be converted to string"); } budget::data_writer& budget::data_writer::operator<<(const std::string& value){ diff --git a/src/data_cache.cpp b/src/data_cache.cpp index 696d49dd..43115aea 100644 --- a/src/data_cache.cpp +++ b/src/data_cache.cpp @@ -75,15 +75,14 @@ std::unordered_map> & data_cache::sorted_group_ } return sorted_group_asset_values_liabilities_; - } else { - if (sorted_group_asset_values_.empty()) { - for (auto& asset_value : sorted_asset_values() | not_liability) { - sorted_group_asset_values_[asset_value.asset_id].push_back(asset_value); - } + } + if (sorted_group_asset_values_.empty()) { + for (auto& asset_value : sorted_asset_values() | not_liability) { + sorted_group_asset_values_[asset_value.asset_id].push_back(asset_value); } - - return sorted_group_asset_values_; } + + return sorted_group_asset_values_; } std::vector & data_cache::liabilities() { diff --git a/src/date.cpp b/src/date.cpp index 7290da58..c4fc1c66 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -16,11 +16,9 @@ #include "data_cache.hpp" #include "views.hpp" -namespace ranges = std::ranges; - budget::date budget::local_day(){ - auto tt = time( NULL ); - auto timeval = localtime( &tt ); + auto tt = time(nullptr); + auto* timeval = localtime(&tt); return { static_cast(timeval->tm_year + 1900), @@ -33,9 +31,9 @@ budget::date budget::date_from_string(std::string_view str){ throw date_exception("Invalid size for date_from_string"); } - date_type y; - date_type m; - date_type d; + date_type y = 0; + date_type m = 0; + date_type d = 0; if (auto [p, ec] = std::from_chars(str.data(), str.data() + 4, y); ec != std::errc() || p != str.data() + 4) { throw date_exception("Invalid year in date_from_string"); @@ -52,7 +50,7 @@ budget::date budget::date_from_string(std::string_view str){ return {y, m, d}; } -std::string budget::date_to_string(budget::date date){ +std::string budget::date_to_string(const budget::date& date) { std::string str(10, '0'); str[4] = '-'; @@ -64,13 +62,13 @@ std::string budget::date_to_string(budget::date date){ } // Convert the month - auto month_ptr = date.month() < 10 ? str.data() + 6 : str.data() + 5; + auto* month_ptr = date.month() < 10 ? str.data() + 6 : str.data() + 5; if (auto [p, ec] = std::to_chars(month_ptr, str.data() + 7, static_cast(date.month())); ec != std::errc() || p != str.data() + 7) { throw date_exception("Can't convert month to string"); } // Convert the month - auto day_ptr = date.day() < 10 ? str.data() + 9 : str.data() + 8; + auto* day_ptr = date.day() < 10 ? str.data() + 9 : str.data() + 8; if (auto [p, ec] = std::to_chars(day_ptr, str.data() + 10, static_cast(date.day())); ec != std::errc() || p != str.data() + 10) { throw date_exception("Can't convert day to string"); } @@ -79,7 +77,7 @@ std::string budget::date_to_string(budget::date date){ } unsigned short budget::start_month(data_cache & cache, budget::year year){ - budget::month m = min_with_default(cache.expenses() | to_date | filter_by_year(year) | to_month, 12); + budget::month const m = min_with_default(cache.expenses() | to_date | filter_by_year(year) | to_month, 12); return min_with_default(cache.earnings() | to_date | filter_by_year(year) | to_month, m); } diff --git a/src/debts.cpp b/src/debts.cpp index 44ea7b1e..598ee896 100644 --- a/src/debts.cpp +++ b/src/debts.cpp @@ -23,7 +23,7 @@ using namespace budget; namespace { -static data_handler debts { "debts", "debts.data" }; +data_handler debts{"debts", "debts.data"}; void edit_direction(bool& ref, const std::string& title){ std::string answer; @@ -32,10 +32,10 @@ void edit_direction(bool& ref, const std::string& title){ std::getline(std::cin, answer); if(!answer.empty()){ - auto direction = answer; + const auto& direction = answer; if(direction != "to" && direction != "from"){ - throw budget_exception("Invalid direction, only \"to\" and \"from\" are valid"); + throw budget_exception(R"(Invalid direction, only "to" and "from" are valid)"); } ref = direction == "to" ? true : false; @@ -80,7 +80,7 @@ void budget::debt_module::handle(const std::vector& args){ if(args.size() == 1){ list_debts(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "list"){ list_debts(w); @@ -99,7 +99,7 @@ void budget::debt_module::handle(const std::vector& args){ } else if(subcommand == "paid"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto debt = debts[id]; debt.state = 1; @@ -110,7 +110,7 @@ void budget::debt_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if (debts.remove(id)) { std::cout << "Debt " << id << " has been deleted" << std::endl; @@ -120,7 +120,7 @@ void budget::debt_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto debt = debts[id]; edit(debt); @@ -142,7 +142,7 @@ void budget::save_debts(){ debts.save(); } -void budget::debt::save(data_writer & writer){ +void budget::debt::save(data_writer& writer) const { writer << id; writer << state; writer << guid; diff --git a/src/earnings.cpp b/src/earnings.cpp index 32324014..e95f204f 100644 --- a/src/earnings.cpp +++ b/src/earnings.cpp @@ -25,7 +25,7 @@ using namespace budget; namespace { -static data_handler earnings { "earnings", "earnings.data" }; +data_handler earnings{"earnings", "earnings.data"}; } //end of anonymous namespace @@ -57,7 +57,7 @@ void budget::earnings_module::handle(const std::vector& args){ if(args.size() == 1){ show_earnings(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "show"){ if(args.size() == 2){ @@ -94,7 +94,7 @@ void budget::earnings_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if (earnings.remove(id)) { std::cout << "earning " << id << " has been deleted" << std::endl; @@ -104,7 +104,7 @@ void budget::earnings_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto earning = earnings[id]; @@ -134,7 +134,7 @@ void budget::save_earnings(){ earnings.save(); } -void budget::earning::save(data_writer & writer){ +void budget::earning::save(data_writer& writer) const { writer << id; writer << guid; writer << account; diff --git a/src/expenses.cpp b/src/expenses.cpp index 6b79393f..931d3067 100644 --- a/src/expenses.cpp +++ b/src/expenses.cpp @@ -23,11 +23,9 @@ using namespace budget; -namespace ranges = std::ranges; - namespace { -static data_handler expenses { "expenses", "expenses.data" }; +data_handler expenses{"expenses", "expenses.data"}; void show_templates() { std::vector columns = {"ID", "Account", "Name", "Amount"}; @@ -80,7 +78,7 @@ void budget::expenses_module::handle(const std::vector& args){ if(args.size() == 1){ show_expenses(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "show"){ if(args.size() == 2){ @@ -169,7 +167,7 @@ void budget::expenses_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if (expenses.remove(id)) { std::cout << "Expense " << id << " has been deleted" << std::endl; @@ -179,7 +177,7 @@ void budget::expenses_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto expense = expenses[id]; @@ -222,7 +220,7 @@ bool budget::edit_expense(const expense& expense){ return expenses.indirect_edit(expense); } -void budget::expense::save(data_writer & writer){ +void budget::expense::save(data_writer& writer) const { writer << id; writer << guid; writer << account; diff --git a/src/fortune.cpp b/src/fortune.cpp index e8160cbb..6d53fa93 100644 --- a/src/fortune.cpp +++ b/src/fortune.cpp @@ -23,7 +23,7 @@ using namespace budget; namespace { -static data_handler fortunes { "fortunes", "fortunes.data" }; +data_handler fortunes{"fortunes", "fortunes.data"}; } //end of anonymous namespace @@ -60,8 +60,9 @@ void budget::status_fortunes(budget::writer& w, bool short_view){ return; } - std::vector short_columns = {"From", "To", "Amount", "Diff.", "Avg/Day", "Avg/Day Tot."}; - std::vector long_columns = {"From", "To", "Amount", "Diff.", "Time", "Avg/Day", "Diff. Tot.", "Avg/Day Tot.", "Edit"}; + const std::vector short_columns = {"From", "To", "Amount", "Diff.", "Avg/Day", "Avg/Day Tot."}; + const std::vector long_columns = {"From", "To", "Amount", "Diff.", "Time", + "Avg/Day", "Diff. Tot.", "Avg/Day Tot.", "Edit"}; auto columns = short_view ? short_columns : long_columns; std::vector> contents; @@ -72,14 +73,14 @@ void budget::status_fortunes(budget::writer& w, bool short_view){ [](const budget::fortune& a, const budget::fortune& b){ return a.check_date < b.check_date; }); budget::money previous; - budget::money first = sorted_values.front().amount; + budget::money const first = sorted_values.front().amount; budget::date previous_date; - budget::date first_date = sorted_values.front().check_date; + budget::date const first_date = sorted_values.front().check_date; for(std::size_t i = 0; i < sorted_values.size(); ++i){ auto& fortune = sorted_values[i]; - bool display; + bool display = false; if(!short_view){ display = true; } else { @@ -99,9 +100,9 @@ void budget::status_fortunes(budget::writer& w, bool short_view){ contents.push_back({"", to_string(fortune.check_date), to_string(fortune.amount), "", "", "", "", "", "::edit::fortunes::" + budget::to_string(fortune.id)}); } } else if (i == 1) { - money diff = fortune.amount - previous; - long d = fortune.check_date - previous_date; - money avg = diff / d; + const money diff = fortune.amount - previous; + const long d = fortune.check_date - previous_date; + const money avg = diff / d; if (short_view) { contents.push_back({to_string(previous_date), to_string(fortune.check_date), to_string(fortune.amount), @@ -111,13 +112,13 @@ void budget::status_fortunes(budget::writer& w, bool short_view){ format_money(diff), to_string(d), to_string(avg), "", "", "::edit::fortunes::" + budget::to_string(fortune.id)}); } } else { - money diff = fortune.amount - previous; - long d = fortune.check_date - previous_date; - money avg = diff / d; + const money diff = fortune.amount - previous; + const long d = fortune.check_date - previous_date; + const money avg = diff / d; - money tot_diff = fortune.amount - first; - long tot_d = fortune.check_date - first_date; - money tot_avg = tot_diff / tot_d; + const money tot_diff = fortune.amount - first; + const long tot_d = fortune.check_date - first_date; + const money tot_avg = tot_diff / tot_d; if (short_view) { contents.push_back({to_string(previous_date), to_string(fortune.check_date), to_string(fortune.amount), @@ -170,7 +171,7 @@ void budget::fortune_module::handle(const std::vector& args){ if(args.size() == 1){ status_fortunes(w, false); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "list"){ list_fortunes(w); @@ -194,7 +195,7 @@ void budget::fortune_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if(!fortunes.exists(id)){ throw budget_exception("There are no fortune with id " + args[2]); @@ -206,7 +207,7 @@ void budget::fortune_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto fortune = fortunes[id]; @@ -235,7 +236,7 @@ void budget::save_fortunes(){ fortunes.save(); } -void budget::fortune::save(data_writer & writer){ +void budget::fortune::save(data_writer& writer) const { writer << id; writer << guid; writer << check_date; diff --git a/src/incomes.cpp b/src/incomes.cpp index 0affe612..ef8b49d5 100644 --- a/src/incomes.cpp +++ b/src/incomes.cpp @@ -24,7 +24,7 @@ using namespace budget; namespace { -static data_handler incomes { "incomes", "incomes.data" }; +data_handler incomes{"incomes", "incomes.data"}; } //end of anonymous namespace @@ -54,7 +54,7 @@ void budget::incomes_module::handle(const std::vector& args){ if(args.size() == 1){ show_incomes(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if (subcommand == "show") { show_incomes(w); @@ -71,7 +71,7 @@ void budget::incomes_module::handle(const std::vector& args){ } void budget::show_incomes(budget::writer& w) { - if (!incomes.size()) { + if (incomes.empty()) { w << title_begin << "No income " << set_button("incomes") << title_end; return; } @@ -98,7 +98,7 @@ void budget::save_incomes(){ incomes.save(); } -void budget::income::save(data_writer& writer) { +void budget::income::save(data_writer& writer) const { writer << id; writer << guid; writer << amount; @@ -159,7 +159,7 @@ budget::money budget::get_base_income(data_cache & cache){ return get_base_income(cache, today); } -budget::money budget::get_base_income(data_cache & cache, budget::date d){ +budget::money budget::get_base_income(data_cache& cache, const budget::date& d) { // First, we try to get the base income from the incomes module for (auto & income : cache.incomes()) { @@ -180,10 +180,10 @@ budget::money budget::get_base_income(data_cache & cache, budget::date d){ } budget::income budget::new_income(budget::money amount, bool print){ - budget::date d = budget::local_day(); + budget::date const d = budget::local_day(); - budget::date since(d.year(), d.month(), 1); - budget::date until = budget::date(2099,12,31); + budget::date const since(d.year(), d.month(), 1); + budget::date const until = budget::date(2099, 12, 31); budget::income new_income; new_income.guid = generate_guid(); @@ -191,7 +191,7 @@ budget::income budget::new_income(budget::money amount, bool print){ new_income.until = until; new_income.amount = amount; - if (incomes.size()) { + if (!incomes.empty()) { auto incomes_copy = all_incomes(); // Try to edit the income from the same month diff --git a/src/liabilities.cpp b/src/liabilities.cpp index 230eaa2d..dfc4e073 100644 --- a/src/liabilities.cpp +++ b/src/liabilities.cpp @@ -29,7 +29,7 @@ using namespace budget; namespace { -static data_handler liabilities { "liabilities", "liabilities.data" }; +data_handler liabilities{"liabilities", "liabilities.data"}; std::vector get_liabilities_names(){ std::vector names; @@ -72,7 +72,7 @@ void budget::liabilities_module::handle(const std::vector& args){ if(args.size() == 1){ show_liabilities(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "show"){ show_liabilities(w); @@ -181,7 +181,7 @@ void budget::liabilities_module::handle(const std::vector& args){ if (args.size() == 2) { budget::show_asset_values(w, true); } else { - auto& subsubcommand = args[2]; + const auto& subsubcommand = args[2]; if (subsubcommand == "set") { asset_value asset_value; @@ -264,7 +264,7 @@ budget::liability budget::get_liability(size_t id){ return liabilities[id]; } -budget::liability budget::get_liability(std::string name){ +budget::liability budget::get_liability(const std::string& name) { if (auto range = liabilities.data() | filter_by_name(name); range){ return *std::ranges::begin(range); } @@ -351,7 +351,7 @@ void budget::set_liabilities_next_id(size_t next_id){ } void budget::show_liabilities(budget::writer& w){ - if (!liabilities.size()) { + if (liabilities.empty()) { w << "No liabilities" << end_of_line; return; } @@ -405,7 +405,7 @@ void budget::show_liabilities(budget::writer& w){ } bool budget::liability::is_fi() const { - for (auto& [class_id, alloc] : classes) { + for (const auto& [class_id, alloc] : classes) { if (get_asset_class(class_id).fi) { return true; } @@ -438,7 +438,7 @@ bool budget::no_liabilities() { return liabilities.empty(); } -budget::money budget::get_liability_value(budget::liability & liability, budget::date d, data_cache & cache) { +budget::money budget::get_liability_value(budget::liability& liability, const budget::date& d, data_cache& cache) { budget::money asset_value_amount; for (auto& asset_value : cache.sorted_group_asset_values(true)[liability.id]) { @@ -462,28 +462,27 @@ budget::money budget::get_liability_value_conv(budget::liability & liability, da return get_liability_value_conv(liability, budget::local_day(), cache); } -budget::money budget::get_liability_value_conv(budget::liability & liability, budget::date d, data_cache & cache) { +budget::money budget::get_liability_value_conv(budget::liability& liability, const budget::date& d, data_cache& cache) { auto amount = get_liability_value(liability, d, cache); if (amount) { return amount * exchange_rate(liability.currency, d); - } else { - return amount; } + return amount; } budget::money budget::get_liability_value_conv(budget::liability & liability, const std::string& currency, data_cache & cache) { return get_liability_value_conv(liability, budget::local_day(), currency, cache); } -budget::money budget::get_liability_value_conv(budget::liability & liability, budget::date d, const std::string& currency, data_cache & cache) { +budget::money budget::get_liability_value_conv(budget::liability& liability, const budget::date& d, + const std::string& currency, data_cache& cache) { auto amount = get_liability_value(liability, d, cache); if (amount) { return amount * exchange_rate(liability.currency, currency, d); - } else { - return amount; } + return amount; } void budget::migrate_liabilities_6_to_7(){ @@ -521,7 +520,7 @@ void budget::update_asset_class_allocation(budget::liability& liability, budget: } budget::money budget::get_asset_class_allocation(const budget::liability& liability, const budget::asset_class & clas) { - for (auto & [class_id, class_alloc] : liability.classes) { + for (const auto& [class_id, class_alloc] : liability.classes) { if (class_id == clas.id) { return class_alloc; } diff --git a/src/money.cpp b/src/money.cpp index 904fc8b0..f473c949 100644 --- a/src/money.cpp +++ b/src/money.cpp @@ -23,7 +23,8 @@ money budget::money_from_string(std::string_view money_string){ if (auto [p, ec] = std::from_chars(money_string.data(), money_string.data() + money_string.size(), dollars); ec == std::errc()) { if (p == money_string.data() + money_string.size()) { return {dollars, cents}; - } else if(*p == '.') { + } + if (*p == '.') { ++p; if (auto [p2, ec] = std::from_chars(p, money_string.data() + money_string.size(), cents); ec == std::errc()) { @@ -40,9 +41,9 @@ money budget::money_from_string(std::string_view money_string){ } std::string budget::money_to_string(const money& amount) { - std::array buffer; + std::array buffer{}; - auto p1 = buffer.begin(); + auto* p1 = buffer.begin(); if (amount.negative()){ *p1++ = '-'; @@ -57,12 +58,12 @@ std::string budget::money_to_string(const money& amount) { if (auto [p3, ec] = std::to_chars(p2, buffer.end(), amount.cents()); ec == std::errc()) { return {buffer.begin(), p3}; - } else { - throw budget::budget_exception("money cant' be converted to string"); } - } else { + throw budget::budget_exception("money cant' be converted to string"); } + + throw budget::budget_exception("money cant' be converted to string"); } std::ostream& budget::operator<<(std::ostream& stream, const money& amount){ @@ -76,14 +77,14 @@ budget::money budget::random_money(size_t min, size_t max){ std::uniform_int_distribution dollars_dist(min, max); std::uniform_int_distribution cents_dist(0, 99); - return budget::money(dollars_dist(engine), cents_dist(engine)); + return {dollars_dist(engine), cents_dist(engine)}; } std::string budget::random_name(size_t length){ static std::random_device rd; static std::mt19937_64 engine(rd()); - std::uniform_int_distribution letters_dist(0, 25); + std::uniform_int_distribution letters_dist(0, 25); std::string name; diff --git a/src/objectives.cpp b/src/objectives.cpp index af44b7ed..1209fe5a 100644 --- a/src/objectives.cpp +++ b/src/objectives.cpp @@ -27,7 +27,7 @@ using namespace budget; namespace { -static data_handler objectives { "objectives", "objectives.data" }; +data_handler objectives{"objectives", "objectives.data"}; void edit(budget::objective& objective){ edit_string(objective.name, "Name", not_empty_checker()); @@ -112,7 +112,7 @@ void budget::monthly_objective_status(budget::writer& w){ std::vector> contents; for (unsigned short i = sm; i <= current_month; ++i) { - budget::month month = i; + budget::month const month = i; // Compute the month status auto status = budget::compute_month_status(w.cache, current_year, month); @@ -222,7 +222,7 @@ void budget::objectives_module::handle(const std::vector& args){ console_writer w(std::cout); status_objectives(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "list"){ console_writer w(std::cout); @@ -242,7 +242,7 @@ void budget::objectives_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if (objectives.remove(id)) { std::cout << "Goal " << id << " has been deleted" << std::endl; @@ -252,7 +252,7 @@ void budget::objectives_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto objective = objectives[id]; @@ -275,7 +275,7 @@ void budget::save_objectives(){ objectives.save(); } -void budget::objective::save(data_writer & writer){ +void budget::objective::save(data_writer& writer) const { writer << id; writer << guid; writer << name; @@ -316,7 +316,7 @@ void budget::set_objectives_next_id(size_t next_id){ void budget::list_objectives(budget::writer& w){ w << title_begin << "Goals " << add_button("objectives") << title_end; - if (objectives.size() == 0) { + if (objectives.empty()) { w << "No goals" << end_of_line; } else { std::vector columns = {"ID", "Name", "Type", "Source", "Operator", "Amount", "Edit"}; @@ -333,7 +333,7 @@ void budget::list_objectives(budget::writer& w){ void budget::status_objectives(budget::writer& w){ w << title_begin << "Goals " << add_button("objectives") << title_end; - if(objectives.size() == 0){ + if (objectives.empty()) { w << "No goals" << end_of_line; } else { auto today = budget::local_day(); diff --git a/src/overview.cpp b/src/overview.cpp index 270c7b9c..b4ab90cd 100644 --- a/src/overview.cpp +++ b/src/overview.cpp @@ -32,15 +32,15 @@ bool invalid_accounts_all(){ auto today = budget::local_day(); - std::vector previous = all_accounts(cache, sy, start_month(cache, sy)); + const std::vector previous = all_accounts(cache, sy, start_month(cache, sy)); for(unsigned short j = sy; j <= today.year(); ++j){ - budget::year year = j; + const budget::year year = j; auto sm = start_month(cache, year); for(unsigned short i = sm; i < 13; ++i){ - budget::month month = i; + const budget::month month = i; auto current_accounts = all_accounts(cache, year, month); @@ -51,8 +51,8 @@ bool invalid_accounts_all(){ for(auto& c : current_accounts){ bool found = false; - for(auto& p : previous){ - if(p.name == c.name){ + for (const auto& p : previous) { + if (p.name == c.name) { found = true; break; } @@ -76,7 +76,7 @@ bool invalid_accounts(budget::year year){ std::vector previous = all_accounts(cache, year, sm);; for(unsigned short i = sm + 1; i < 13; ++i){ - budget::month month = i; + const budget::month month = i; auto current_accounts = all_accounts(cache, year, month); @@ -109,13 +109,13 @@ template void add_recap_line(std::vector>& contents, const std::string& title, const std::vector& values, J functor){ std::vector total_line; - total_line.push_back(""); + total_line.emplace_back(""); total_line.push_back(title); total_line.push_back(to_string(functor(values.front()))); for(size_t i = 1; i < values.size(); ++i){ - total_line.push_back(""); - total_line.push_back(""); + total_line.emplace_back(""); + total_line.emplace_back(""); total_line.push_back(to_string(functor(values[i]))); } @@ -243,7 +243,7 @@ void add_values_column(budget::month month, for (auto& expense : sorted_values) { if (expense.date.year() == year && expense.date.month() == month) { if (indexes.contains(get_account(expense.account).name)) { - size_t index = indexes[get_account(expense.account).name]; + const size_t index = indexes[get_account(expense.account).name]; size_t& row = current[index]; if (contents.size() <= row) { @@ -275,7 +275,7 @@ struct icompare_str { size_t operator()(const std::string& value) const { auto l_value = value; std::transform(l_value.begin(), l_value.end(), l_value.begin(), ::tolower); - std::hash hasher; + const std::hash hasher; return hasher(l_value); } }; @@ -383,7 +383,7 @@ void aggregate_overview(const Data & data, budget::writer& w, bool full, bool di template void aggregate_overview_month(const Data & data, budget::writer& w, bool full, bool disable_groups, const std::string& separator, budget::year year, Functor&& func){ - int months; + int months = 1; if (year == budget::local_day().year()) { months = budget::local_day().month(); } else { @@ -513,7 +513,7 @@ void aggregate_overview_fv(const Data & data, budget::writer& w, bool full, bool void add_month_columns(std::vector& columns, budget::month sm){ for(unsigned short i = sm; i < 13; ++i){ - budget::month m = i; + const budget::month m = i; columns.emplace_back(m.as_long_string()); } @@ -537,7 +537,7 @@ int get_current_months(budget::year year){ template inline void generate_total_line(std::vector>& contents, std::vector& totals, budget::year year, budget::month sm){ std::vector last_row; - last_row.push_back("Total"); + last_row.emplace_back("Total"); auto current_months = get_current_months(year); @@ -550,7 +550,7 @@ inline void generate_total_line(std::vector>& contents, total_total += total; - budget::month m = i; + const budget::month m = i; if(m < sm + current_months){ current_total += total; } @@ -580,12 +580,12 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit columns.push_back(title); add_month_columns(columns, sm); - columns.push_back("Total"); - columns.push_back("Mean"); + columns.emplace_back("Total"); + columns.emplace_back("Mean"); if(current){ - columns.push_back("C. Total"); - columns.push_back("C. Mean"); + columns.emplace_back("C. Total"); + columns.emplace_back("C. Mean"); } std::unordered_map row_mapping; @@ -604,7 +604,7 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit //Fill the table for(unsigned short j = sm; j < 13; ++j){ - budget::month m = j; + const budget::month m = j; for(auto& account : all_accounts(w.cache, year, m)){ budget::money month_total; @@ -655,11 +655,11 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit if(last){ contents.push_back({"Previous Year"}); - budget::year last_year = year - 1; + const budget::year last_year = year - 1; budget::money total; for(unsigned short j = sm; j < 13; ++j){ - budget::month m = j; + const budget::month m = j; budget::money month_total; @@ -854,14 +854,14 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu auto months = 12 - sm + 1; auto current_months = get_current_months(year); - columns.push_back("Local Balance"); + columns.emplace_back("Local Balance"); add_month_columns(columns, sm); - columns.push_back("Total"); - columns.push_back("Mean"); + columns.emplace_back("Total"); + columns.emplace_back("Mean"); if(current){ - columns.push_back("C. Total"); - columns.push_back("C. Mean"); + columns.emplace_back("C. Total"); + columns.emplace_back("C. Mean"); } std::vector totals(12, budget::money()); @@ -881,7 +881,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu //Fill the table for(unsigned short i = sm; i < 13; ++i){ - budget::month m = i; + const budget::month m = i; for(auto& account : all_accounts(w.cache, year, m)){ budget::money total_expenses; @@ -938,7 +938,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu budget::money total; for (unsigned short i = sm; i < 13; ++i) { - budget::month m = i; + const budget::month m = i; auto status = compute_month_status(w.cache, year - 1, m); @@ -963,7 +963,7 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu double current_total_savings_rate = 0.0; for (unsigned short i = sm; i < 13; ++i) { - budget::month m = i; + const budget::month m = i; auto status = compute_month_status(w.cache, year, m); @@ -983,11 +983,11 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu total_savings_rate += savings_rate; } - contents.back().push_back(""); + contents.back().emplace_back(""); contents.back().push_back(to_string_precision(total_savings_rate / 12, 2) + "%"); if (current) { - contents.back().push_back(""); + contents.back().emplace_back(""); contents.back().push_back(to_string_precision(current_total_savings_rate / current_months, 2) + "%"); } } @@ -1018,11 +1018,11 @@ void budget::display_local_balance(budget::writer& w, budget::year year, bool cu total_savings_rate += savings_rate; } - contents.back().push_back(""); + contents.back().emplace_back(""); contents.back().push_back(to_string_precision(total_savings_rate / 12, 2) + "%"); if (current) { - contents.back().push_back(""); + contents.back().emplace_back(""); contents.back().push_back(to_string_precision(current_total_savings_rate / current_months, 2) + "%"); } } @@ -1036,7 +1036,7 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed, auto sm = start_month(w.cache, year); - columns.push_back("Balance"); + columns.emplace_back("Balance"); add_month_columns(columns, sm); std::vector totals(12, budget::money()); @@ -1065,7 +1065,7 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed, //Fill the table for(unsigned short i = sm; i <= 12; ++i){ - budget::month m = i; + const budget::month m = i; for(auto& account : all_accounts(w.cache, year, m)){ budget::money total_expenses; @@ -1099,7 +1099,7 @@ void budget::display_balance(budget::writer& w, budget::year year, bool relaxed, budget::money total; for(unsigned short i = sm; i < 13; ++i){ - budget::month m = i; + const budget::month m = i; auto status = compute_month_status(w.cache, year - 1, m); @@ -1213,13 +1213,13 @@ void budget::display_month_overview(budget::month month, budget::year year, budg if (has_taxes_account()) { second_contents.emplace_back(std::vector{"Savings Rate After Tax", budget::to_string(savings_rate_after) + "%"}); - double tax_rate = 100 * (taxes / income); + const double tax_rate = 100 * (taxes / income); second_contents.emplace_back(std::vector{"Tax Rate", budget::to_string(tax_rate) + "%"}); } - budget::date month_start(year, month, 1); - budget::date month_end = month_start.end_of_month(); + const budget::date month_start(year, month, 1); + const budget::date month_end = month_start.end_of_month(); auto net_worth_end = get_net_worth(month_end, writer.cache); auto net_worth_month_start = get_net_worth(month_start, writer.cache); @@ -1266,8 +1266,8 @@ void budget::display_month_account_overview(size_t account_id, budget::month mon //Balances contents.emplace_back(columns.size() * 3, ""); - std::vector balances{total_budget + total_earnings[0] - total_expenses[0]}; - std::vector local_balances{account.amount + total_earnings[0] - total_expenses[0]}; + const std::vector balances{total_budget + total_earnings[0] - total_expenses[0]}; + const std::vector local_balances{account.amount + total_earnings[0] - total_expenses[0]}; add_recap_line(contents, "Balance", balances, [](const budget::money& m){ return format_money(m);}); add_recap_line(contents, "Local Balance", local_balances, [](const budget::money& m){ return format_money(m);}); diff --git a/src/versioning.cpp b/src/versioning.cpp index b650ad9e..75c13828 100644 --- a/src/versioning.cpp +++ b/src/versioning.cpp @@ -22,7 +22,7 @@ std::string exec_command(const std::string& command) { FILE* stream = popen(command.c_str(), "r"); - while (fgets(buffer, 1024, stream) != NULL) { + while (fgets(buffer, 1024, stream) != nullptr) { output << buffer; } @@ -43,7 +43,7 @@ void budget::versioning_module::handle(const std::vector& args){ if(args.size() == 1){ std::cout << "Missing subcommand" << std::endl; } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "save"){ std::cout << exec_command("git -C " + budget_folder().string() + " commit -a -m Update" ) << std::endl; diff --git a/src/wishes.cpp b/src/wishes.cpp index ed71d40f..c8f10cfc 100644 --- a/src/wishes.cpp +++ b/src/wishes.cpp @@ -26,14 +26,13 @@ #include "console.hpp" #include "budget_exception.hpp" #include "compute.hpp" -#include "console.hpp" #include "writer.hpp" using namespace budget; namespace { -static data_handler wishes { "wishes", "wishes.data" }; +data_handler wishes { "wishes", "wishes.data" }; std::string wish_status(size_t v){ switch(v){ @@ -79,9 +78,9 @@ void edit(budget::wish& wish){ budget::money cash_for_wishes(){ if(budget::net_worth_over_fortune()){ return get_net_worth_cash(); - } else { - return current_fortune(); } + + return current_fortune(); } } //end of anonymous namespace @@ -125,7 +124,7 @@ void budget::wishes_module::handle(const std::vector& args){ if(args.size() == 1){ status_wishes(w); } else { - auto& subcommand = args[1]; + const auto& subcommand = args[1]; if(subcommand == "list"){ list_wishes(w); @@ -147,7 +146,7 @@ void budget::wishes_module::handle(const std::vector& args){ } else if(subcommand == "delete"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); if (wishes.remove(id)) { std::cout << "wish " << id << " has been deleted" << std::endl; @@ -157,7 +156,7 @@ void budget::wishes_module::handle(const std::vector& args){ } else if(subcommand == "edit"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto wish = wishes[id]; @@ -169,7 +168,7 @@ void budget::wishes_module::handle(const std::vector& args){ } else if(subcommand == "paid"){ enough_args(args, 3); - size_t id = to_number(args[2]); + const auto id = to_number(args[2]); auto wish = wishes[id]; @@ -194,7 +193,7 @@ void budget::save_wishes(){ wishes.save(); } -void budget::wish::save(data_writer & writer){ +void budget::wish::save(data_writer & writer) const { writer << id; writer << guid; writer << name; @@ -238,7 +237,7 @@ void budget::set_wishes_next_id(size_t next_id){ void budget::list_wishes(budget::writer& w){ w << title_begin << "Wishes " << add_button("wishes") << title_end; - if (wishes.size() == 0) { + if (wishes.empty()) { w << "No wishes" << end_of_line; } else { std::vector columns = {"ID", "Name", "Importance", "Urgency", "Amount", "Paid", "Diff", "Accuracy", "Edit"};