fix a few core dumps

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1323 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 1998-10-19 02:33:33 +00:00
parent 2b174a1f1a
commit ca495ef8b4
2 changed files with 34 additions and 38 deletions

View File

@ -305,6 +305,9 @@ xaccReadAccountGroup( char *datafile )
*/ */
xaccAccountGroupMarkSaved (grp); xaccAccountGroupMarkSaved (grp);
/* auto-number the accounts, if they are not already numbered */
xaccGroupDepthAutoCode (grp);
/* the number of unclaimed accounts should be zero if the /* the number of unclaimed accounts should be zero if the
* read succeeded. But just in case of a very unlikely * read succeeded. But just in case of a very unlikely
* error, try to continue anyway. */ * error, try to continue anyway. */

View File

@ -23,6 +23,8 @@
* Huntington Beach, CA 92648-4632 * * Huntington Beach, CA 92648-4632 *
\********************************************************************/ \********************************************************************/
#include <assert.h>
#include "config.h" #include "config.h"
#include "Account.h" #include "Account.h"
@ -311,7 +313,7 @@ xaccRemoveGroup (AccountGroup *grp)
void void
xaccRemoveAccount (Account *acc) xaccRemoveAccount (Account *acc)
{ {
int i,j; int i,j, nacc;
AccountGroup *grp; AccountGroup *grp;
Account **arr; Account **arr;
@ -323,31 +325,27 @@ xaccRemoveAccount (Account *acc)
* are not yet parented. */ * are not yet parented. */
if (NULL == grp) return; if (NULL == grp) return;
nacc = grp->numAcc;
assert (nacc);
arr = grp->account; arr = grp->account;
grp->saved = FALSE; for( i=0,j=0; j<nacc; i++,j++ ) {
grp->numAcc--;
if (0 < grp->numAcc) {
for( i=0,j=0; i<grp->numAcc; i++,j++ ) {
arr[i] = arr[j]; arr[i] = arr[j];
if( acc == arr[j] ) { i--; } if( acc == arr[j] ) { i--; }
} }
arr[grp->numAcc] = NULL; nacc --;
} else { arr[nacc] = NULL;
arr[grp->numAcc] = NULL; grp->numAcc = nacc;
grp->account = NULL; grp->saved = FALSE;
/* if this was the last account in a group, delete /* if this was the last account in a group, delete
* the group as well (unless its a root group) */ * the group as well (unless its a root group) */
if (grp->parent) { if ((0 == nacc) && (grp->parent)) {
xaccRemoveGroup (grp); xaccRemoveGroup (grp);
xaccFreeAccountGroup (grp); xaccFreeAccountGroup (grp);
} }
} }
}
/********************************************************************\ /********************************************************************\
\********************************************************************/ \********************************************************************/
@ -377,8 +375,8 @@ xaccInsertSubAccount( Account *adult, Account *child )
void void
xaccGroupInsertAccount( AccountGroup *grp, Account *acc ) xaccGroupInsertAccount( AccountGroup *grp, Account *acc )
{ {
int i,j,nacc; int i,nacc;
Account **oldarr, **newarr; Account **arr;
int ralo = 1; int ralo = 1;
if (NULL == grp) return; if (NULL == grp) return;
@ -399,31 +397,24 @@ xaccGroupInsertAccount( AccountGroup *grp, Account *acc )
acc->parent = grp; acc->parent = grp;
nacc = grp->numAcc; nacc = grp->numAcc;
oldarr = grp->account; arr = grp->account;
newarr = oldarr;
if (ralo) { if (ralo) {
newarr = (Account **) realloc (oldarr, (nacc+2)*sizeof(Account *)); arr = (Account **) realloc (arr, (nacc+2)*sizeof(Account *));
if (newarr == oldarr) ralo = 0;
} }
/* insert account in proper sort order */ /* insert account in proper sort order */
for( i=nacc-1, j=nacc; i>=0; i--, j-- ) { for (i=nacc; i>=0; i--) {
if (i != j) { if ((0<i) && (0 < xaccAccountOrder (&(arr[i]), &acc))) {
if (0 < xaccAccountOrder (&(oldarr[i]), &acc)) { arr[i] = arr[i-1];
newarr[j] = oldarr[i];
} else { } else {
newarr[j] = acc; arr[i] = acc;
j--; break;
}
} else {
newarr[j] = oldarr[i];
} }
} }
if (ralo) free (oldarr);
nacc++; nacc++;
newarr[nacc] = NULL; arr[nacc] = NULL;
grp->account = newarr; grp->account = arr;
grp->numAcc = nacc; grp->numAcc = nacc;
} }
@ -472,6 +463,7 @@ void
xaccGroupDepthAutoCode (AccountGroup *grp) xaccGroupDepthAutoCode (AccountGroup *grp)
{ {
int depth; int depth;
if (!grp) return;
/* get the depth */ /* get the depth */
depth = xaccGroupGetDepth (grp); depth = xaccGroupGetDepth (grp);
@ -484,6 +476,7 @@ void
xaccGroupAutoCode (AccountGroup *grp, int depth) xaccGroupAutoCode (AccountGroup *grp, int depth)
{ {
int i; int i;
if (!grp || (0>depth)) return;
for (i=0; i<grp->numAcc; i++) { for (i=0; i<grp->numAcc; i++) {
Account *acc = grp->account[i]; Account *acc = grp->account[i];