Compare commits

...
This repository has been archived on 2020-05-16. You can view files and clone it, but cannot push or open issues or pull requests.

28 Commits
doc ... master

Author SHA1 Message Date
tastytea 9337964266
Install headers more elegantly.
continuous-integration/drone/push Build is passing Details
2019-08-03 22:18:16 +02:00
tastytea 49b2117f13
License change to BSD-3-Clause.
continuous-integration/drone/push Build is passing Details
2019-08-03 15:01:15 +02:00
tastytea 3da87609f4
Set BUILD_SHARED_LIBS via option().
continuous-integration/drone/push Build is passing Details
2019-08-03 13:12:09 +02:00
tastytea 32e0a46a7f
Require CMake 3.6. 2019-08-03 13:11:56 +02:00
tastytea 88751694a5
Changed uint_fast8_t to uint8_t. The *fast* types are shit.
continuous-integration/drone/push Build is passing Details
2019-08-02 13:36:59 +02:00
tastytea 9a5bad2335
Fixed paths in Doxyfile. 2019-08-02 13:33:27 +02:00
tastytea 26ae664334
Replaced ${PROJECT_NAME} with @PROJECT_NAME@.
continuous-integration/drone/push Build is passing Details
2019-08-02 13:07:41 +02:00
tastytea 83a01f2941
Fixed example location in readme.
continuous-integration/drone/push Build is passing Details
2019-08-02 10:12:56 +02:00
tastytea 146dca1359
No genrator expressions in set() allowed, apparently.
continuous-integration/drone/push Build is passing Details
2019-08-02 09:20:22 +02:00
tastytea 2947c31263
set_property didn't work like I thought. 🤷
continuous-integration/drone/push Build is failing Details
2019-08-02 09:10:57 +02:00
tastytea 20d9c64161
Use CMAKE_SHARED_LINKER_FLAGS with cmake < 3.13.
continuous-integration/drone/push Build is failing Details
2019-08-02 09:05:10 +02:00
tastytea 8adbb9478e
Updated Doxygen generation. 2019-08-02 08:54:43 +02:00
tastytea 2fb329a302
Rearranged the file structure and rewrote the CMake recipes.
continuous-integration/drone/push Build is failing Details
* Compile a library again.
* Made CMake recipes modular.
* Added xdgcfgConfig.cmake.
* Added pkg-config file.
* Updated readme.
2019-08-02 08:44:07 +02:00
tastytea 91bbae667c
Added gcc-9 to CI. 2019-08-02 08:38:27 +02:00
tastytea bb08020611
Added install instruction for Gentoo.
continuous-integration/drone/push Build is passing Details
2019-07-22 03:10:29 +02:00
tastytea 1cca2b7406
CI: Added tests and examples, fixed omission.
continuous-integration/drone/push Build is passing Details
2019-07-22 02:50:59 +02:00
tastytea 4b3ececa13
Added CMake options to the readme. 2019-07-22 02:44:50 +02:00
tastytea e07a6ab6cb
Added tests and made compiling the example optional. 2019-07-22 02:37:03 +02:00
tastytea 3a7a7b428b
Updated CI recipe. 2019-07-21 22:48:25 +02:00
tastytea 7c111cd3ba
Put everything in the header. 2019-07-21 22:48:15 +02:00
tastytea e22f82fc6f
Removed superfluous consts.
the build was successful Details
2018-12-29 04:10:17 +01:00
tastytea 6c0976baa7
Updated drone recipe
the build was successful Details
2018-11-02 23:45:14 +01:00
tastytea eb47e38444
Fixed bug: Parent directories were only created if subdir was non-empty 2018-11-02 23:01:11 +01:00
tastytea 4785f8ce64
Made sure all parent directories are created, if necesssary
the build was successful Details
2018-08-31 19:22:00 +02:00
tastytea d3d4786e21
Added drone file
the build was successful Details
2018-08-11 08:37:21 +02:00
tastytea bae3f09ed2
Changed get_filepath() from string to filesystem::path 2018-08-11 07:10:53 +02:00
tastytea f4ab842787
typo 2018-08-10 03:02:57 +02:00
tastytea b04cb01382
Added documentation generation 2018-08-10 02:55:09 +02:00
20 changed files with 487 additions and 109 deletions

80
.drone.yml Normal file
View File

