gnucash stable: gnc_list_all_paths returns std::vector instead of GList of EnvPaths

Christopher Lam clam at code.gnucash.org
Mon May 22 00:43:50 EDT 2023


Updated	 via  https://github.com/Gnucash/gnucash/commit/89f7e893 (commit)
	from  https://github.com/Gnucash/gnucash/commit/bba49a6d (commit)



commit 89f7e8933b0efb6169cb648b9059720dfb9f8700
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Mon May 22 09:45:33 2023 +0800

    gnc_list_all_paths returns std::vector instead of GList of EnvPaths
    
    because its consumers are now cpp there's no need to return a GList.

diff --git a/gnucash/gnome-utils/gnc-main-window.cpp b/gnucash/gnome-utils/gnc-main-window.cpp
index 7db7a5b0ee..e3248fad16 100644
--- a/gnucash/gnome-utils/gnc-main-window.cpp
+++ b/gnucash/gnome-utils/gnc-main-window.cpp
@@ -5160,7 +5160,6 @@ add_about_paths (GtkDialog *dialog)
 {
     GtkWidget *page_vbox = gnc_get_dialog_widget_from_id (dialog, "page_vbox");
     GtkWidget *grid;
-    GList *paths;
     gint i = 0;
 
     if (!page_vbox)
@@ -5170,15 +5169,12 @@ add_about_paths (GtkDialog *dialog)
     }
 
     grid = gtk_grid_new ();
-    paths = gnc_list_all_paths ();
 
