Add Config::write().
This commit is contained in:
parent
9f08830e32
commit
3f80752239
|
@ -72,12 +72,19 @@ public:
|
|||
|
||||
public:
|
||||
/*!
|
||||
* @brief Read and parse the configuration file.
|
||||
* @brief Read and parse the configuration from file.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
void read();
|
||||
|
||||
/*!
|
||||
* @brief Write configuration to file.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
void write() const;
|
||||
|
||||
/*!
|
||||
* @brief Set a configuration setting.
|
||||
*
|
||||
|
|
|
@ -46,6 +46,7 @@ public:
|
|||
* * `std::runtime_error` (Directory not found)
|
||||
* * `std::filesystem::filesystem_error` (Could not create directory)
|
||||
* * `std::ifstream::failure` (Could not read from file)
|
||||
* * `std::ofstream::failure` (Could not write to file)
|
||||
* * `nlohmann::detail::parse_error` (Could not parse file)
|
||||
*
|
||||
* @param application_name The name of your application. Influences the
|
||||
|
|
|
@ -18,12 +18,14 @@
|
|||
#include "directories.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace FediPotato
|
||||
{
|
||||
|
||||
using std::ifstream;
|
||||
using std::ofstream;
|
||||
using std::setw;
|
||||
|
||||
Config::Config(const string_view application_name, const string_view filename)
|
||||
: _filename{filename}
|
||||
|
@ -36,10 +38,7 @@ void Config::read()
|
|||
{
|
||||
if (!fs::exists(_filepath))
|
||||
{
|
||||
ofstream out{_filepath};
|
||||
out.exceptions(ofstream::failbit | ofstream::badbit);
|
||||
out << "{}";
|
||||
out.close();
|
||||
write();
|
||||
}
|
||||
|
||||
ifstream file(_filepath);
|
||||
|
@ -53,4 +52,16 @@ void Config::read()
|
|||
file >> _configfile;
|
||||
}
|
||||
|
||||
void Config::write() const
|
||||
{
|
||||
ofstream file(_filepath);
|
||||
if (file.fail() || file.bad())
|
||||
{
|
||||
throw std::ofstream::failure{
|
||||
string("Could not read ") += _filepath, std::io_errc::stream};
|
||||
}
|
||||
|
||||
file << setw(4) << _configfile << '\n';
|
||||
}
|
||||
|
||||
} // namespace FediPotato
|
||||
|
|
Reference in New Issue