@ -0,0 +1,80 @@
# -*- fill-column: 1000 -*-
kind: pipeline
name: build x86_64
volumes:
- name: debian-package-cache
host:
path: /var/cache/debian-package-cache
trigger:
event:
exclude:
- tag
steps:
- name: gcc5
image: debian:stretch-slim
pull: always
environment:
CXX: g++-5
CXXFLAGS: -pipe -O2
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy -t xenial g++-5
- apt-get install -qy cmake pkg-config libconfig++-dev libxdg-basedir-dev catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_TESTS=YES -DWITH_EXAMPLES=YES ..
- make VERBOSE=1
- make install DESTDIR=install
- cd tests && ctest -Q
volumes:
- name: debian-package-cache
path: /var/cache/apt/archives
- name: gcc9
image: debian:stretch-slim
pull: always
environment:
CXX: g++-9
CXXFLAGS: -pipe -O2
LANG: en_US.utf-8
commands:
- rm /etc/apt/apt.conf.d/docker-clean
- alias apt-get='rm -f /var/cache/apt/archives/lock && apt-get'
- apt-get update -q
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
- apt-get install -qy gnupg
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
- apt-get update -q
- apt-get install -qy -t xenial g++-9
- apt-get install -qy cmake pkg-config libconfig++-dev libxdg-basedir-dev catch
- rm -rf build && mkdir -p build && cd build
- cmake -DWITH_TESTS=YES -DWITH_EXAMPLES=YES ..
- make VERBOSE=1
- make install DESTDIR=install
- cd tests && ctest -Q
- name: notify
image: drillster/drone-email
pull: always
settings:
host: cryptoparty-celle.de
from: drone@tzend.de
username:
from_secret: email_username
password:
from_secret: email_password
when:
status: [ changed, failure ]

2
.gitignore vendored
View File

@ -1 +1,3 @@
/build/
/doc/
/update_doc.sh

View File

@ -1,41 +1,57 @@
cmake_minimum_required (VERSION 3.7)
project(xdgcfg
VERSION 0.1.0
LANGUAGES CXX
)
# Support version 3.6 and above, but use policy settings up to 3.14.
# 3.6 is needed because of IMPORTED_TARGET in pkg_check_modules().
cmake_minimum_required(VERSION 3.6...3.14)
# Ranges are supported from 3.12, set policy to current for < 3.12.
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
include(GNUInstallDirs)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
pkg_check_modules(LIBCONFIG REQUIRED libconfig++)
project(xdgcfg
VERSION 0.5.0
LANGUAGES CXX
)
# DESCRIPTION was introduced in version 3.9.
if(NOT (${CMAKE_VERSION} VERSION_LESS 3.9))
set(PROJECT_DESCRIPTION
"Wrapper around libconfig that writes and reads files in XDG_CONFIG_HOME.")
endif()
# All custom build switches.
option(WITH_EXAMPLES "Compile examples." NO)
option(WITH_TESTS "Compile tests." NO)
option(BUILD_SHARED_LIBS "Build shared libraries." YES)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(DEBUG_CXXFLAGS
"-Wall"
"-Wextra"
"-Wpedantic"
"-ftrapv"
"-fsanitize=undefined"
"-g"
"-Og"
"-fno-omit-frame-pointer")
set(DEBUG_LDFLAGS
"-fsanitize=undefined")
add_compile_options("$<$<CONFIG:Debug>:${DEBUG_CXXFLAGS}>")
# add_link_options was introduced in version 3.13.
if(${CMAKE_VERSION} VERSION_LESS 3.13)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${DEBUG_LDFLAGS}")
else()
add_link_options("$<$<CONFIG:Debug>:${DEBUG_LDFLAGS}>")
endif()
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
include_directories(${LIBCONFIG_INCLUDE_DIRS})
add_subdirectory(src)
add_subdirectory(pkg-config)
add_subdirectory(cmake)
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
link_directories(${LIBCONFIG_LIBRARY_DIRS})
if(WITH_EXAMPLES)
add_subdirectory(examples)
endif()
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)
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})
if(WITH_TESTS)
add_subdirectory(tests)
endif()

28
Doxyfile Normal file
View File

