/* Public Domain / CC-0 * Author: tastytea */ #include #include #include #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; } const 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; } } const 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; }