changeset 604:ff11ca6b0d55

Xdg: use new style
author David Demelier <markand@malikania.fr>
date Mon, 05 Dec 2016 21:07:55 +0100
parents 05f830e0539b
children 0f9db77f5aa5
files modules/xdg/test/main.cpp modules/xdg/xdg.hpp
diffstat 2 files changed, 85 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/modules/xdg/test/main.cpp	Fri Dec 02 22:22:17 2016 +0100
+++ b/modules/xdg/test/main.cpp	Mon Dec 05 21:07:55 2016 +0100
@@ -26,7 +26,7 @@
 
 std::string myhome;
 
-}
+} // !namespace
 
 #if defined(_WIN32)
 
@@ -39,7 +39,7 @@
     return true;
 }
 
-inline bool setenv(const std::string &name, const std::string &value, bool)
+inline bool setenv(const std::string& name, const std::string& value, bool)
 {
     std::string t = name + "=" + value;
 
@@ -54,117 +54,117 @@
 {
     ASSERT_TRUE(unsetenv("XDG_CONFIG_HOME") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.config", xdg.configHome());
+    ASSERT_EQ(myhome + "/.config", xdg.config_home());
 }
 
 TEST(HomeEmpty, data)
 {
     ASSERT_TRUE(unsetenv("XDG_DATA_HOME") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.local/share", xdg.dataHome());
+    ASSERT_EQ(myhome + "/.local/share", xdg.data_home());
 }
 
 TEST(HomeEmpty, cache)
 {
     ASSERT_TRUE(unsetenv("XDG_CACHE_HOME") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.cache", xdg.cacheHome());
+    ASSERT_EQ(myhome + "/.cache", xdg.cache_home());
 }
 
 TEST(HomeEmpty, runtime)
 {
     ASSERT_TRUE(unsetenv("XDG_RUNTIME_DIR") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_TRUE(xdg.runtimeDir().empty());
+    ASSERT_TRUE(xdg.runtime_dir().empty());
 }
 
 TEST(HomeValid, config)
 {
     ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "/test/config", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ("/test/config", xdg.configHome());
+    ASSERT_EQ("/test/config", xdg.config_home());
 }
 
 TEST(HomeValid, data)
 {
     ASSERT_TRUE(setenv("XDG_DATA_HOME", "/test/data", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ("/test/data", xdg.dataHome());
+    ASSERT_EQ("/test/data", xdg.data_home());
 }
 
 TEST(HomeValid, cache)
 {
     ASSERT_TRUE(setenv("XDG_CACHE_HOME", "/test/cache", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ("/test/cache", xdg.cacheHome());
+    ASSERT_EQ("/test/cache", xdg.cache_home());
 }
 
 TEST(HomeValid, runtime)
 {
     ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "/test/runtime", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ("/test/runtime", xdg.runtimeDir());
+    ASSERT_EQ("/test/runtime", xdg.runtime_dir());
 }
 
 TEST(HomeInvalid, config)
 {
     ASSERT_TRUE(setenv("XDG_CONFIG_HOME", "invalid", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.config", xdg.configHome());
+    ASSERT_EQ(myhome + "/.config", xdg.config_home());
 }
 
 TEST(HomeInvalid, data)
 {
     ASSERT_TRUE(setenv("XDG_DATA_HOME", "invalid", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.local/share", xdg.dataHome());
+    ASSERT_EQ(myhome + "/.local/share", xdg.data_home());
 }
 
 TEST(HomeInvalid, cache)
 {
     ASSERT_TRUE(setenv("XDG_CACHE_HOME", "invalid", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_EQ(myhome + "/.cache", xdg.cacheHome());
+    ASSERT_EQ(myhome + "/.cache", xdg.cache_home());
 }
 
 TEST(HomeInvalid, runtime)
 {
     ASSERT_TRUE(setenv("XDG_RUNTIME_DIR", "invalid", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    ASSERT_TRUE(xdg.runtimeDir().empty());
+    ASSERT_TRUE(xdg.runtime_dir().empty());
 }
 
 TEST(DirectoriesEmpty, config)
 {
     ASSERT_TRUE(unsetenv("XDG_CONFIG_DIRS") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.configDirs();
+    const auto &list = xdg.config_dirs();
 
     ASSERT_EQ((size_t)1, list.size());
     ASSERT_EQ("/etc/xdg", list[0]);
@@ -174,9 +174,9 @@
 {
     ASSERT_TRUE(unsetenv("XDG_DATA_DIRS") == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.dataDirs();
+    const auto &list = xdg.data_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/usr/local/share", list[0]);
@@ -187,9 +187,9 @@
 {
     ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:/config2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.configDirs();
+    const auto &list = xdg.config_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/config1", list[0]);
@@ -200,9 +200,9 @@
 {
     ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:/data2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.dataDirs();
+    const auto &list = xdg.data_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/data1", list[0]);
@@ -213,9 +213,9 @@
 {
     ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "bad1:bad2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.configDirs();
+    const auto &list = xdg.config_dirs();
 
     ASSERT_EQ((size_t)1, list.size());
     ASSERT_EQ("/etc/xdg", list[0]);
@@ -225,9 +225,9 @@
 {
     ASSERT_TRUE(setenv("XDG_DATA_DIRS", "bad1:bad2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.dataDirs();
+    const auto &list = xdg.data_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/usr/local/share", list[0]);
@@ -238,9 +238,9 @@
 {
     ASSERT_TRUE(setenv("XDG_CONFIG_DIRS", "/config1:bad:/config2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.configDirs();
+    const auto &list = xdg.config_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/config1", list[0]);
@@ -251,9 +251,9 @@
 {
     ASSERT_TRUE(setenv("XDG_DATA_DIRS", "/data1:bad:/data2", true) == 0);
 
-    Xdg xdg;
+    xdg xdg;
 
-    const auto &list = xdg.dataDirs();
+    const auto &list = xdg.data_dirs();
 
     ASSERT_EQ((size_t)2, list.size());
     ASSERT_EQ("/data1", list[0]);
--- a/modules/xdg/xdg.hpp	Fri Dec 02 22:22:17 2016 +0100
+++ b/modules/xdg/xdg.hpp	Mon Dec 05 21:07:55 2016 +0100
@@ -32,7 +32,6 @@
 #include <vector>
 
 /**
- * \class Xdg
  * \brief XDG directory specifications.
  *
  * Read and get XDG directories.
@@ -40,42 +39,45 @@
  * This file should compiles on Windows to facilitate portability but its
  * functions must not be used.
  */
-class Xdg {
+class xdg {
 private:
-    std::string m_configHome;
-    std::string m_dataHome;
-    std::string m_cacheHome;
-    std::string m_runtimeDir;
-    std::vector<std::string> m_configDirs;
-    std::vector<std::string> m_dataDirs;
+    std::string m_config_home;
+    std::string m_data_home;
+    std::string m_cache_home;
+    std::string m_runtime_dir;
+    std::vector<std::string> m_config_dirs;
+    std::vector<std::string> m_data_dirs;
 
-    bool isabsolute(const std::string &path) const noexcept
+    inline bool is_absolute(const std::string& path) const noexcept
     {
         return path.length() > 0 && path[0] == '/';
     }
 
-    std::vector<std::string> split(const std::string &arg) const
+    std::vector<std::string> split(const std::string& arg) const
     {
         std::stringstream iss(arg);
         std::string item;
         std::vector<std::string> elems;
 
-        while (std::getline(iss, item, ':'))
-            if (isabsolute(item))
+        while (std::getline(iss, item, ':')) {
+            if (is_absolute(item)) {
                 elems.push_back(item);
+            }
+        }
 
         return elems;
     }
 
-    std::string envOrHome(const std::string &var, const std::string &repl) const
+    std::string env_or_home(const std::string& var, const std::string& repl) const
     {
         auto value = std::getenv(var.c_str());
 
-        if (value == nullptr || !isabsolute(value)) {
+        if (value == nullptr || !is_absolute(value)) {
             auto home = std::getenv("HOME");
 
-            if (home == nullptr)
+            if (home == nullptr) {
                 throw std::runtime_error("could not get home directory");
+            }
 
             return std::string(home) + "/" + repl;
         }
@@ -83,13 +85,14 @@
         return value;
     }
 
-    std::vector<std::string> listOrDefaults(const std::string &var,
-                                            const std::vector<std::string> &list) const
+    std::vector<std::string> list_or_defaults(const std::string& var,
+                                              const std::vector<std::string>& list) const
     {
         auto value = std::getenv(var.c_str());
 
-        if (!value)
+        if (!value) {
             return list;
+        }
 
         // No valid item at all? Use defaults.
         auto result = split(value);
@@ -103,22 +106,24 @@
      *
      * \throw std::runtime_error on failures
      */
-    Xdg()
+    xdg()
     {
-        m_configHome    = envOrHome("XDG_CONFIG_HOME", ".config");
-        m_dataHome      = envOrHome("XDG_DATA_HOME", ".local/share");
-        m_cacheHome     = envOrHome("XDG_CACHE_HOME", ".cache");
+        m_config_home    = env_or_home("XDG_CONFIG_HOME", ".config");
+        m_data_home      = env_or_home("XDG_DATA_HOME", ".local/share");
+        m_cache_home     = env_or_home("XDG_CACHE_HOME", ".cache");
 
-        m_configDirs    = listOrDefaults("XDG_CONFIG_DIRS", { "/etc/xdg" });
-        m_dataDirs      = listOrDefaults("XDG_DATA_DIRS", { "/usr/local/share", "/usr/share" });
+        m_config_dirs    = list_or_defaults("XDG_CONFIG_DIRS", { "/etc/xdg" });
+        m_data_dirs      = list_or_defaults("XDG_DATA_DIRS", { "/usr/local/share", "/usr/share" });
 
         /*
          * Runtime directory is a special case and does not have a replacement,
          * the application should manage this by itself.
          */
         auto runtime = std::getenv("XDG_RUNTIME_DIR");
-        if (runtime && isabsolute(runtime))
-            m_runtimeDir = runtime;
+
+        if (runtime && is_absolute(runtime)) {
+            m_runtime_dir = runtime;
+        }
     }
 
     /**
@@ -126,9 +131,9 @@
      *
      * \return the config directory
      */
-    inline const std::string &configHome() const noexcept
+    inline const std::string& config_home() const noexcept
     {
-        return m_configHome;
+        return m_config_home;
     }
 
     /**
@@ -136,9 +141,9 @@
      *
      * \return the data directory
      */
-    inline const std::string &dataHome() const noexcept
+    inline const std::string& data_home() const noexcept
     {
-        return m_dataHome;
+        return m_data_home;
     }
 
     /**
@@ -146,9 +151,9 @@
      *
      * \return the cache directory
      */
-    inline const std::string &cacheHome() const noexcept
+    inline const std::string& cache_home() const noexcept
     {
-        return m_cacheHome;
+        return m_cache_home;
     }
 
     /**
@@ -159,9 +164,9 @@
      *
      * \return the runtime directory
      */
-    inline const std::string &runtimeDir() const noexcept
+    inline const std::string& runtime_dir() const noexcept
     {
-        return m_runtimeDir;
+        return m_runtime_dir;
     }
 
     /**
@@ -169,9 +174,9 @@
      *
      * \return the list of config directories
      */
-    inline const std::vector<std::string> &configDirs() const noexcept
+    inline const std::vector<std::string>& config_dirs() const noexcept
     {
-        return m_configDirs;
+        return m_config_dirs;
     }
 
     /**
@@ -180,9 +185,9 @@
      *
      * \return the list of data directories
      */
-    inline const std::vector<std::string> &dataDirs() const noexcept
+    inline const std::vector<std::string>& data_dirs() const noexcept
     {
-        return m_dataDirs;
+        return m_data_dirs;
     }
 };