2018-08-10 02:22:06 +02:00
|
|
|
/* Public Domain / CC-0
|
|
|
|
* Author: tastytea <tastytea@tastytea.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <basedir.h>
|
|
|
|
#include "xdgcfg.hpp"
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
xdgcfg::xdgcfg(const string &filename, const string &subdir)
|
|
|
|
: _cfg()
|
|
|
|
, _verbose(false)
|
|
|
|
{
|
|
|
|
xdgHandle xdg;
|
|
|
|
xdgInitHandle(&xdg);
|
|
|
|
_filepath = xdgConfigHome(&xdg);
|
|
|
|
xdgWipeHandle(&xdg);
|
|
|
|
|
|
|
|
if (!subdir.empty())
|
|
|
|
{
|
2018-08-11 07:10:53 +02:00
|
|
|
_filepath /= subdir;
|
2018-11-02 23:01:11 +01:00
|
|
|
}
|
|
|
|
if (!fs::exists(_filepath))
|
|
|
|
{
|
|
|
|
fs::create_directories(_filepath);
|
2018-08-10 02:22:06 +02:00
|
|
|
}
|
2018-08-11 07:10:53 +02:00
|
|
|
_filepath /= filename;
|
2018-08-10 02:22:06 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 04:10:17 +01:00
|
|
|
uint_fast8_t xdgcfg::read()
|
2018-08-10 02:22:06 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_cfg.readFile(_filepath.c_str());
|
|
|
|
}
|
|
|
|
catch (const libconfig::FileIOException &e)
|
|
|
|
{
|
|
|
|
if (_verbose)
|
|
|
|
{
|
|
|
|
cerr << "I/O error while reading " << _filepath
|
|
|
|
<< " - " << e.what() << endl;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
catch (const libconfig::ParseException &e)
|
|
|
|
{
|
|
|
|
if (_verbose)
|
|
|
|
{
|
|
|
|
cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
|
|
|
|
<< " - " << e.getError() << endl;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-29 04:10:17 +01:00
|
|
|
bool xdgcfg::write()
|
2018-08-10 02:22:06 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_cfg.writeFile(_filepath.c_str());
|
|
|
|
}
|
|
|
|
catch (const libconfig::FileIOException &e)
|
|
|
|
{
|
|
|
|
if (_verbose)
|
|
|
|
{
|
|
|
|
cerr << "I/O error while writing " << _filepath
|
|
|
|
<< " - " << e.what() << endl;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
libconfig::Config &xdgcfg::get_cfg()
|
|
|
|
{
|
|
|
|
return _cfg;
|
|
|
|
}
|
|
|
|
|
2018-08-11 07:10:53 +02:00
|
|
|
const fs::path xdgcfg::get_filepath() const
|
2018-08-10 02:22:06 +02:00
|
|
|
{
|
|
|
|
return _filepath;
|
|
|
|
}
|
|
|
|
|
2018-12-29 04:10:17 +01:00
|
|
|
void xdgcfg::set_verbose(bool verbose)
|
2018-08-10 02:22:06 +02:00
|
|
|
{
|
|
|
|
_verbose = verbose;
|
|
|
|
}
|
|
|
|
|
2018-12-29 04:10:17 +01:00
|
|
|
bool xdgcfg::get_verbose() const
|
2018-08-10 02:22:06 +02:00
|
|
|
{
|
|
|
|
return _verbose;
|
|
|
|
}
|