diff --git a/src/engine/Account.c b/src/engine/Account.c index 6bbbe7f660e..effcaced202 100644 --- a/src/engine/Account.c +++ b/src/engine/Account.c @@ -563,6 +563,70 @@ xaccCheckTransDateOrder (Transaction *trans ) /********************************************************************\ \********************************************************************/ +/* The sort order is used to implicitly define an + * order for report generation */ + +static int typeorder[NUM_ACCOUNT_TYPES] = { + BANK, STOCK, MUTUAL, CURRENCY, CASH, ASSET, + CREDIT, LIABILITY, INCOME, EXPENSE, EQUITY }; + +static int revorder[NUM_ACCOUNT_TYPES] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; + + +int +xaccAccountOrder (Account **aa, Account **ab) +{ + char *da, *db; + int ta, tb; + + if ( (*aa) && !(*ab) ) return -1; + if ( !(*aa) && (*ab) ) return +1; + if ( !(*aa) && !(*ab) ) return 0; + + /* sort on accountCode strings */ + da = (*aa)->accountCode; + db = (*ab)->accountCode; + SAFE_STRCMP (da, db); + + /* if acccount-type-order array not initialized, initialize it */ + /* this will happen at most once during program invocation */ + if (-1 == revorder[0]) { + int i; + for (i=0; itype; + tb = (*ab)->type; + ta = revorder[ta]; + tb = revorder[tb]; + if (ta < tb) return -1; + if (ta > tb) return +1; + + /* otherwise, sort on accountName strings */ + da = (*aa)->accountName; + db = (*ab)->accountName; + SAFE_STRCMP (da, db); + + /* accountName strings should really, really be unique, and so in theory + * we should never ever get here. But just in case theory is broke ... */ + da = (*aa)->currency; + db = (*ab)->currency; + SAFE_STRCMP (da, db); + + da = (*aa)->security; + db = (*ab)->security; + SAFE_STRCMP (da, db); + + return 0; +} + +/********************************************************************\ +\********************************************************************/ + int xaccIsAccountInList (Account * acc, Account **list) { diff --git a/src/engine/Account.h b/src/engine/Account.h index e8f12e87982..0c09bc50f8b 100644 --- a/src/engine/Account.h +++ b/src/engine/Account.h @@ -72,10 +72,21 @@ int xaccCheckDateOrder (Account *, Split *); int xaccCheckTransDateOrder (Transaction *); /* The xaccIsAccountInList() subroutine returns the number of times - * that an account appears in the account list. */ + * that an account appears in the account list. + */ int xaccIsAccountInList (Account * acc, Account **list); void xaccZeroRunningBalances (Account **list); +/* The xaccAccountOrder() subroutine defines a sorting order + * on accounts. It takes pointers to two accounts, and + * returns -1 if the first account is "less than" the second, + * returns +1 if the first is "greater than" the second, and + * 0 if they are equal. To determine the sort order, first + * the account codes are compared, and if these are equal, then + * account types, and, if these are queal, the account names. + */ +int xaccAccountOrder (Account**, Account **); + /* The xaccConsolidateTransactions() subroutine scans through * all of the transactions in an account, and compares them. * If any of them are exact duplicates, the duplicates are removed.