73 lines
1.2 KiB
C++
73 lines
1.2 KiB
C++
/* Public Domain / CC-0
|
|
* Author: tastytea <tastytea@tastytea.de>
|
|
*/
|
|
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <basedir.h>
|
|
#include "xdgjson.hpp"
|
|
|
|
xdgjson::xdgjson(const string &filename, const string &subdir)
|
|
: _json()
|
|
{
|
|
xdgHandle xdg;
|
|
xdgInitHandle(&xdg);
|
|
_filepath = xdgConfigHome(&xdg);
|
|
xdgWipeHandle(&xdg);
|
|
|
|
if (!subdir.empty())
|
|
{
|
|
_filepath /= subdir;
|
|
}
|
|
if (!fs::exists(_filepath))
|
|
{
|
|
fs::create_directories(_filepath);
|
|
}
|
|
_filepath /= filename;
|
|
}
|
|
|
|
bool xdgjson::read()
|
|
{
|
|
std::ifstream file(_filepath);
|
|
if (file.is_open())
|
|
{
|
|
std::stringstream config;
|
|
config << file.rdbuf();
|
|
file.close();
|
|
config >> _json;
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool xdgjson::write()
|
|
{
|
|
std::ofstream file(_filepath);
|
|
if (file.is_open())
|
|
{
|
|
const string config = _json.toStyledString();
|
|
file.write(config.c_str(), config.length());
|
|
file.close();
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Json::Value &xdgjson::get_json()
|
|
{
|
|
return _json;
|
|
}
|
|
|
|
const fs::path xdgjson::get_filepath() const
|
|
{
|
|
return _filepath;
|
|
}
|