This repository has been archived on 2020-05-16. You can view files and clone it, but cannot push or open issues or pull requests.
xdgcfg/src/xdgcfg.cpp

98 lines
1.7 KiB
C++
Raw Normal View History

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())
{
_filepath /= subdir;
}
if (!fs::exists(_filepath))
{
fs::create_directories(_filepath);
2018-08-10 02:22:06 +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;
}
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;
}