Compare commits
28 Commits
Author | SHA1 | Date |
---|---|---|
tastytea | 9337964266 | |
tastytea | 49b2117f13 | |
tastytea | 3da87609f4 | |
tastytea | 32e0a46a7f | |
tastytea | 88751694a5 | |
tastytea | 9a5bad2335 | |
tastytea | 26ae664334 | |
tastytea | 83a01f2941 | |
tastytea | 146dca1359 | |
tastytea | 2947c31263 | |
tastytea | 20d9c64161 | |
tastytea | 8adbb9478e | |
tastytea | 2fb329a302 | |
tastytea | 91bbae667c | |
tastytea | bb08020611 | |
tastytea | 1cca2b7406 | |
tastytea | 4b3ececa13 | |
tastytea | e07a6ab6cb | |
tastytea | 3a7a7b428b | |
tastytea | 7c111cd3ba | |
tastytea | e22f82fc6f | |
tastytea | 6c0976baa7 | |
tastytea | eb47e38444 | |
tastytea | 4785f8ce64 | |
tastytea | d3d4786e21 | |
tastytea | bae3f09ed2 | |
tastytea | f4ab842787 | |
tastytea | b04cb01382 |
|
@ -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 ]
|
|
@ -1 +1,3 @@
|
||||||
/build/
|
/build/
|
||||||
|
/doc/
|
||||||
|
/update_doc.sh
|
||||||
|
|
|
@ -1,41 +1,57 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
# Support version 3.6 and above, but use policy settings up to 3.14.
|
||||||
project(xdgcfg
|
# 3.6 is needed because of IMPORTED_TARGET in pkg_check_modules().
|
||||||
VERSION 0.1.0
|
cmake_minimum_required(VERSION 3.6...3.14)
|
||||||
LANGUAGES CXX
|
# 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)
|
project(xdgcfg
|
||||||
find_package(PkgConfig REQUIRED)
|
VERSION 0.5.0
|
||||||
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
|
LANGUAGES CXX
|
||||||
pkg_check_modules(LIBCONFIG REQUIRED libconfig++)
|
)
|
||||||
|
# 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 14)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED YES)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
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})
|
add_subdirectory(src)
|
||||||
include_directories(${LIBCONFIG_INCLUDE_DIRS})
|
add_subdirectory(pkg-config)
|
||||||
|
add_subdirectory(cmake)
|
||||||
|
|
||||||
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
|
if(WITH_EXAMPLES)
|
||||||
link_directories(${LIBCONFIG_LIBRARY_DIRS})
|
add_subdirectory(examples)
|
||||||
|
endif()
|
||||||
|
|
||||||
add_library(xdgcfg SHARED src/xdgcfg.cpp)
|
if(WITH_TESTS)
|
||||||
set_target_properties(xdgcfg PROPERTIES
|
add_subdirectory(tests)
|
||||||
VERSION ${PROJECT_VERSION}
|
endif()
|
||||||
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})
|
|
||||||
|
|
|
@ -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
|
|
@ -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.
|
64
README.md
64
README.md
|
@ -1,30 +1,62 @@
|
||||||
**xdgcfg** is a very simple wrapper around libconfig written in C++. It reads
|
**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.
|
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
|
### Dependencies
|
||||||
|
|
||||||
* C++ compiler
|
* C++ compiler (Tested: g++ 5/8/9)
|
||||||
* [cmake](https://cmake.org/)
|
* [cmake](https://cmake.org/) (at least: 3.1)
|
||||||
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/)
|
* [pkgconfig](https://pkgconfig.freedesktop.org/wiki/) (tested: 0.29)
|
||||||
* [libconfig++](https://github.com/hyperrealm/libconfig)
|
* [libconfig++](https://github.com/hyperrealm/libconfig) (tested: 1.5)
|
||||||
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git)
|
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git) (tested: 1.2)
|
||||||
|
|
||||||
### Usage
|
### Compile
|
||||||
|
|
||||||
You can create dynamic and static libraries:
|
``` shell
|
||||||
|
|
||||||
```SH
|
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake ..
|
cmake ..
|
||||||
make
|
cmake --build .
|
||||||
make install
|
make install
|
||||||
```
|
```
|
||||||
|
|
||||||
Or just copy `xdgcfg.hpp` and `xdgcfg.cpp` into your project folder.
|
### CMake options
|
||||||
|
|
||||||
### Documentation
|
* `-DWITH_TESTS=YES` to compile the tests.
|
||||||
|
* `-DWITH_EXAMPLES=YES` to compile the example.
|
||||||
`xdgcfg.hpp` has explanatory comments in it and there is an
|
* `-DBUILD_SHARED_LIBS=NO` to build a static library.
|
||||||
[example](src/example.cpp).
|
|
||||||
|
|
|
@ -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
|
|
@ -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}")
|
|
@ -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")
|
|
@ -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)
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,16 +1,31 @@
|
||||||
/* Public Domain / CC-0
|
/* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||||
* Author: 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
|
#ifndef XDGCFG_HPP
|
||||||
#define XDGCFG_HPP
|
#define XDGCFG_HPP
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
#include <filesystem>
|
||||||
|
#else
|
||||||
|
#include <experimental/filesystem>
|
||||||
|
#endif
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <libconfig.h++>
|
#include <libconfig.h++>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
#else
|
||||||
|
namespace fs = std::experimental::filesystem;
|
||||||
|
#endif
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::uint_fast8_t;
|
using std::uint8_t;
|
||||||
|
using std::cerr;
|
||||||
|
using std::endl;
|
||||||
|
|
||||||
class xdgcfg
|
class xdgcfg
|
||||||
{
|
{
|
||||||
|
@ -33,14 +48,14 @@ public:
|
||||||
*
|
*
|
||||||
* @return 0 on success, 1 on I/O error, 2 on parse error.
|
* @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
|
* @brief Write the file
|
||||||
*
|
*
|
||||||
* @return `true` on success
|
* @return `true` on success
|
||||||
*/
|
*/
|
||||||
const bool write();
|
bool write();
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns a reference to the config as libconfig::Config
|
* @brief Returns a reference to the config as libconfig::Config
|
||||||
|
@ -55,17 +70,17 @@ public:
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns the complete filepath
|
* @brief Returns the complete filepath
|
||||||
*/
|
*/
|
||||||
const string get_filepath() const;
|
const fs::path get_filepath() const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Sets verbosity
|
* @brief Sets verbosity
|
||||||
*/
|
*/
|
||||||
const void set_verbose(bool verbose);
|
void set_verbose(bool verbose);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Returns verbosity
|
* @brief Returns verbosity
|
||||||
*/
|
*/
|
||||||
const bool get_verbose() const;
|
bool get_verbose() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*!
|
/*!
|
||||||
|
@ -76,7 +91,7 @@ private:
|
||||||
/*!
|
/*!
|
||||||
* Complete filepath
|
* Complete filepath
|
||||||
*/
|
*/
|
||||||
string _filepath;
|
fs::path _filepath;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Print out error messages if true
|
* Print out error messages if true
|
||||||
|
@ -84,4 +99,8 @@ private:
|
||||||
bool _verbose;
|
bool _verbose;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @example example.cpp
|
||||||
|
*/
|
||||||
|
|
||||||
#endif // XDGCFG_HPP
|
#endif // XDGCFG_HPP
|
|
@ -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")
|
|
@ -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
|
|
@ -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}")
|
|
@ -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;
|
|
||||||
}
|
|
|
@ -1,25 +1,12 @@
|
||||||
/* Public Domain / CC-0
|
/* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
||||||
* Author: 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 <basedir.h>
|
||||||
#include "xdgcfg.hpp"
|
#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)
|
xdgcfg::xdgcfg(const string &filename, const string &subdir)
|
||||||
: _cfg()
|
: _cfg()
|
||||||
, _verbose(false)
|
, _verbose(false)
|
||||||
|
@ -31,16 +18,16 @@ xdgcfg::xdgcfg(const string &filename, const string &subdir)
|
||||||
|
|
||||||
if (!subdir.empty())
|
if (!subdir.empty())
|
||||||
{
|
{
|
||||||
_filepath += '/' + subdir;
|
_filepath /= subdir;
|
||||||
if (!fs::exists(_filepath))
|
|
||||||
{
|
|
||||||
fs::create_directory(_filepath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_filepath += '/' + filename;
|
if (!fs::exists(_filepath))
|
||||||
|
{
|
||||||
|
fs::create_directories(_filepath);
|
||||||
|
}
|
||||||
|
_filepath /= filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint_fast8_t xdgcfg::read()
|
uint8_t xdgcfg::read()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -68,7 +55,7 @@ const uint_fast8_t xdgcfg::read()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool xdgcfg::write()
|
bool xdgcfg::write()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -92,17 +79,17 @@ libconfig::Config &xdgcfg::get_cfg()
|
||||||
return _cfg;
|
return _cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string xdgcfg::get_filepath() const
|
const fs::path xdgcfg::get_filepath() const
|
||||||
{
|
{
|
||||||
return _filepath;
|
return _filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
const void xdgcfg::set_verbose(bool verbose)
|
void xdgcfg::set_verbose(bool verbose)
|
||||||
{
|
{
|
||||||
_verbose = verbose;
|
_verbose = verbose;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool xdgcfg::get_verbose() const
|
bool xdgcfg::get_verbose() const
|
||||||
{
|
{
|
||||||
return _verbose;
|
return _verbose;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
|
@ -0,0 +1,5 @@
|
||||||
|
/* This file is part of xdgcfg. */
|
||||||
|
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
|
||||||
|
#include <catch.hpp>
|
|
@ -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! 🙂");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue