Skip to content

Commit

Permalink
gnc_commodity_table_get_namespaces returns std::vector<std::string>
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Apr 13, 2024
1 parent 01bffa4 commit 6ff411f
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 125 deletions.
9 changes: 3 additions & 6 deletions bindings/guile/gnc-optiondb.i
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace std {

%begin
%{
#include <gnc-commodity.hpp>
#include <gnc-optiondb.h>
#include <gnc-optiondb.hpp>
#include <gnc-optiondb-impl.hpp>
Expand Down Expand Up @@ -1982,13 +1983,9 @@ gnc_register_multichoice_callback_option(GncOptionDBPtr& db,
gnc_commodity* commodity{};
const auto book{qof_session_get_book(gnc_get_current_session())};
const auto commodity_table{gnc_commodity_table_get_table(book)};
const auto namespaces{gnc_commodity_table_get_namespaces(commodity_table)};
for (auto node = namespaces; node && commodity == nullptr;
node = g_list_next(node))
for (const auto& name_space : gnc_commodity_table_get_namespaces(commodity_table))
{
commodity = gnc_commodity_table_lookup(commodity_table,
(const char*)(node->data),
value);
commodity = gnc_commodity_table_lookup(commodity_table, name_space.c_str(), value);

if (commodity)
return gnc_make_commodity_option(section, name, key, doc_string,
Expand Down
24 changes: 12 additions & 12 deletions gnucash/gnome-utils/dialog-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@

#include "dialog-commodity.h"
#include "dialog-utils.h"
#include "gnc-commodity.hpp"
#include "gnc-engine.h"
#include "gnc-gtk-utils.h"
#include "gnc-gui-query.h"
#include "gnc-ui-util.h"
#include "gnc-ui.h"

#include <algorithm>

/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;

Expand Down Expand Up @@ -560,7 +563,7 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
GtkComboBox *combo_box;
GtkTreeModel *model;
GtkTreeIter iter, match;
GList *namespaces, *node;
std::vector<std::string> namespaces;
gboolean matched = FALSE;

g_return_if_fail(GTK_IS_COMBO_BOX (cbwe));
Expand All @@ -582,20 +585,18 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
case DIAG_COMM_NON_CURRENCY_SELECT:
namespaces =
gnc_commodity_table_get_namespaces (gnc_get_current_commodities());
node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_CURRENCY, collate);
if (node)
{
namespaces = g_list_remove_link (namespaces, node);
g_list_free_1 (node);
}

if (auto it = std::find (namespaces.begin(), namespaces.end(), GNC_COMMODITY_NS_CURRENCY);
it != namespaces.end())
namespaces.erase (it);

if (gnc_commodity_namespace_is_iso (init_string))
init_string = nullptr;
break;

case DIAG_COMM_CURRENCY:
default:
namespaces = g_list_prepend (nullptr, (gpointer)GNC_COMMODITY_NS_CURRENCY);
namespaces = { GNC_COMMODITY_NS_CURRENCY };
break;
}

Expand Down Expand Up @@ -623,10 +624,10 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
}

/* add all others to the combobox */
namespaces = g_list_sort(namespaces, collate);
for (node = namespaces; node; node = node->next)
std::sort (namespaces.begin(), namespaces.end());
for (const auto& ns_str : namespaces)
{
auto ns = static_cast<const char*>(node->data);
auto ns = ns_str.c_str();
/* Skip template, legacy and currency namespaces.
The latter was added as first entry earlier */
if ((g_utf8_collate(ns, GNC_COMMODITY_NS_LEGACY) == 0) ||
Expand All @@ -650,7 +651,6 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,

if (matched)
gtk_combo_box_set_active_iter (combo_box, &match);
g_list_free(namespaces);
}


Expand Down
6 changes: 2 additions & 4 deletions gnucash/gnome/dialog-price-edit-db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
auto oldest = gnc_time (nullptr);
auto model = gtk_tree_view_get_model (view);
const auto commodity_table = gnc_get_current_commodities ();
auto namespace_list = gnc_commodity_table_get_namespaces (commodity_table);

for (auto node_n = namespace_list; node_n; node_n = g_list_next (node_n))
for (const auto& tmp_namespace_str : gnc_commodity_table_get_namespaces (commodity_table))
{
auto tmp_namespace = static_cast<char*>(node_n->data);
auto tmp_namespace = tmp_namespace_str.c_str();
DEBUG("Looking at namespace %s", tmp_namespace);
auto commodity_list = gnc_commodity_table_get_commodities (commodity_table, tmp_namespace);
for (auto node_c = commodity_list; node_c; node_c = g_list_next (node_c))
Expand Down Expand Up @@ -281,7 +280,6 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
}
g_list_free (commodity_list);
}
g_list_free (namespace_list);

return oldest;
}
Expand Down
10 changes: 3 additions & 7 deletions gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "gnc-uri-utils.h"
#include "gnc-ui-util.h"
#include "dialog-utils.h"
#include "gnc-commodity.hpp"

#include "gnc-component-manager.h"

Expand Down Expand Up @@ -445,9 +446,7 @@ GtkTreeModel *get_model (bool all_commodity)
GtkTreeModel *store, *model;
const gnc_commodity_table *commodity_table = gnc_get_current_commodities ();
gnc_commodity *tmp_commodity = nullptr;
char *tmp_namespace = nullptr;
GList *commodity_list = nullptr;
GList *namespace_list = gnc_commodity_table_get_namespaces (commodity_table);
GtkTreeIter iter;

store = GTK_TREE_MODEL(gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
Expand All @@ -460,10 +459,9 @@ GtkTreeModel *get_model (bool all_commodity)
gtk_list_store_set (GTK_LIST_STORE(store), &iter,
DISPLAYED_COMM, " ", SORT_COMM, " ", COMM_PTR, nullptr, SEP, false, -1);

namespace_list = g_list_first (namespace_list);
while (namespace_list != nullptr)
for (const auto& tmp_namespace_str : gnc_commodity_table_get_namespaces (commodity_table))
{
tmp_namespace = (char*)namespace_list->data;
auto tmp_namespace = tmp_namespace_str.c_str();
DEBUG("Looking at namespace %s", tmp_namespace);

/* Hide the template entry */
Expand Down Expand Up @@ -507,10 +505,8 @@ GtkTreeModel *get_model (bool all_commodity)
}
}
}
namespace_list = g_list_next (namespace_list);
}
g_list_free (commodity_list);
g_list_free (namespace_list);
g_object_unref (store);

