Skip to content

Commit

Permalink
gnc_commodity_table_get_commodities returns std::vector<gnc_commodity*>
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Apr 30, 2024
1 parent b54a1a8 commit 778bdac
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 141 deletions.
1 change: 0 additions & 1 deletion bindings/engine-common.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ static const GncGUID * gncAccountGetGUID(Account *x)
%typemap(newfree) TransList * "g_list_free($1);"
%typemap(newfree) PriceList * "g_list_free($1);"
%typemap(newfree) LotList * "g_list_free($1);"
%typemap(newfree) CommodityList * "g_list_free($1);"

%typemap(freearg) AccountList * "g_list_free($1);"

Expand Down
2 changes: 1 addition & 1 deletion bindings/engine.i
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ GLIST_HELPER_INOUT(LotList, SWIGTYPE_p_GNCLot);
GLIST_HELPER_INOUT(AccountList, SWIGTYPE_p_Account);
GLIST_HELPER_INOUT(PriceList, SWIGTYPE_p_GNCPrice);
// TODO: free PriceList?
GLIST_HELPER_INOUT(CommodityList, SWIGTYPE_p_gnc_commodity);
VECTOR_HELPER_INOUT(CommVec, SWIGTYPE_p_gnc_commodity, gnc_commodity);
VECTOR_HELPER_INOUT(SplitsVec, SWIGTYPE_p_Split, Split);
VECTOR_HELPER_INOUT(AccountVec, SWIGTYPE_p_Account, Account);

Expand Down
2 changes: 1 addition & 1 deletion common/base-typemaps.i
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ typedef char gchar;
}
}

%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList *,
%typemap(out) GList *, SplitList *, AccountList *, LotList *,
MonetaryList *, PriceList *, EntryList * {
gpointer data;
GList *l;
Expand Down
9 changes: 2 additions & 7 deletions gnucash/gnome-utils/dialog-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ gnc_ui_update_commodity_picker (GtkWidget *cbwe,
const gchar * name_space,
const gchar * init_string)
{
GList * commodities;
GList * iterator = nullptr;
GList * commodity_items = nullptr;
GtkComboBox *combo_box;
Expand All @@ -471,14 +470,10 @@ gnc_ui_update_commodity_picker (GtkWidget *cbwe,
gtk_combo_box_set_active(combo_box, -1);

table = gnc_commodity_table_get_table (gnc_get_current_book ());
commodities = gnc_commodity_table_get_commodities(table, name_space);
for (iterator = commodities; iterator; iterator = iterator->next)
for (auto comm : gnc_commodity_table_get_commodities(table, name_space))
{
commodity_items =
g_list_prepend (commodity_items,
(gpointer) gnc_commodity_get_printname(GNC_COMMODITY(iterator->data)));
commodity_items = g_list_prepend (commodity_items, (gpointer) gnc_commodity_get_printname(comm));
}
g_list_free(commodities);

commodity_items = g_list_sort(commodity_items, collate);
for (iterator = commodity_items; iterator; iterator = iterator->next)
Expand Down
11 changes: 6 additions & 5 deletions gnucash/gnome-utils/gnc-currency-edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

#include <config.h>

#include <algorithm>

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
Expand All @@ -56,6 +58,7 @@

#include "gnc-currency-edit.h"
#include "gnc-commodity.h"
#include "gnc-commodity.hpp"
#include "gnc-gtk-utils.h"
#include "gnc-ui-util.h"
#include "gnc-engine.h"
Expand Down Expand Up @@ -311,12 +314,10 @@ add_item(gnc_commodity *commodity, GNCCurrencyEdit *gce)
static void
fill_currencies(GNCCurrencyEdit *gce)
{
GList *currencies;

currencies = gnc_commodity_table_get_commodities
auto currencies = gnc_commodity_table_get_commodities
(gnc_get_current_commodities (), GNC_COMMODITY_NS_CURRENCY);
g_list_foreach(currencies, (GFunc)add_item, gce);
g_list_free(currencies);
std::for_each (currencies.begin(), currencies.end(),
[gce](auto comm){ add_item (comm, gce); });
}


Expand Down
5 changes: 1 addition & 4 deletions gnucash/gnome/dialog-price-edit-db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,8 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
{
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))
for (auto tmp_commodity : gnc_commodity_table_get_commodities (commodity_table, tmp_namespace))
{
auto tmp_commodity = static_cast<gnc_commodity*>(node_c->data);
auto num = gnc_pricedb_num_prices (pdb, tmp_commodity);
DEBUG("Looking at commodity %s, Number of prices %d", gnc_commodity_get_fullname (tmp_commodity), num);

Expand Down Expand Up @@ -278,7 +276,6 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
g_list_free_full (list, (GDestroyNotify)gnc_price_unref);
}
}
g_list_free (commodity_list);
}

