Put everything in the header.

This commit is contained in:
tastytea 2019-07-21 22:37:02 +02:00
parent e22f82fc6f
commit 7c111cd3ba
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 93 additions and 126 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.1)
project(xdgcfg
VERSION 0.2.3
VERSION 0.3.0
LANGUAGES CXX
)
@ -13,8 +13,9 @@ set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -g -Og -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Wpedantic -ftrapv \
-fsanitize=undefined -g -Og -fno-omit-frame-pointer")
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
include_directories(${LIBCONFIG_INCLUDE_DIRS})
@ -22,21 +23,8 @@ include_directories(${LIBCONFIG_INCLUDE_DIRS})
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
link_directories(${LIBCONFIG_LIBRARY_DIRS})
add_library(xdgcfg SHARED src/xdgcfg.cpp)
set_target_properties(xdgcfg PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${xdgcfg_VERSION_MAJOR})
target_link_libraries(xdgcfg
${LIBXDG_BASEDIR_LIBRARIES} ${LIBCONFIG_LIBRARIES}
stdc++fs)
add_library(xdgcfg_static STATIC src/xdgcfg.cpp)
set_target_properties(xdgcfg_static PROPERTIES
OUTPUT_NAME xdgcfg)
add_executable(example src/example.cpp)
target_link_libraries(example xdgcfg)
target_link_libraries(example
${LIBXDG_BASEDIR_LIBRARIES} ${LIBCONFIG_LIBRARIES} stdc++fs)
install(TARGETS xdgcfg LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS xdgcfg_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES src/xdgcfg.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

View File

@ -4,7 +4,7 @@ necessary.
### Dependencies
* C++ compiler (Tested: g++ 5/6/7/8)
* C++ compiler (Tested: g++ 5/8)
* [cmake](https://cmake.org/)
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/)
* [libconfig++](https://github.com/hyperrealm/libconfig)
@ -12,9 +12,9 @@ necessary.
### Usage
You can create dynamic and static libraries:
Copy `xdgcfg.hpp` into a folder where your project can find it.
```SH
``` shell
mkdir build
cd build
cmake ..
@ -22,7 +22,6 @@ make
make install
```
Or just copy `xdgcfg.hpp` and `xdgcfg.cpp` into your project folder.
### Documentation

View File

@ -1,97 +0,0 @@
/* 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);
}
_filepath /= filename;
}
uint_fast8_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;
}

View File

@ -11,8 +11,10 @@
#include <experimental/filesystem>
#endif
#include <string>
#include <iostream>
#include <cstdint>
#include <libconfig.h++>
#include <basedir.h>
#if __cplusplus >= 201703L
namespace fs = std::filesystem;
@ -21,6 +23,8 @@
#endif
using std::string;
using std::uint_fast8_t;
using std::cerr;
using std::endl;
class xdgcfg
{
@ -36,21 +40,82 @@ public:
* @param filename The name of the file, including extension
* @param subdir The subdir (optional)
*/
explicit xdgcfg(const string &filename, const string &subdir = "");
explicit 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;
}
/*!
* @brief Read the file
*
* @return 0 on success, 1 on I/O error, 2 on parse error.
*/
uint_fast8_t read();
uint_fast8_t 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;
}
/*!
* @brief Write the file
*
* @return `true` on success
*/
bool write();
bool 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;
}
/*!
* @brief Returns a reference to the config as libconfig::Config
@ -60,22 +125,34 @@ public:
* libconfig::Config &cfg = config.get_cfg();
* @endcode
*/
libconfig::Config &get_cfg();
libconfig::Config &get_cfg()
{
return _cfg;
}
/*!
* @brief Returns the complete filepath
*/
const fs::path get_filepath() const;
const fs::path get_filepath() const
{
return _filepath;
}
/*!
* @brief Sets verbosity
*/
void set_verbose(bool verbose);
void set_verbose(bool verbose)
{
_verbose = verbose;
}
/*!
* @brief Returns verbosity
*/
bool get_verbose() const;
bool get_verbose() const
{
return _verbose;
}
private:
/*!