From ea0b2f2bb871e7760d1936bb9b193655682df413 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Fri, 18 Sep 2015 09:18:33 +0200 Subject: Create a xdg_path function --- louloulibs/louloulibs.h.cmake | 3 ++- louloulibs/utils/xdg.cpp | 20 ++++++++++++++++++++ louloulibs/utils/xdg.hpp | 13 +++++++++++++ src/main.cpp | 16 ++-------------- src/test.cpp | 22 ++++++++++++++++++++++ 5 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 louloulibs/utils/xdg.cpp create mode 100644 louloulibs/utils/xdg.hpp diff --git a/louloulibs/louloulibs.h.cmake b/louloulibs/louloulibs.h.cmake index 707d3fe..2feaf4e 100644 --- a/louloulibs/louloulibs.h.cmake +++ b/louloulibs/louloulibs.h.cmake @@ -5,4 +5,5 @@ #cmakedefine POLLER ${POLLER} #cmakedefine BOTAN_FOUND #cmakedefine CARES_FOUND -#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}" \ No newline at end of file +#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}" +#cmakedefine PROJECT_NAME "${PROJECT_NAME}" \ No newline at end of file diff --git a/louloulibs/utils/xdg.cpp b/louloulibs/utils/xdg.cpp new file mode 100644 index 0000000..7a60dd8 --- /dev/null +++ b/louloulibs/utils/xdg.cpp @@ -0,0 +1,20 @@ +#include +#include + +#include "louloulibs.h" + +std::string xdg_config_path(const std::string& filename) +{ + const char* xdg_config_home = ::getenv("XDG_CONFIG_HOME"); + if (xdg_config_home && xdg_config_home[0] == '/') + return std::string{xdg_config_home} + "/" PROJECT_NAME "/" + filename; + else + { + const char* home = ::getenv("HOME"); + if (home) + return std::string{home} + "/" ".config" "/" PROJECT_NAME "/" + filename; + else + return filename; + } +} + diff --git a/louloulibs/utils/xdg.hpp b/louloulibs/utils/xdg.hpp new file mode 100644 index 0000000..20f5bb7 --- /dev/null +++ b/louloulibs/utils/xdg.hpp @@ -0,0 +1,13 @@ +#ifndef XDG_HPP_INCLUDED +#define XDG_HPP_INCLUDED + +#include + +/** + * Returns a path for the given filename, according to the XDG base + * directory specification, see + * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ +std::string xdg_config_path(const std::string& filename); + +#endif /* XDG_HPP_INCLUDED */ diff --git a/src/main.cpp b/src/main.cpp index 159dcbe..5c9e071 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,11 +4,11 @@ #include #include #include +#include #include #include #include -#include #include @@ -69,19 +69,7 @@ int main(int ac, char** av) if (ac > 1) Config::filename = av[1]; else - { - const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); - if (xdg_config_home && xdg_config_home[0] == '/') - Config::filename = std::string{xdg_config_home} + "/" "biboumi" "/" "biboumi.cfg"; - else - { - const char* home = getenv("HOME"); - if (home) - Config::filename = std::string{home} + "/" ".config" "/" "biboumi" "/" "biboumi.cfg"; - else - Config::filename = "biboumi.cfg"; - } - } + Config::filename = xdg_path("biboumi.cfg"); Config::file_must_exist = true; std::cerr << "Using configuration file: " << Config::filename << std::endl; diff --git a/src/test.cpp b/src/test.cpp index 842dfed..b8737e6 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -402,6 +402,28 @@ int main() assert(iid6.is_channel); assert(!iid6.is_user); } + { + + { + std::cout << color << "Testing the xdg_path function…" << reset << std::endl; + std::string res; + + ::unsetenv("XDG_CONFIG_HOME"); + ::unsetenv("HOME"); + res = xdg_config_path("coucou.txt"); + std::cout << res << std::endl; + assert(res == "coucou.txt"); + + ::setenv("HOME", "/home/user", 1); + res = xdg_config_path("coucou.txt"); + std::cout << res << std::endl; + assert(res == "/home/user/.config/biboumi/coucou.txt"); + + ::setenv("XDG_CONFIG_HOME", "/some_weird_dir", 1); + res = xdg_config_path("coucou.txt"); + std::cout << res << std::endl; + assert(res == "/some_weird_dir/biboumi/coucou.txt"); + } return 0; } -- cgit v1.2.3