return oldest;
Expand Down
10 changes: 1 addition & 9 deletions gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ GtkTreeModel *get_model (bool all_commodity)
{
GtkTreeModel *store, *model;
const gnc_commodity_table *commodity_table = gnc_get_current_commodities ();
gnc_commodity *tmp_commodity = nullptr;
GList *commodity_list = nullptr;
GtkTreeIter iter;

store = GTK_TREE_MODEL(gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
Expand All @@ -469,9 +467,6 @@ GtkTreeModel *get_model (bool all_commodity)
{
if ((g_utf8_collate (tmp_namespace, GNC_COMMODITY_NS_CURRENCY ) == 0) || (all_commodity == true))
{
commodity_list = gnc_commodity_table_get_commodities (commodity_table, tmp_namespace);
commodity_list = g_list_first (commodity_list);

// if this is the CURRENCY, add a row to be identified as a separator row
if ((g_utf8_collate (tmp_namespace, GNC_COMMODITY_NS_CURRENCY) == 0) && (all_commodity == true))
{
Expand All @@ -480,11 +475,10 @@ GtkTreeModel *get_model (bool all_commodity)
SORT_COMM, "CURRENCY-", COMM_PTR, nullptr, SEP, true, -1);
}

while (commodity_list != nullptr)
for (auto tmp_commodity : gnc_commodity_table_get_commodities (commodity_table, tmp_namespace))
{
const gchar *name_str;
gchar *sort_str;
tmp_commodity = (gnc_commodity*)commodity_list->data;
DEBUG("Looking at commodity %s", gnc_commodity_get_fullname (tmp_commodity));

name_str = gnc_commodity_get_printname (tmp_commodity);
Expand All @@ -501,12 +495,10 @@ GtkTreeModel *get_model (bool all_commodity)
SORT_COMM, sort_str, COMM_PTR, tmp_commodity, SEP, false, -1);

g_free (sort_str);
commodity_list = g_list_next (commodity_list);
}
}
}
}
g_list_free (commodity_list);
g_object_unref (store);

return model;
Expand Down
6 changes: 2 additions & 4 deletions gnucash/import-export/import-commodity-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,16 @@ gnc_commodity * gnc_import_select_commodity(const char * cusip,
{
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))
for (auto com : gnc_commodity_table_get_commodities (commodity_table, ns))
{
auto com = static_cast<gnc_commodity*>(m->data);
DEBUG("Looking at commodity %s", gnc_commodity_get_fullname (com));
if (!g_strcmp0 (gnc_commodity_get_cusip (com), cusip))
{
retval = com;
DEBUG("Commodity %s matches.", gnc_commodity_get_fullname (com));
break;
}
}
g_list_free (comm_list);
if (retval)
break;
}
Expand Down
18 changes: 6 additions & 12 deletions libgnucash/backend/xml/io-gncxml-v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,12 +930,9 @@ write_counts (FILE* out, ...)
}

static gint
compare_commodity_ids (gconstpointer a, gconstpointer b)
compare_commodity_less (gnc_commodity* a, gnc_commodity* b)
{
const gnc_commodity* ca = (const gnc_commodity*) a;
const gnc_commodity* cb = (const gnc_commodity*) b;
return (g_strcmp0 (gnc_commodity_get_mnemonic (ca),
gnc_commodity_get_mnemonic (cb)));
return (g_strcmp0 (gnc_commodity_get_mnemonic (a), gnc_commodity_get_mnemonic (b)) < 0);
}

static gboolean write_pricedb (FILE* out, QofBook* book, sixtp_gdv2* gd);
Expand Down Expand Up @@ -1041,16 +1038,14 @@ write_commodities (FILE* out, QofBook* book, sixtp_gdv2* gd)

for (const auto& name_space : namespaces)
{
GList* comms, *lp2;
xmlNodePtr comnode;

comms = gnc_commodity_table_get_commodities (tbl, name_space.c_str());
comms = g_list_sort (comms, compare_commodity_ids);
auto comms = gnc_commodity_table_get_commodities (tbl, name_space.c_str());
std::sort (comms.begin(), comms.end(), compare_commodity_less);

for (lp2 = comms; lp2; lp2 = lp2->next)
for (auto comm : comms)
{
comnode = gnc_commodity_dom_tree_create (static_cast<const gnc_commodity*>
(lp2->data));
comnode = gnc_commodity_dom_tree_create (comm);
if (comnode == NULL)
continue;

Expand All @@ -1066,7 +1061,6 @@ write_commodities (FILE* out, QofBook* book, sixtp_gdv2* gd)
sixtp_run_callback (gd, "commodities");
}

g_list_free (comms);
if (!success)
break;
}
Expand Down
76 changes: 34 additions & 42 deletions libgnucash/engine/gnc-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1739,29 +1739,15 @@ gnc_commodity_table_find_full(const gnc_commodity_table * table,
const char * name_space,
const char * fullname)
{
gnc_commodity * retval = nullptr;
GList * all;
GList * iterator;

if (!fullname || (fullname[0] == '\0'))
return nullptr;

all = gnc_commodity_table_get_commodities(table, name_space);

for (iterator = all; iterator; iterator = iterator->next)
{
auto commodity = GNC_COMMODITY (iterator->data);
if (!strcmp(fullname,
gnc_commodity_get_printname(commodity)))
{
retval = commodity;
break;
}
}

g_list_free (all);
auto commodities{gnc_commodity_table_get_commodities(table, name_space)};
auto it = std::find_if (commodities.begin(), commodities.end(),
[fullname](auto comm)
{ return !g_strcmp0 (fullname, gnc_commodity_get_printname(comm)); });

return retval;
return it == commodities.end() ? nullptr : *it;;
}


