Blob Blame History Raw
From 54a5d391f4107bff58b5e1d9cb248975968ea074 Mon Sep 17 00:00:00 2001
From: Kjell Ahlstedt <kjellahlstedt@gmail.com>
Date: Fri, 20 Mar 2020 18:58:17 +0100
Subject: [PATCH] Glib::build_filename(): Fix the template overload

and add some tests to tests/glibmm_buildfilename/main.cc.

Fixes #71
---
 glib/src/miscutils.hg              | 15 ++++++++++-----
 tests/glibmm_buildfilename/main.cc | 25 +++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/glib/src/miscutils.hg b/glib/src/miscutils.hg
index f4742542..9fe6ea7d 100644
--- a/glib/src/miscutils.hg
+++ b/glib/src/miscutils.hg
@@ -389,20 +389,25 @@ std::string canonicalize_filename(StdStringView filename, StdStringView relative
 GLIBMM_API
 std::string build_filename(const Glib::ArrayHandle<std::string>& elements);
 
-/** Creates a filename from one or more elements using the correct separator for filenames.
+/** Creates a filename from two or more elements using the correct separator for filenames.
  * No attempt is made to force the resulting filename to be an absolute path.
  * If the first element is a relative path, the result will be a relative path.
+ * @tparam String1 std::string or const char*.
+ * @tparam String2 std::string or const char*.
  * @tparam Strings std::string or const char*.
- * @param strings The path elements.
+ * @param elem1 First path element.
+ * @param elem2 Second path element.
+ * @param strings The following path elements, if any.
  * @return The resulting path.
  *
  * @newin{2,64}
  */
-template <typename... Strings>
-std::string build_filename(const Strings&... strings)
+template <typename String1, typename String2, typename... Strings>
+std::string build_filename(const String1& elem1, const String2& elem2, const Strings&... strings)
 {
   return Glib::convert_return_gchar_ptr_to_stdstring(
-    g_build_filename(StdStringView(strings).c_str()..., nullptr));
+    g_build_filename(StdStringView(elem1).c_str(), StdStringView(elem2).c_str(),
+      StdStringView(strings).c_str()..., nullptr));
 }
 
 // When the templated build_filename() overload was added, the following
diff --git a/tests/glibmm_buildfilename/main.cc b/tests/glibmm_buildfilename/main.cc
index 2460622b..c72f2066 100644
--- a/tests/glibmm_buildfilename/main.cc
+++ b/tests/glibmm_buildfilename/main.cc
@@ -1,6 +1,7 @@
 #include <glibmm.h>
 #include <iostream>
 #include <string.h>
+#include <vector>
 
 // Use this line if you want debug output:
 // std::ostream& ostr = std::cout;
@@ -39,5 +40,29 @@ main(int, char**)
   path = Glib::build_filename(dir_1, dir_2, dir_1, dir_3, dir_2, dir_3, dir_1, dir_2, file_2);
   ostr << "Path 5: " << path << std::endl;
 
+  path = Glib::build_filename(dir_2, file_2);
+  ostr << "Path 6: " << path << std::endl;
+
+  path = Glib::build_filename(dir_2, file_3);
+  ostr << "Path 7: " << path << std::endl;
+
+  path = Glib::build_filename(dir_3, file_3);
+  ostr << "Path 8: " << path << std::endl;
+
+//  path = Glib::build_filename(dir_1);
+//  ostr << "Path 9: " << path << std::endl;
+
+//  path = Glib::build_filename(nullptr);
+//  ostr << "Path 10: " << path << std::endl;
+
+  std::vector<std::string> pathv;
+  pathv.push_back("vdir1");
+  path = Glib::build_filename(pathv);
+  ostr << "Path v1: " << path << std::endl;
+
+  pathv.push_back("vdir2");
+  path = Glib::build_filename(pathv);
+  ostr << "Path v2: " << path << std::endl;
+
   return EXIT_SUCCESS;
 }
-- 
2.25.2

diff --git a/glib/glibmm/miscutils.h b/glib/glibmm/miscutils.h
index ec56f36..69442a2 100644
--- a/glib/glibmm/miscutils.h
+++ b/glib/glibmm/miscutils.h
@@ -517,20 +517,25 @@ std::string canonicalize_filename(StdStringView filename, StdStringView relative
 GLIBMM_API
 std::string build_filename(const Glib::ArrayHandle<std::string>& elements);
 
-/** Creates a filename from one or more elements using the correct separator for filenames.
+/** Creates a filename from two or more elements using the correct separator for filenames.
  * No attempt is made to force the resulting filename to be an absolute path.
  * If the first element is a relative path, the result will be a relative path.
+ * @tparam String1 std::string or const char*.
+ * @tparam String2 std::string or const char*.
  * @tparam Strings std::string or const char*.
- * @param strings The path elements.
+ * @param elem1 First path element.
+ * @param elem2 Second path element.
+ * @param strings The following path elements, if any.
  * @return The resulting path.
  *
  * @newin{2,64}
  */
-template <typename... Strings>
-std::string build_filename(const Strings&... strings)
+template <typename String1, typename String2, typename... Strings>
+std::string build_filename(const String1& elem1, const String2& elem2, const Strings&... strings)
 {
   return Glib::convert_return_gchar_ptr_to_stdstring(
-    g_build_filename(StdStringView(strings).c_str()..., nullptr));
+    g_build_filename(StdStringView(elem1).c_str(), StdStringView(elem2).c_str(),
+      StdStringView(strings).c_str()..., nullptr));
 }
 
 // When the templated build_filename() overload was added, the following