-    for (GList *path_node = paths; path_node; path_node = g_list_next (path_node))
+    for (const auto& ep : gnc_list_all_paths ())
     {
-        EnvPaths *ep = (EnvPaths*)path_node->data;
-
-        gchar *env_name = g_strconcat (ep->env_name, ":", NULL);
+        gchar *env_name = g_strconcat (ep.env_name, ":", NULL);
         GtkWidget *label = gtk_label_new (env_name);
-        const gchar *uri = gnc_uri_create_uri ("file", NULL, 0, NULL, NULL, ep->env_path);
+        const gchar *uri = gnc_uri_create_uri ("file", NULL, 0, NULL, NULL, ep.env_path);
         gchar *display_uri = gnc_doclink_get_unescaped_just_uri (uri);
         GtkWidget *widget = gtk_link_button_new_with_label (uri, display_uri);
 
@@ -5189,7 +5185,7 @@ add_about_paths (GtkDialog *dialog)
         gtk_widget_set_margin_top (widget, 0);
         gtk_widget_set_margin_bottom (widget, 0);
 
-        if (ep->modifiable)
+        if (ep.modifiable)
         {
             GtkWidget *mod_lab = gtk_label_new (_("(user modifiable)"));
             gtk_grid_attach (GTK_GRID(grid), mod_lab, 2, i, 1, 1);
@@ -5205,7 +5201,6 @@ add_about_paths (GtkDialog *dialog)
     gtk_container_add_with_properties (GTK_CONTAINER(page_vbox), grid,
                                        "position", 1, NULL);
     gtk_widget_show_all (grid);
-    g_list_free_full (paths, g_free);
 }
 
 /** Create and display the "about" dialog for gnucash.
diff --git a/gnucash/gnucash-core-app.cpp b/gnucash/gnucash-core-app.cpp
index f307e6ab94..4746765d09 100644
--- a/gnucash/gnucash-core-app.cpp
+++ b/gnucash/gnucash-core-app.cpp
@@ -242,17 +242,14 @@ Gnucash::CoreApp::parse_command_line (int argc, char **argv)
 
     if (m_show_paths)
     {
-        auto paths { gnc_list_all_paths ()};
         std::cout << _("GnuCash Paths") << '\n';
-        for (auto n = paths; n; n = n->next)
+        for (const auto& ep : gnc_list_all_paths ())
         {
-            auto it = static_cast<EnvPaths*>(n->data);
-            std::cout << it->env_name << ": " << it->env_path;
-            if (it->modifiable)
+            std::cout << ep.env_name << ": " << ep.env_path;
+            if (ep.modifiable)
                 std::cout << ' ' << _("(user modifiable)");
             std::cout << '\n';
         }
-        g_list_free_full (paths, g_free);
         exit (0);
     }
 
diff --git a/libgnucash/core-utils/gnc-filepath-utils.cpp b/libgnucash/core-utils/gnc-filepath-utils.cpp
index 1fdf64942a..ede4b9d3d1 100644
--- a/libgnucash/core-utils/gnc-filepath-utils.cpp
+++ b/libgnucash/core-utils/gnc-filepath-utils.cpp
@@ -68,7 +68,6 @@
 #include <boost/locale.hpp>
 #include <regex>
 #include <iostream>
-#include <numeric>
 
 /* Below cvt and bfs_locale should be used with boost::filesystem::path (bfs)
  * objects created alter in this source file. The rationale is as follows:
@@ -1307,27 +1306,20 @@ gnc_filepath_locate_doc_file (const gchar *name)
     return result;
 }
 
-GList *
-gnc_list_all_paths (void)
+std::vector<EnvPaths>
+gnc_list_all_paths ()
 {
     if (gnc_userdata_home.empty())
         gnc_filepath_init ();
 
-    std::vector<EnvPaths> paths
-        { { "GNC_USERDATA_DIR", gnc_userdata_home_str.c_str(), true},
-          { "GNC_USERCONFIG_DIR", gnc_userconfig_home_str.c_str(), true },
-          { "GNC_BIN", g_getenv ("GNC_BIN"), false },
-          { "GNC_LIB", g_getenv ("GNC_LIB"), false },
-          { "GNC_CONF", g_getenv ("GNC_CONF"), false },
-          { "GNC_DATA", g_getenv ("GNC_DATA"), false },
-        };
-    auto accum = [](const auto& a, const auto& b)
-    {
-        EnvPaths *ep = g_new0 (EnvPaths, 1);
-        *ep = b;
-        return g_list_prepend (a, ep);
+    return {
+        { "GNC_USERDATA_DIR", gnc_userdata_home_str.c_str(), true},
+        { "GNC_USERCONFIG_DIR", gnc_userconfig_home_str.c_str(), true },
+        { "GNC_BIN", g_getenv ("GNC_BIN"), false },
+        { "GNC_LIB", g_getenv ("GNC_LIB"), false },
+        { "GNC_CONF", g_getenv ("GNC_CONF"), false },
+        { "GNC_DATA", g_getenv ("GNC_DATA"), false },
     };
-    return std::accumulate (paths.rbegin(), paths.rend(), (GList*) nullptr, accum);
 }
 
 static const std::regex
diff --git a/libgnucash/core-utils/gnc-filepath-utils.h b/libgnucash/core-utils/gnc-filepath-utils.h
index 79b4158eee..38c5ca3afe 100644
--- a/libgnucash/core-utils/gnc-filepath-utils.h
+++ b/libgnucash/core-utils/gnc-filepath-utils.h
@@ -178,24 +178,6 @@ gchar *gnc_filepath_locate_ui_file (const gchar *name);
  */
 gchar *gnc_filepath_locate_doc_file (const gchar *name);
 
-typedef struct
-{
-    const gchar *env_name;
-    const gchar *env_path;
-    gboolean modifiable;
-} EnvPaths;
-
-
-/** Returns a GList* of the environment variables used by GnuCash.
- *
- *  @return a GList* of EnvPaths structs, describing the environment
- *  variables used by GnuCash.
- *
- *  @note It is the caller's responsibility to free the GList with
- *  g_list_free_full (paths, g_free)
- */
-GList *gnc_list_all_paths (void);
-
 gboolean gnc_filename_is_backup (const char *filename);
 
 gboolean gnc_filename_is_datafile (const char *filename);
@@ -217,6 +199,23 @@ gboolean gnc_filename_is_datafile (const char *filename);
  */
 std::ofstream gnc_open_filestream(const char *path);
 
+#include <vector>
+
+struct EnvPaths
+{
+    const gchar *env_name;
+    const gchar *env_path;
+    gboolean modifiable;
+};
+
+
+/** Returns a vector of the environment variables used by GnuCash.
+ *
+ *  @return a vector of EnvPaths structs, describing the environment
+ *  variables used by GnuCash.
+ */
+std::vector<EnvPaths> gnc_list_all_paths ();
+
 #endif
 
 #endif /* GNC_FILEPATH_UTILS_H */



Summary of changes:
 gnucash/gnome-utils/gnc-main-window.cpp      | 13 ++++-------
 gnucash/gnucash-core-app.cpp                 |  9 +++----
 libgnucash/core-utils/gnc-filepath-utils.cpp | 26 +++++++--------------
 libgnucash/core-utils/gnc-filepath-utils.h   | 35 ++++++++++++++--------------
 4 files changed, 33 insertions(+), 50 deletions(-)



More information about the gnucash-changes mailing list