From 79bd6f7d01a89c58dfd457785b0c85a89d80155c Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 17 Oct 2018 17:28:29 +0200 Subject: [PATCH] Improved readability of the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … by using the namespace "database" for functions that interact with the database. --- src/interface_qt.cpp | 10 +++++----- src/interface_text.cpp | 10 +++++----- src/whyblocked.cpp | 13 +++++++------ src/whyblocked.hpp | 14 +++++++++----- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index fdf4c79..c58f537 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -42,7 +42,7 @@ void MainWindow::populate_tableview() tableview->horizontalHeader()->resizeSection(0, 250); result_view result; - if (view(result)) + if (database::view(result)) { for (const std::tuple &line : result) { @@ -85,7 +85,7 @@ void MainWindow::remove() for (auto &row : selection->selectedRows()) { const string user = row.data().toString().toStdString(); - ::remove(user); + database::remove(user); statusBar()->showMessage(tr("Removed %1 from database.") .arg(QString::fromStdString(user))); _model->removeRow(row.row()); @@ -119,7 +119,7 @@ void MainWindow::show_details(QModelIndex index) result_details result; string text = ""; - if (details(user, result)) + if (database::details(user, result)) { if (!std::get<2>(result).empty()) { @@ -188,13 +188,13 @@ void DialogAdd::accept() { return; } - ::add_block(user, blocked, reason); + database::add_block(user, blocked, reason); _parent->add_row(QString::fromStdString(user), blocked, QString::fromStdString(reason)); for (const string &receipt : std::get<3>(data)) { - ::add_url(user, receipt); + database::add_receipt(user, receipt); } _parent->statusBar()->showMessage(tr("Added %1 to database.") diff --git a/src/interface_text.cpp b/src/interface_text.cpp index 86c8907..6fe102a 100644 --- a/src/interface_text.cpp +++ b/src/interface_text.cpp @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) cin.ignore(); std::getline(cin, reason, '\n'); - if (add_block(user, blocked, reason)) + if (database::add_block(user, blocked, reason)) { cout << user << " added.\n"; } @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) cout << "URL: "; cin >> url; - if (add_url(user, url)) + if (database::add_receipt(user, url)) { cout << "Receipt added.\n"; } @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) cout << "User or instance: "; cin >> user; - if (remove(user)) + if (database::remove(user)) { cout << user << " removed.\n"; } @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) case 'V': { result_view result; - if (view(result)) + if (database::view(result)) { for (const std::tuple &line : result) { @@ -138,7 +138,7 @@ int main(int argc, char *argv[]) cin >> answer; { result_details result; - if (details(answer, result)) + if (database::details(answer, result)) { cout << answer << " is "; if (std::get<0>(result) == 1) diff --git a/src/whyblocked.cpp b/src/whyblocked.cpp index f2d489d..09b82bf 100644 --- a/src/whyblocked.cpp +++ b/src/whyblocked.cpp @@ -54,7 +54,8 @@ const string get_filepath() return filepath; } -const bool add_block(const string &user, const int blocked, const string &reason) +const bool database::add_block(const string &user, const int blocked, + const string &reason) { try { @@ -72,13 +73,13 @@ const bool add_block(const string &user, const int blocked, const string &reason return true; } -const bool add_url(const string &user, const string &url) +const bool database::add_receipt(const string &user, const string &receipt) { try { sqlite::connection con(get_filepath()); sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);"); - ins % user % url; + ins % user % receipt; ins(); } catch (const std::exception &e) @@ -90,7 +91,7 @@ const bool add_url(const string &user, const string &url) return true; } -const bool remove(const string &user) +const bool database::remove(const string &user) { try { @@ -111,7 +112,7 @@ const bool remove(const string &user) return true; } -const bool view(result_view &result) +const bool database::view(result_view &result) { try { @@ -137,7 +138,7 @@ const bool view(result_view &result) return true; } -const bool details(const string &user, result_details &result) +const bool database::details(const string &user, result_details &result) { try { diff --git a/src/whyblocked.hpp b/src/whyblocked.hpp index 2bdefd2..c0f4372 100644 --- a/src/whyblocked.hpp +++ b/src/whyblocked.hpp @@ -26,10 +26,14 @@ using result_view = std::vector>; using result_details = std::tuple>; const string get_filepath(); -const bool add_block(const string &user, const int blocked, const string &reason); -const bool add_url(const string &user, const string &url); -const bool remove(const string &user); -const bool view(result_view &result); -const bool details(const string &user, result_details &result); +namespace database +{ + const bool add_block(const string &user, const int blocked, + const string &reason); + const bool add_receipt(const string &user, const string &receipt); + const bool remove(const string &user); + const bool view(result_view &result); + const bool details(const string &user, result_details &result); +} #endif // WHYBLOCKED_HPP