return model;
Expand Down
9 changes: 4 additions & 5 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "gnc-ui-util.h"
#include "Account.h"
#include "Transaction.h"
#include "gnc-commodity.hpp"
#include "gnc-pricedb.h"
#include <gnc-exp-parser.h>

Expand Down Expand Up @@ -201,15 +202,13 @@ gnc_commodity* parse_commodity (const std::string& comm_str)
if (!comm)
{
/* If that fails try mnemonic in all other namespaces */
auto namespaces = gnc_commodity_table_get_namespaces(table);
for (auto ns = namespaces; ns; ns = ns->next)
for (const auto& ns_str : gnc_commodity_table_get_namespaces(table))
{
gchar* ns_str = (gchar*)ns->data;
if (g_utf8_collate(ns_str, GNC_COMMODITY_NS_CURRENCY) == 0)
if (ns_str == GNC_COMMODITY_NS_CURRENCY)
continue;

comm = gnc_commodity_table_lookup (table,
ns_str, comm_str.c_str());
ns_str.c_str(), comm_str.c_str());
if (comm)
break;
}
Expand Down
10 changes: 5 additions & 5 deletions gnucash/import-export/import-commodity-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "Account.h"
#include "Transaction.h"
#include "dialog-commodity.h"
#include "gnc-commodity.hpp"
#include "gnc-engine.h"
#include "gnc-ui-util.h"

