parent
3a8b8c715a
commit
733aad9903
|
@ -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
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
../xdgcfg/src/xdgcfg.cpp
|
|
@ -0,0 +1,95 @@
|
|||
/* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the BSD-3-Clause license.
|
||||
*/
|
||||
|
||||
#include <basedir.h>
|
||||
#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;
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
../xdgcfg/src/xdgcfg.hpp
|
|
@ -0,0 +1,106 @@
|
|||
/* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||
*
|
||||
* 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 <filesystem>
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include <libconfig.h++>
|
||||
|
||||
#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
|
1
xdgcfg
1
xdgcfg
|
@ -1 +0,0 @@
|
|||
Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528
|
Loading…
Reference in New Issue