Expand Down Expand Up @@ -1978,10 +1964,10 @@ gnc_commodity_is_currency(const gnc_commodity *cm)
* list commodities in a given namespace
********************************************************************/

static CommodityList*
static CommVec
commodity_table_get_all_noncurrency_commodities(const gnc_commodity_table* table)
{
CommodityList *retval = NULL;
CommVec retval;
for (const auto& name_space : gnc_commodity_table_get_namespaces(table))
{
gnc_commodity_namespace *ns = NULL;
Expand All @@ -1990,26 +1976,33 @@ commodity_table_get_all_noncurrency_commodities(const gnc_commodity_table* table
ns = gnc_commodity_table_find_namespace(table, name_space.c_str());
if (!ns)
continue;
retval = g_list_concat(g_hash_table_values(ns->cm_table), retval);
auto val = g_hash_table_values(ns->cm_table);
for (auto n = val; n; n = n->next)
retval.push_back (GNC_COMMODITY(n->data));
g_list_free (val);
}
return retval;
}

CommodityList *
CommVec
gnc_commodity_table_get_commodities(const gnc_commodity_table * table,
const char * name_space)
{
gnc_commodity_namespace * ns = nullptr;
CommVec retval;

if (!table)
return nullptr;
return retval;
if (g_strcmp0(name_space, GNC_COMMODITY_NS_NONISO_GUI) == 0)
return commodity_table_get_all_noncurrency_commodities(table);
ns = gnc_commodity_table_find_namespace(table, name_space);
auto ns = gnc_commodity_table_find_namespace(table, name_space);
if (!ns)
return nullptr;
return retval;

return g_hash_table_values(ns->cm_table);
auto val = g_hash_table_values(ns->cm_table);
for (auto n = val; n; n = n->next)
retval.push_back (GNC_COMMODITY(n->data));
g_list_free (val);
return retval;
}

/********************************************************************
Expand All @@ -2020,45 +2013,45 @@ gnc_commodity_table_get_commodities(const gnc_commodity_table * table,
static void
get_quotables_helper1(gpointer key, gpointer value, gpointer data)
{
auto comm = GNC_COMMODITY(value);
auto comm{GNC_COMMODITY(value)};
auto rv{static_cast<CommVec*>(data)};
gnc_commodityPrivate* priv = GET_PRIVATE(comm);
auto l = static_cast<GList**>(data);

if (!priv->quote_flag || !priv->quote_source || !priv->quote_source->get_supported())
return;
*l = g_list_prepend(*l, value);
rv->push_back (comm);
}

static gboolean
get_quotables_helper2 (gnc_commodity *comm, gpointer data)
{
auto l = static_cast<GList**>(data);
gnc_commodityPrivate* priv = GET_PRIVATE(comm);
auto rv{static_cast<CommVec*>(data)};

if (!priv->quote_flag || priv->quote_source || !priv->quote_source->get_supported())
return TRUE;
*l = g_list_prepend(*l, comm);
rv->push_back (comm);
return TRUE;
}

CommodityList *
CommVec
gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * table)
{
gnc_commodity_namespace * ns = nullptr;
GList * l = nullptr;
CommVec rv;
regex_t pattern;
const char *expression = gnc_prefs_get_namespace_regexp();

ENTER("table=%p, expression=%s", table, expression);
if (!table)
return nullptr;
return {};

if (expression && *expression)
{
if (regcomp(&pattern, expression, REG_EXTENDED | REG_ICASE) != 0)
{
LEAVE("Cannot compile regex");
return nullptr;
return {};
}

for (const auto& name_space_str : gnc_commodity_table_get_namespaces(table))
Expand All @@ -2070,19 +2063,18 @@ gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * table)
ns = gnc_commodity_table_find_namespace(table, name_space);
if (ns)
{
g_hash_table_foreach(ns->cm_table, &get_quotables_helper1, (gpointer) &l);
g_hash_table_foreach(ns->cm_table, get_quotables_helper1, &rv);
}
}
}
regfree(&pattern);
}
else
{
gnc_commodity_table_foreach_commodity(table, get_quotables_helper2,
(gpointer) &l);
gnc_commodity_table_foreach_commodity(table, get_quotables_helper2, &rv);
}
LEAVE("list head %p", l);
return l;
LEAVE("list head %p", &rv);
return rv;
}

/********************************************************************
Expand Down
Loading

0 comments on commit 778bdac

Please sign in to comment.