This repository has been archived on 2020-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
FediPotato/lib/include/config.hpp

148 lines
3.9 KiB
C++

/* This file is part of FediPotato.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FEDIPOTATO_CONFIG_HPP
#define FEDIPOTATO_CONFIG_HPP
#include "directories.hpp"
#include <nlohmann/json.hpp>
#if __has_include(<filesystem>)
# include <filesystem>
#else
# include <experimental/filesystem>
#endif
#include <string>
#include <string_view>
namespace FediPotato
{
using json = nlohmann::json;
namespace fs = std::filesystem;
using std::string;
using std::string_view;
/*!
* @brief Read and write the configuration.
*
* @throw std::runtime_error Directory not found.
* @throw std::filesystem::filesystem_error Could not create directory.
* @throw std::ifstream::failure Could not read from file.
* @throw std::ofstream::failure Could not write to file.
* @throw nlohmann::detail::parse_error Could not parse file.
* @throw nlohmann::detail::type_error Setting not found / Wrong type.
*
* @since 0.1.0
*
* @headerfile config.hpp FediPotato/config.hpp
*/
class Config
{
public:
/*!
* @brief Construct a Config object.
*
* @param application_name The name of your application.
* @param filename The filename to open.
*
* @throw std::runtime_error Directory not found.
* @throw std::filesystem::filesystem_error Could not create directory.
*
* @since 0.1.0
*/
explicit Config(const string_view application_name,
const string_view filename)
: _filename{filename}
, _filepath{Directories(application_name).config() /= filename}
{
read();
}
//! Copy constructor
Config(const Config &other) = default;
//! Move constructor
Config(Config &&other) noexcept = default;
//! Destructor
virtual ~Config() noexcept = default;
//! Copy assignment operator
Config& operator=(const Config &other) = delete;
//! Move assignment operator
Config& operator=(Config &&other) noexcept = delete;
public:
/*!
* @brief Read and parse the configuration from file.
*
* @throw std::ifstream::failure Could not read from file.
* @throw nlohmann::detail::parse_error Could not parse file.
*
* @since 0.1.0
*/
void read();
/*!
* @brief Write configuration to file.
*
* @since 0.1.0
*/
void write() const;
/*!
* @brief Set a configuration setting.
*
* @param key The key of the setting.
* @param value The value of the setting.
*
* @throw std::ofstream::failure Could not write to file.
*
* @since 0.1.0
*/
inline void set(const string_view key, const string_view value)
{
_configfile[key.data()] = value;
}
/*!
* @brief Get a configuration setting.
*
* @param key The key of the setting.
*
* @throw nlohmann::detail::type_error Setting not found / Wrong type.
*
* @since 0.1.0
*/
[[nodiscard]]
inline string get(const string_view key) const
{
return _configfile[key.data()].get<string>();
}
private:
const string _filename;
const fs::path _filepath;
json _configfile;
};
} // namespace FediPotato
#endif // FEDIPOTATO_CONFIG_HPP