@ -0,0 +1,28 @@
# -*- mode: conf-unix -*-
PROJECT_NAME = "xdgcfg"
PROJECT_NUMBER = 0.0.0
INPUT = README.md include/ src/
USE_MDFILE_AS_MAINPAGE = README.md
EXAMPLE_PATH = examples/
EXAMPLE_RECURSIVE = YES
GENERATE_HTML = YES
HTML_OUTPUT = doc/html
GENERATE_LATEX = NO
ALLOW_UNICODE_NAMES = YES
BRIEF_MEMBER_DESC = YES
REPEAT_BRIEF = YES
ALWAYS_DETAILED_SEC = YES
INLINE_INHERITED_MEMB = NO
INHERIT_DOCS = YES
SEPARATE_MEMBER_PAGES = NO
TAB_SIZE = 4
MARKDOWN_SUPPORT = YES
AUTOLINK_SUPPORT = YES
INLINE_SIMPLE_STRUCTS = NO
QUIET = NO
WARNINGS = YES
BUILTIN_STL_SUPPORT = YES
VERBATIM_HEADERS = YES
INLINE_SOURCES = YES
SEARCHENGINE = YES
SHOW_FILES = YES

26
LICENSE Normal file
View File

@ -0,0 +1,26 @@
Copyright (c) 2019 tastytea <tastytea@tastytea.de>. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,30 +1,62 @@
**xdgcfg** is a very simple wrapper around libconfig written in C++. It reads
and writes files into `${XDG_CONFIG_HOME}`. It creates subdirectories if
and writes files in `${XDG_CONFIG_HOME}`. It creates subdirectories if
necessary.
## Usage
`xdgcfg.hpp` has explanatory comments in it and there is an
[example](examples/example.cpp). The reference is also available at
[doc.schlomp.space/xdgcfg/](https://doc.schlomp.space/xdgcfg/classxdgcfg.html).
Use it in your CMake project like this:
``` cmake
find_package(xdgcfg CONFIG REQUIRED)
target_link_libraries(MyProject xdgcfg::xdgcfg)
```
If you don't use CMake, you can get the compile-flags with pkg-config:
``` shell
pkg-config --libs --cflags xdgcfg
```
## Install
### Gentoo
Add my [repository](https://schlomp.space/tastytea/overlay) and install it from
there.
``` shell
eselect repository enable tastytea
echo "dev-cpp/xdgcfg" >> /etc/portage/package.accept_keywords/xdgcfg
emaint sync -r tastytea
emerge -a dev-cpp/xdgcfg
```
### From source
### Dependencies
* C++ compiler
* [cmake](https://cmake.org/)
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/)
* [libconfig++](https://github.com/hyperrealm/libconfig)
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git)
* C++ compiler (Tested: g++ 5/8/9)
* [cmake](https://cmake.org/) (at least: 3.1)
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/) (tested: 0.29)
* [libconfig++](https://github.com/hyperrealm/libconfig) (tested: 1.5)
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git) (tested: 1.2)
### Usage
### Compile
You can create dynamic and static libraries:
```SH
``` shell
mkdir build
cd build
cmake ..
make
cmake --build .
make install
```
Or just copy `xdgcfg.hpp` and `xdgcfg.cpp` into your project folder.
### CMake options
### Documentation
`xdgcfg.hpp` has explanatory comments in it and there is an
[example](src/example.cpp).
* `-DWITH_TESTS=YES` to compile the tests.
* `-DWITH_EXAMPLES=YES` to compile the example.
* `-DBUILD_SHARED_LIBS=NO` to build a static library.

7
build_doc.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
if [[ -f Doxyfile ]]; then
mkdir -p doc
(doxygen -s -g - && cat Doxyfile && echo -n "PROJECT_NUMBER = " &&
grep -Eo '[0-9]+.[0-9]+.[0-9]+$' CMakeLists.txt) | doxygen -
fi

19
cmake/CMakeLists.txt Normal file
View File

@ -0,0 +1,19 @@
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PACKAGE_VERSION}
COMPATIBILITY ExactVersion) # NOTE: Set to SameMajorVersion when stable.
install(EXPORT ${PROJECT_NAME}Targets
FILE "${PROJECT_NAME}Targets.cmake"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
configure_file("${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

View File

@ -0,0 +1,6 @@
include(CMakeFindDependencyMacro)
find_dependency(PkgConfig REQUIRED)
pkg_check_modules(libconfig++ REQUIRED IMPORTED_TARGET libconfig++)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

9
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
include(GNUInstallDirs)
add_executable(example example.cpp)
target_link_libraries(example xdgcfg)
# # In your own project, you would link to xdgcfg like this:
# find_package(xdgcfg CONFIG REQUIRED)
# target_link_libraries(example xdgcfg::xdgcfg)

34
examples/example.cpp Normal file
View File

@ -0,0 +1,34 @@
/* This file is part of xdgcfg. */
#include <iostream>
#include <libconfig.h++>
#include "xdgcfg.hpp"
int main()
{
xdgcfg config("test.cfg", // File name.
"xdgcfg"); // Sub directory (optional).
config.set_verbose(true); // Print error messages.
if (config.read() != 0)
{
std::cout << "File not found.\n";
}
// Get a reference to the libconfig::Config object and use it as you would
// normally do.
libconfig::Config &cfg = config.get_cfg();
libconfig::Setting &root = cfg.getRoot();
if (!root.exists("Hello"))
{
root.add("Hello", libconfig::Setting::TypeString) = "World";
}
if (!config.write())
{
std::cerr << "Writing failed.\n";
}
std::cout << "Hello: " << root["Hello"].c_str() << std::endl;
}

View File

@ -1,16 +1,31 @@
/* Public Domain / CC-0
* Author: tastytea <tastytea@tastytea.de>
/* 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::uint_fast8_t;
using std::uint8_t;
using std::cerr;
using std::endl;
class xdgcfg
{
@ -33,14 +48,14 @@ public:
*
* @return 0 on success, 1 on I/O error, 2 on parse error.
*/
const uint_fast8_t read();
uint8_t read();
/*!
* @brief Write the file
*
* @return `true` on success
*/
const bool write();
bool write();
/*!
* @brief Returns a reference to the config as libconfig::Config
@ -55,17 +70,17 @@ public:
/*!
* @brief Returns the complete filepath
*/
const string get_filepath() const;
const fs::path get_filepath() const;
/*!
* @brief Sets verbosity
*/
const void set_verbose(bool verbose);
void set_verbose(bool verbose);
/*!
* @brief Returns verbosity
*/
const bool get_verbose() const;
bool get_verbose() const;
private:
/*!
@ -76,7 +91,7 @@ private:
/*!
* Complete filepath
*/
string _filepath;
fs::path _filepath;
/*!
* Print out error messages if true
@ -84,4 +99,8 @@ private:
bool _verbose;
};
/*!
* @example example.cpp
*/
#endif // XDGCFG_HPP

View File

@ -0,0 +1,7 @@
include(GNUInstallDirs)
configure_file("${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

13
pkg-config/xdgcfg.pc.in Normal file
View File

@ -0,0 +1,13 @@
name=@PROJECT_NAME@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
Name: ${name}
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@
Libs: -L${libdir} -l${name}
Cflags: -I${includedir}
Requires: libconfig++
Requires.private: libxdg-basedir

28
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,28 @@
include(GNUInstallDirs)
find_package(PkgConfig REQUIRED)
pkg_check_modules(libxdg-basedir REQUIRED IMPORTED_TARGET libxdg-basedir)
pkg_check_modules(libconfig++ REQUIRED IMPORTED_TARGET libconfig++)
set(${PROJECT_NAME}_public_headers "../include/xdgcfg.hpp")
add_library(${PROJECT_NAME} "xdgcfg.cpp" "${${PROJECT_NAME}_public_headers}")
set_target_properties(xdgcfg PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR}
PUBLIC_HEADER "${${PROJECT_NAME}_public_headers}")
target_include_directories(${PROJECT_NAME}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(${PROJECT_NAME}
PRIVATE PkgConfig::libxdg-basedir
PUBLIC PkgConfig::libconfig++ stdc++fs)
install(TARGETS ${PROJECT_NAME}
EXPORT "${PROJECT_NAME}Targets"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

View File

@ -1,22 +0,0 @@
#include <iostream>
#include <libconfig.h++>
#include "xdgcfg.hpp"
int main(int argc, char *argv[])
{
xdgcfg config("test.cfg", "xdgcfg");
config.set_verbose(true);
if (config.read() != 0)
{
config.write();
}
libconfig::Config &cfg = config.get_cfg();
libconfig::Setting &root = cfg.getRoot();
if (!root.exists("Hello"))
{
root.add("Hello", libconfig::Setting::TypeString) = "World";
}
config.write();
std::cout << "Hello: " << root["Hello"].c_str() << std::endl;
}

View File

@ -1,25 +1,12 @@
/* Public Domain / CC-0
* Author: tastytea <tastytea@tastytea.de>
/* 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.
*/
#if __cplusplus >= 201703L
#include <filesystem>
#else
#include <experimental/filesystem>
#endif
#include <iostream>
#include <basedir.h>
#include "xdgcfg.hpp"
#if __cplusplus >= 201703L
namespace fs = std::filesystem;
#else
namespace fs = std::experimental::filesystem;
#endif
using std::cerr;
using std::endl;
xdgcfg::xdgcfg(const string &filename, const string &subdir)
: _cfg()
, _verbose(false)
@ -31,16 +18,16 @@ xdgcfg::xdgcfg(const string &filename, const string &subdir)
if (!subdir.empty())
{
_filepath += '/' + subdir;
if (!fs::exists(_filepath))
{
fs::create_directory(_filepath);
}
_filepath /= subdir;
}
_filepath += '/' + filename;
if (!fs::exists(_filepath))
{
fs::create_directories(_filepath);
}
_filepath /= filename;
}
const uint_fast8_t xdgcfg::read()
uint8_t xdgcfg::read()
{
try
{
@ -68,7 +55,7 @@ const uint_fast8_t xdgcfg::read()
return 0;
}
const bool xdgcfg::write()
bool xdgcfg::write()
{
try
{
@ -92,17 +79,17 @@ libconfig::Config &xdgcfg::get_cfg()
return _cfg;
}
const string xdgcfg::get_filepath() const
const fs::path xdgcfg::get_filepath() const
{
return _filepath;
}
const void xdgcfg::set_verbose(bool verbose)
void xdgcfg::set_verbose(bool verbose)
{
_verbose = verbose;
}
const bool xdgcfg::get_verbose() const
bool xdgcfg::get_verbose() const
{
return _verbose;
}

26
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
include(CTest)
file(GLOB sources_tests test_*.cpp)
find_package(Catch2 CONFIG)
if(Catch2_FOUND) # Catch 2.x
include(Catch)
add_executable(all_tests main.cpp ${sources_tests})
target_link_libraries(all_tests Catch2::Catch2 xdgcfg)
target_include_directories(all_tests
PRIVATE "../include" "/usr/include/catch2")
catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}")
else() # Catch 1.x
if(EXISTS "/usr/include/catch.hpp")
message(STATUS "Catch 1.x found.")
foreach(src ${sources_tests})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} main.cpp ${src})
target_include_directories(${bin} PRIVATE "../include")
target_link_libraries(${bin} xdgcfg)
add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}")
endforeach()
else()
message(FATAL_ERROR
"Neither Catch 2.x nor Catch 1.x could be found.")
endif()
endif()

5
tests/main.cpp Normal file
View File

@ -0,0 +1,5 @@
/* This file is part of xdgcfg. */
#define CATCH_CONFIG_MAIN
#include <catch.hpp>

56
tests/test_write_read.cpp Normal file
View File

@ -0,0 +1,56 @@
/* This file is part of xdgcfg. */
#include <exception>
#include <string>
#include <catch.hpp>
#include "xdgcfg.hpp"
using std::string;
SCENARIO ("A config file can be written and then read.")
{
bool exception = false;
WHEN ("Writing config")
{
xdgcfg config("test.cfg", "xdgcfg");
libconfig::Config &cfg = config.get_cfg();
libconfig::Setting &root = cfg.getRoot();
try
{
root.add("Hello", libconfig::Setting::TypeString) = "World! 🙂";
config.write();
}
catch (const std::exception &e)
{
exception = true;
}
THEN ("No exception is thrown")
{
REQUIRE_FALSE(exception);
}
WHEN ("Reading config")
{
string value;
try
{
value = root["Hello"].c_str();
}
catch (const std::exception &e)
{
exception = true;
}
THEN ("No exception is thrown")
AND_THEN ("Value is correct")
{
REQUIRE_FALSE(exception);
REQUIRE(value == "World! 🙂");
}
}
}
}