r23173 - gnucash/trunk/src/engine - Modify xaccAccountChildrenEqual to not depend upon the order

John Ralls jralls at code.gnucash.org
Tue Sep 17 19:25:46 EDT 2013


Author: jralls
Date: 2013-09-17 19:25:43 -0400 (Tue, 17 Sep 2013)
New Revision: 23173
Trac: http://svn.gnucash.org/trac/changeset/23173

Modified:
   gnucash/trunk/src/engine/Account.c
Log:
Modify xaccAccountChildrenEqual to not depend upon the order

A SQL backend can't be expected to always return children in the same order.

Modified: gnucash/trunk/src/engine/Account.c
===================================================================
--- gnucash/trunk/src/engine/Account.c	2013-09-17 23:25:11 UTC (rev 23172)
+++ gnucash/trunk/src/engine/Account.c	2013-09-17 23:25:43 UTC (rev 23173)
@@ -1248,6 +1248,20 @@
 
 /********************************************************************\
 \********************************************************************/
+static gint
+compare_account_by_name (gconstpointer a, gconstpointer b)
+{
+    AccountPrivate *priv_a, *priv_b;
+    if (a && !b) return 1;
+    if (b && !a) return -1;
+    if (!a && !b) return 0;
+    priv_a = GET_PRIVATE((Account*)a);
+    priv_b = GET_PRIVATE((Account*)b);
+    if ((priv_a->accountCode && strlen (priv_a->accountCode)) ||
+	(priv_b->accountCode && strlen (priv_b->accountCode)))
+	return g_strcmp0 (priv_a->accountCode, priv_b->accountCode);
+    return g_strcmp0 (priv_a->accountName, priv_b->accountName);
+}
 
 static gboolean
 xaccAcctChildrenEqual(const GList *na,
@@ -1256,15 +1270,28 @@
 {
     if ((!na && nb) || (na && !nb))
     {
-        PWARN ("only one has accounts");
+        PINFO ("only one has accounts");
         return(FALSE);
     }
+    if (g_list_length ((GList*)na) != g_list_length ((GList*)nb))
+    {
+	PINFO ("Accounts have different numbers of children");
+	return (FALSE);
+    }
 
-    while (na && nb)
+    while (na)
     {
         Account *aa = na->data;
-        Account *ab = nb->data;
+        Account *ab;
+	GList *node = g_list_find_custom ((GList*)nb, aa,
+					  (GCompareFunc)compare_account_by_name);
 
+	if (!node)
+	{
+	    PINFO ("Unable to find matching child account.");
+	    return FALSE;
+	}
+	ab = node->data;
         if (!xaccAccountEqual(aa, ab, check_guids))
         {
             char sa[GUID_ENCODING_LENGTH + 1];
@@ -1279,15 +1306,8 @@
         }
 
         na = na->next;
-        nb = nb->next;
     }
 
-    if (na || nb)
-    {
-        PWARN ("different numbers of accounts");
-        return(FALSE);
-    }
-
     return(TRUE);
 }
 



More information about the gnucash-changes mailing list