Create a xdg_path function
This commit is contained in:
@@ -6,3 +6,4 @@
|
|||||||
#cmakedefine BOTAN_FOUND
|
#cmakedefine BOTAN_FOUND
|
||||||
#cmakedefine CARES_FOUND
|
#cmakedefine CARES_FOUND
|
||||||
#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}"
|
#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}"
|
||||||
|
#cmakedefine PROJECT_NAME "${PROJECT_NAME}"
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <utils/xdg.hpp>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef XDG_HPP_INCLUDED
|
||||||
|
#define XDG_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 */
|
||||||
+2
-14
@@ -4,11 +4,11 @@
|
|||||||
#include <config/config.hpp>
|
#include <config/config.hpp>
|
||||||
#include <logger/logger.hpp>
|
#include <logger/logger.hpp>
|
||||||
#include <utils/reload.hpp>
|
#include <utils/reload.hpp>
|
||||||
|
#include <utils/xdg.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstdlib>
|
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
@@ -69,19 +69,7 @@ int main(int ac, char** av)
|
|||||||
if (ac > 1)
|
if (ac > 1)
|
||||||
Config::filename = av[1];
|
Config::filename = av[1];
|
||||||
else
|
else
|
||||||
{
|
Config::filename = xdg_path("biboumi.cfg");
|
||||||
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::file_must_exist = true;
|
Config::file_must_exist = true;
|
||||||
std::cerr << "Using configuration file: " << Config::filename << std::endl;
|
std::cerr << "Using configuration file: " << Config::filename << std::endl;
|
||||||
|
|||||||
@@ -402,6 +402,28 @@ int main()
|
|||||||
assert(iid6.is_channel);
|
assert(iid6.is_channel);
|
||||||
assert(!iid6.is_user);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user