[account.cpp] refactor gnc_account_lookup_by_name

This commit is contained in:
Christopher Lam 2021-08-03 22:08:11 +08:00
parent bebc366e88
commit 09e2e7613c

View File

@ -2972,37 +2972,17 @@ gnc_account_get_descendants_sorted (const Account *account)
return g_list_reverse (list);
}
static gpointer
is_acct_name (Account *account, gpointer user_data)
{
auto name {static_cast<gchar*>(user_data)};
return (g_strcmp0 (name, xaccAccountGetName (account)) ? nullptr : account);
}
Account *
gnc_account_lookup_by_name (const Account *parent, const char * name)
{
AccountPrivate *cpriv, *ppriv;
Account *child, *result;
GList *node;
g_return_val_if_fail(GNC_IS_ACCOUNT(parent), NULL);
g_return_val_if_fail(name, NULL);
/* first, look for accounts hanging off the current node */
ppriv = GET_PRIVATE(parent);
for (node = ppriv->children; node; node = node->next)
{
child = static_cast<Account*>(node->data);
cpriv = GET_PRIVATE(child);
if (g_strcmp0(cpriv->accountName, name) == 0)
return child;
}
/* if we are still here, then we haven't found the account yet.
* Recursively search each of the child accounts next */
for (node = ppriv->children; node; node = node->next)
{
child = static_cast<Account*>(node->data);
result = gnc_account_lookup_by_name (child, name);
if (result)
return result;
}
return NULL;
return (Account*)gnc_account_foreach_descendant_until (parent, is_acct_name, (char*)name);
}
Account *