Skip to content

Commit

Permalink
[gnucash/*.cpp] use c++ algorithms with commodities
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed May 31, 2024
1 parent 46c7dca commit e951695
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
29 changes: 8 additions & 21 deletions gnucash/gnome-utils/dialog-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,31 +429,20 @@ gnc_ui_select_commodity_namespace_changed_cb (GtkComboBox *cbwe,
/********************************************************************
* gnc_ui_update_commodity_picker
********************************************************************/
static int
collate(gconstpointer a, gconstpointer b)
{
if (!a)
return -1;
if (!b)
return 1;
return g_utf8_collate (static_cast<const char*>(a), static_cast<const char*>(b));
}


void
gnc_ui_update_commodity_picker (GtkWidget *cbwe,
const gchar * name_space,
const gchar * init_string)
{
GList * iterator = nullptr;
GList * commodity_items = nullptr;
std::vector<std::string> commodity_items;
GtkComboBox *combo_box;
GtkEntry *entry;
GtkTreeModel *model;
GtkTreeIter iter;
gnc_commodity_table *table;
gint current = 0, match = 0;
gchar *name;

g_return_if_fail(GTK_IS_COMBO_BOX(cbwe));
g_return_if_fail(name_space);
Expand All @@ -470,25 +459,23 @@ 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 ());

for (auto comm : gnc_commodity_table_get_commodities(table, name_space))
{
commodity_items = g_list_prepend (commodity_items, (gpointer) gnc_commodity_get_printname(comm));
}
commodity_items.push_back (gnc_commodity_get_printname(comm));

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

commodity_items = g_list_sort(commodity_items, collate);
for (iterator = commodity_items; iterator; iterator = iterator->next)
for (auto name : commodity_items)
{
name = (char *)iterator->data;
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, name, -1);
gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, name.c_str(), -1);

if (init_string && g_utf8_collate(name, init_string) == 0)
if (init_string && name == init_string)
match = current;
current++;
}

gtk_combo_box_set_active(combo_box, match);
g_list_free(commodity_items);
}


Expand Down
19 changes: 9 additions & 10 deletions gnucash/import-export/import-commodity-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@author Copyright (C) 2002 Benoit Grégoire <[email protected]>
*/
#include <config.h>
#include <algorithm>

#include <gtk/gtk.h>
#include <glib/gi18n.h>
Expand Down Expand Up @@ -70,18 +71,16 @@ gnc_commodity * gnc_import_select_commodity(const char * cusip,
{
auto ns = ns_str.c_str();
DEBUG("Looking at namespace %s", ns);
for (auto com : gnc_commodity_table_get_commodities (commodity_table, ns))
auto commodities{gnc_commodity_table_get_commodities (commodity_table, ns)};
auto it = std::find_if (commodities.begin(), commodities.end(),
[cusip](auto com)
{ return !g_strcmp0 (gnc_commodity_get_cusip (com), cusip); });
if (it != commodities.end())
{
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;
}
}
if (retval)
retval = *it;
DEBUG("Commodity %s matches.", gnc_commodity_get_fullname (*it));
break;
};
}

if (retval == NULL && ask_on_unknown != 0)
Expand Down

0 comments on commit e951695

Please sign in to comment.