Expand Down Expand Up @@ -64,11 +65,10 @@ gnc_commodity * gnc_import_select_commodity(const char * cusip,
DEBUG("Looking for commodity with exchange_code: %s", cusip);

g_assert(commodity_table);
GList *namespace_list = gnc_commodity_table_get_namespaces(commodity_table);

for (GList *n = namespace_list; !retval && n; n = g_list_next (n))
for (const auto& ns_str : gnc_commodity_table_get_namespaces(commodity_table))
{
auto ns = static_cast<const char*>(n->data);
auto ns = ns_str.c_str();
DEBUG("Looking at namespace %s", ns);
GList *comm_list = gnc_commodity_table_get_commodities (commodity_table, ns);
for (GList *m = comm_list; !retval && m; m = g_list_next (m))
Expand All @@ -82,10 +82,10 @@ gnc_commodity * gnc_import_select_commodity(const char * cusip,
}
}
g_list_free (comm_list);
if (retval)
break;
}

g_list_free(namespace_list);

if (retval == NULL && ask_on_unknown != 0)
{
const gchar *message =
Expand Down
8 changes: 2 additions & 6 deletions libgnucash/app-utils/gnc-quotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,8 +978,6 @@ CommVec
gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
{
gnc_commodity_namespace * ns = NULL;
const char *name_space;
GList * nslist, * tmp;
CommVec l;
regex_t pattern;
const char *expression = gnc_prefs_get_namespace_regexp ();
Expand All @@ -996,10 +994,9 @@ gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
return CommVec ();
}

nslist = gnc_commodity_table_get_namespaces (table);
for (tmp = nslist; tmp; tmp = tmp->next)
for (const auto& name_space_str : gnc_commodity_table_get_namespaces (table))
{
name_space = static_cast<const char *> (tmp->data);
auto name_space = name_space_str.c_str();
if (regexec (&pattern, name_space, 0, NULL, 0) == 0)
{
// DEBUG ("Running list of %s commodities", name_space);
Expand All @@ -1011,7 +1008,6 @@ gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
}
}
}
g_list_free (nslist);
regfree (&pattern);
}
else
Expand Down
28 changes: 8 additions & 20 deletions libgnucash/backend/xml/io-gncxml-v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <zlib.h>
#include <errno.h>

#include "gnc-commodity.hpp"
#include "gnc-engine.h"
#include "gnc-pricedb-p.h"
#include "Scrub.h"
Expand Down Expand Up @@ -928,14 +929,6 @@ write_counts (FILE* out, ...)
return success;
}

static gint
compare_namespaces (gconstpointer a, gconstpointer b)
{
const gchar* sa = (const gchar*) a;
const gchar* sb = (const gchar*) b;
return (g_strcmp0 (sa, sb));
}

static gint
compare_commodity_ids (gconstpointer a, gconstpointer b)
{
Expand Down Expand Up @@ -1038,25 +1031,20 @@ gboolean
write_commodities (FILE* out, QofBook* book, sixtp_gdv2* gd)
{
gnc_commodity_table* tbl;
GList* namespaces;
GList* lp;
gboolean success = TRUE;

tbl = gnc_commodity_table_get_table (book);

namespaces = gnc_commodity_table_get_namespaces (tbl);
if (namespaces)
{
namespaces = g_list_sort (namespaces, compare_namespaces);
}
auto namespaces = gnc_commodity_table_get_namespaces (tbl);

std::sort (namespaces.begin(), namespaces.end());

for (lp = namespaces; success && lp; lp = lp->next)
for (const auto& name_space : namespaces)
{
GList* comms, *lp2;
xmlNodePtr comnode;

comms = gnc_commodity_table_get_commodities (tbl,
static_cast<const char*> (lp->data));
comms = gnc_commodity_table_get_commodities (tbl, name_space.c_str());
comms = g_list_sort (comms, compare_commodity_ids);

for (lp2 = comms; lp2; lp2 = lp2->next)
Expand All @@ -1079,10 +1067,10 @@ write_commodities (FILE* out, QofBook* book, sixtp_gdv2* gd)
}

g_list_free (comms);
if (!success)
break;
}

if (namespaces) g_list_free (namespaces);

return success;
}

Expand Down
Loading

0 comments on commit 6ff411f

Please sign in to comment.