Get db path from settings file instead of using a hardcoded value

This commit is contained in:
King_DuckZ 2020-08-10 13:17:40 +01:00
parent c05213d576
commit 35118dbe4e
9 changed files with 106 additions and 11 deletions

View file

@ -1,2 +1 @@
option('base_url', type: 'string', value: 'https://api.originsro.org')
option('database_file', type: 'string', value: 'originsro.db3')

2
orotool.conf Normal file
View file

@ -0,0 +1,2 @@
[main]
db_path=originsro.db3

52
src/app_config.cpp Normal file
View file

@ -0,0 +1,52 @@
/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "app_config.hpp"
#include "orotool_config.hpp"
#include <fstream>
#include <string>
namespace duck {
namespace {
std::string whole_ini() {
std::ifstream input(g_config_file_path);
input >> std::noskipws;
return { std::istream_iterator<char>(input), std::istream_iterator<char>() };
}
} //unnamed namespace
AppConfig::AppConfig() :
m_ini(whole_ini())
{
}
AppConfig::~AppConfig() noexcept = default;
std::string_view AppConfig::db_path() const {
const auto& map = m_ini.parsed();
auto it_section = map.find("main");
if (map.end() == it_section)
return "";
auto it_setting = it_section->second.find("db_path");
if (it_section->second.end() == it_setting)
return "";
return it_setting->second;
}
} //namespace duck

36
src/app_config.hpp Normal file
View file

@ -0,0 +1,36 @@
/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "ini_file.hpp"
#include <string_view>
namespace duck {
class AppConfig {
public:
AppConfig();
~AppConfig() noexcept;
std::string_view db_path() const;
private:
kamokan::IniFile m_ini;
};
} //namespace duck

View file

@ -20,7 +20,6 @@
namespace duck {
constexpr const char g_base_url[] = @BASE_URL@;
constexpr const char g_database[] = @DATABASE@;
constexpr const char g_config_file_path[] = @CONFIG_FILE_PATH@;
} //namespace duck

View file

@ -19,6 +19,7 @@
#include "oro/originsdb.hpp"
#include "orotool_config.hpp"
#include "evloop.hpp"
#include "app_config.hpp"
#include <iostream>
int main(int argc, char* argv[]) {
@ -41,7 +42,8 @@ int main(int argc, char* argv[]) {
std::cout << "answer: " << ping.second.message << '\n';
std::cout << "version: " << ping.second.version << '\n';
oro::OriginsDB db;
duck::AppConfig app_conf;
oro::OriginsDB db(app_conf.db_path());
duck::test(&oro_api, &db);
/*

View file

@ -21,7 +21,6 @@ endif
conf = configuration_data()
conf.set_quoted('BASE_URL', base_url)
conf.set_quoted('DATABASE', get_option('database_file'))
conf.set_quoted('CONFIG_FILE_PATH', get_option('prefix') / get_option('sysconfdir') / meson.project_name() + '.conf')
project_config_file = configure_file(
input: 'config.hpp.in',
@ -58,6 +57,7 @@ executable(meson.project_name(),
'oro/originsdb.cpp',
'gnulib/lib/base64.c',
'base64.cpp',
'app_config.cpp',
project_config_file,
install: true,
dependencies: lib_deps,

View file

@ -19,7 +19,6 @@
#include "SQLiteCpp/Database.h"
#include "SQLiteCpp/Statement.h"
#include "SQLiteCpp/Transaction.h"
#include "orotool_config.hpp"
#include "SQLiteCpp/SQLiteCpp.h"
#include "oro/items.hpp"
#include "oro/icons.hpp"
@ -30,20 +29,23 @@ namespace oro {
namespace {
class Database : private std::unique_lock<std::mutex>, public SQLite::Database {
public:
explicit Database(std::mutex& mtx) :
Database(std::mutex& mtx, const std::string& path) :
std::unique_lock<std::mutex>(mtx),
SQLite::Database(duck::g_database, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)
SQLite::Database(path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)
{
}
};
} //unnamed namespace
OriginsDB::OriginsDB() = default;
OriginsDB::OriginsDB (std::string_view path) :
m_path(path.data(), path.size())
{
}
OriginsDB::~OriginsDB() noexcept = default;
void OriginsDB::overwrite (const Items& items) {
Database db(m_db_mutex);
Database db(m_db_mutex, m_path);
db.exec("DROP TABLE IF EXISTS items");
@ -85,7 +87,7 @@ void OriginsDB::update (const Icons& icons) {
// "icon":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAP1BMVEX/AP/XqpHDkHqudmL////ox6X/9LxJSUm3t7eSkpLb29tubm7Kt6//8+u+QzDTjG7tZEfyhVmPIhi4o5v/8OVQ35yOAAAAAXRSTlMAQObYZgAAAAFiS0dEBI9o2VEAAAAHdElNRQfkAg4MBBc1u27AAAAAmElEQVQoz7WP2w6DIBAFsSyedavLRf//W7uQVmk0afvQeSIzOQSc+wfDcPOXnigE/1MYxxBw0szTYAMwv2kikunuPcBEc+8XknkCVNXOfHhADOYYVYG9mE92V2y08iksQBbRp+/CDM3CSRulKNb9E5o4p/QK6/Fe0WyhpVI6bxORZK7SD+pELn0t7ZqTd25raT35mrZL/TUPifEKODGf6AcAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjAtMDItMTRUMTI6MDQ6MjMrMDA6MDCsyg5gAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIwLTAyLTE0VDEyOjA0OjIzKzAwOjAw3Ze23AAAAABJRU5ErkJggg=="
//}
Database db(m_db_mutex);
Database db(m_db_mutex, m_path);
SQLite::Statement query(db, "UPDATE items SET icon = ? WHERE item_id = ?");
for (const auto& ico : icons.icons) {

View file

@ -18,6 +18,8 @@
#pragma once
#include <mutex>
#include <string_view>
#include <string>
namespace oro {
@ -28,13 +30,14 @@ struct Creators;
class OriginsDB {
public:
OriginsDB();
explicit OriginsDB(std::string_view path);
~OriginsDB() noexcept;
void overwrite (const Items& items);
void update (const Icons& icons);
private:
std::string m_path;
std::mutex m_db_mutex;
};