diff --git a/README.md b/README.md index 1c4b675..ed853fa 100644 --- a/README.md +++ b/README.md @@ -46,13 +46,7 @@ Gentoo ebuilds are available via my #### Get sourcecode Download the current -[release](https://schlomp.space/tastytea/whyblocked/releases) and copy -[xdgcfg](https://schlomp.space/tastytea/xdgcfg) into `xdgcfg/`. - -If you clone from git, be sure to `git submodule init` and -`git submodule update` afterwards. See the [submodules article in the git book] -(https://git-scm.com/book/en/v2/Git-Tools-Submodules#_cloning_submodules) for -further info. +[release](https://schlomp.space/tastytea/whyblocked/releases). #### Compile diff --git a/src/xdgcfg.cpp b/src/xdgcfg.cpp deleted file mode 120000 index 0f27a45..0000000 --- a/src/xdgcfg.cpp +++ /dev/null @@ -1 +0,0 @@ -../xdgcfg/src/xdgcfg.cpp \ No newline at end of file diff --git a/src/xdgcfg.cpp b/src/xdgcfg.cpp new file mode 100644 index 0000000..066012f --- /dev/null +++ b/src/xdgcfg.cpp @@ -0,0 +1,95 @@ +/* Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the BSD-3-Clause license. + */ + +#include +#include "xdgcfg.hpp" + +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); + } + _filepath /= filename; +} + +uint8_t xdgcfg::read() +{ + 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; +} + +bool xdgcfg::write() +{ + 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 +{ + return _filepath; +} + +void xdgcfg::set_verbose(bool verbose) +{ + _verbose = verbose; +} + +bool xdgcfg::get_verbose() const +{ + return _verbose; +} diff --git a/src/xdgcfg.hpp b/src/xdgcfg.hpp deleted file mode 120000 index f1851a8..0000000 --- a/src/xdgcfg.hpp +++ /dev/null @@ -1 +0,0 @@ -../xdgcfg/src/xdgcfg.hpp \ No newline at end of file diff --git a/src/xdgcfg.hpp b/src/xdgcfg.hpp new file mode 100644 index 0000000..cbec7a8 --- /dev/null +++ b/src/xdgcfg.hpp @@ -0,0 +1,106 @@ +/* Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the BSD-3-Clause license. + */ + +#ifndef XDGCFG_HPP +#define XDGCFG_HPP + +#if __cplusplus >= 201703L + #include +#else + #include +#endif +#include +#include +#include +#include + +#if __cplusplus >= 201703L + namespace fs = std::filesystem; +#else + namespace fs = std::experimental::filesystem; +#endif +using std::string; +using std::uint8_t; +using std::cerr; +using std::endl; + +class xdgcfg +{ +public: + /*! + * @brief Checks if subdir is present, creates it if necessary + * + * Example: + * @code + * xdgcfg config("test.cfg", "subdirectory"); + * @endcode + * + * @param filename The name of the file, including extension + * @param subdir The subdir (optional) + */ + explicit xdgcfg(const string &filename, const string &subdir = ""); + + /*! + * @brief Read the file + * + * @return 0 on success, 1 on I/O error, 2 on parse error. + */ + uint8_t read(); + + /*! + * @brief Write the file + * + * @return `true` on success + */ + bool write(); + + /*! + * @brief Returns a reference to the config as libconfig::Config + * + * Example: + * @code + * libconfig::Config &cfg = config.get_cfg(); + * @endcode + */ + libconfig::Config &get_cfg(); + + /*! + * @brief Returns the complete filepath + */ + const fs::path get_filepath() const; + + /*! + * @brief Sets verbosity + */ + void set_verbose(bool verbose); + + /*! + * @brief Returns verbosity + */ + bool get_verbose() const; + +private: + /*! + * Holds the contents of the CFG file + */ + libconfig::Config _cfg; + + /*! + * Complete filepath + */ + fs::path _filepath; + + /*! + * Print out error messages if true + */ + bool _verbose; +}; + +/*! + * @example example.cpp + */ + +#endif // XDGCFG_HPP diff --git a/xdgcfg b/xdgcfg deleted file mode 160000 index e22f82f..0000000 --- a/xdgcfg +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528