From cf0fd7af1f11635724a0b6bb3e8a74786cc6b097 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sun, 18 Oct 1998 17:55:35 +0000 Subject: [PATCH] add sorted-order accounts git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1319 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/Group.c | 54 ++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/engine/Group.c b/src/engine/Group.c index ea46abc5db..bd9a63d666 100644 --- a/src/engine/Group.c +++ b/src/engine/Group.c @@ -1,7 +1,7 @@ /********************************************************************\ * Group.c -- the main data structure of the program * * Copyright (C) 1997 Robin D. Clark * - * Copyright (C) 1997 Linas Vepstas * + * Copyright (C) 1997, 1998 Linas Vepstas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * @@ -375,38 +375,56 @@ xaccInsertSubAccount( Account *adult, Account *child ) void xaccGroupInsertAccount( AccountGroup *grp, Account *acc ) - { - int i=-1; - Account **arr; +{ + int i,j,nacc; + Account **oldarr, **newarr; + int ralo = 1; if (NULL == grp) return; if (NULL == acc) return; /* If the account is currently in another group, remove it there first. - * Basically, we can't have accounts being in two places at once. */ + * Basically, we can't have accounts being in two places at once. + * If old and new parents are the same, reinsertion causes the sort order + * to be checked. + */ if (acc->parent) { + if (grp == acc->parent) ralo = 0; xaccRemoveAccount (acc); } + grp->saved = FALSE; /* set back-pointer to the accounts parent */ acc->parent = grp; - arr = grp->account; - - grp->saved = FALSE; - - grp->account = (Account **)_malloc(((grp->numAcc)+2)*sizeof(Account *)); - - for( i=0; i<(grp->numAcc); i++ ) { - grp->account[i] = arr[i]; + nacc = grp->numAcc; + oldarr = grp->account; + newarr = oldarr; + if (ralo) { + newarr = (Account **) realloc (oldarr, (nacc+2)*sizeof(Account *)); + if (newarr == oldarr) ralo = 0; } - _free(arr); - - grp->account[(grp->numAcc)] = acc; - grp->numAcc++; - grp->account[(grp->numAcc)] = NULL; + /* insert account in proper sort order */ + for( i=nacc-1, j=nacc; i>=0; i--, j-- ) { + if (i != j) { + if (0 < xaccAccountOrder (&(oldarr[i]), &acc)) { + newarr[j] = oldarr[i]; + } else { + newarr[j] = acc; + j--; + } + } else { + newarr[j] = oldarr[i]; + } } + if (ralo) free (oldarr); + + nacc++; + newarr[nacc] = NULL; + grp->account = newarr; + grp->numAcc = nacc; +} /********************************************************************\ \********************************************************************/