From 275f8c844ec8f1f612c9b71f311bfcb2c52bbce4 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sun, 3 Jan 1999 03:19:24 +0000 Subject: [PATCH] more account-info stuff git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@1537 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/AccInfo.c | 39 ++++++++++++++++++++++++++++++++++++++- src/engine/AccInfo.h | 11 +++++++++++ src/engine/Account.c | 32 ++++++++++++++++++++++++-------- src/engine/Account.h | 3 ++- 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/engine/AccInfo.c b/src/engine/AccInfo.c index ae75b76e75..b13f78737c 100644 --- a/src/engine/AccInfo.c +++ b/src/engine/AccInfo.c @@ -23,6 +23,7 @@ #include "AccInfo.h" #include "AccInfoP.h" #include "messages.h" +#include "util.h" /* =========================================================== */ @@ -56,6 +57,38 @@ char * xaccAccountGetTypeStr (int type) /* =========================================================== */ +AccInfo * +xaccMallocAccInfo (int typo) +{ + AccInfo *u = NULL; + if ((STOCK == typo) || (MUTUAL == typo)) { + u = (AccInfo *) xaccMallocInvAcct (); + u->inv_acct.type = typo; + } + return u; +} + +void +xaccFreeAccInfo (AccInfo *u) +{ + if (!u) return; + if ((STOCK == u->type) || (MUTUAL == u->type)) { + xaccFreeInvAcct ( &(u->inv_acct)); + } +} + +InvAcct * +xaccCastToInvAcct (AccInfo *u) +{ + if (!u) return NULL; + if ((STOCK == u->type) || (MUTUAL == u->type)) { + return ( &(u->inv_acct)); + } + return NULL; +} + +/* =========================================================== */ + InvAcct * xaccMallocInvAcct (void) { @@ -69,7 +102,7 @@ void xaccInitInvAcct (InvAcct *iacc) { if (!iacc) return; - iacc->type = -1; + iacc->type = STOCK; iacc->pricesrc = NULL; iacc->brokerid = NULL; iacc->acctid = NULL; @@ -83,6 +116,10 @@ void xaccFreeInvAcct (InvAcct *iacc) { if (!iacc) return; + + /* if the wrong type then a miscast. can't free. */ + assert ((STOCK == iacc->type) || (MUTUAL == iacc->type)); + if (iacc->pricesrc) { free(iacc->pricesrc); iacc->pricesrc = NULL; } if (iacc->brokerid) { free(iacc->brokerid); iacc->brokerid = NULL; } if (iacc->acctid) { free(iacc->acctid); iacc->acctid = NULL; } diff --git a/src/engine/AccInfo.h b/src/engine/AccInfo.h index 4cba4029db..da9efe2a26 100644 --- a/src/engine/AccInfo.h +++ b/src/engine/AccInfo.h @@ -98,6 +98,17 @@ typedef struct _BankAcct BankAcct; typedef struct _InvAcct InvAcct; typedef union _AccInfo AccInfo; + +/* The AccInfo structure is just a union of the other account + * auxilliary info types. The xaccCastToXXX() functions simply + * provide a safe upcast mechanism (similar to that in C++ ... + * returns the address if the cast is safe, otherwise returns NULL). + */ +AccInfo * xaccMallocAccInfo (int typo); +void xaccFreeAccInfo (AccInfo *u); +InvAcct * xaccCastToInvAcct (AccInfo *); + + InvAcct * xaccMallocInvAcct (void); void xaccInitInvAcct (InvAcct *iacc); void xaccFreeInvAcct (InvAcct *iacc); diff --git a/src/engine/Account.c b/src/engine/Account.c index 1bd3f64be4..8303407ff4 100644 --- a/src/engine/Account.c +++ b/src/engine/Account.c @@ -124,16 +124,10 @@ xaccFreeAccount( Account *acc ) if (NULL == acc) return; - /* recursively free children */ + /* First, recursively free children */ xaccFreeAccountGroup (acc->children); - - if (acc->accountName) free (acc->accountName); - if (acc->accountCode) free (acc->accountCode); - if (acc->description) free (acc->description); - if (acc->notes) free (acc->notes); - if (acc->currency) free (acc->currency); - if (acc->security) free (acc->security); + /* Next, clean up the splits */ /* any split pointing at this account needs to be unmarked */ for (i=0; inumSplits; i++) { s = acc->splits[i]; @@ -158,6 +152,17 @@ xaccFreeAccount( Account *acc ) acc->splits = NULL; acc->numSplits = 0; + /* Finally, clean up the account info */ + if (acc->accInfo) xaccFreeAccInfo (acc->accInfo); + acc->accInfo = NULL; + + if (acc->accountName) free (acc->accountName); + if (acc->accountCode) free (acc->accountCode); + if (acc->description) free (acc->description); + if (acc->notes) free (acc->notes); + if (acc->currency) free (acc->currency); + if (acc->security) free (acc->security); + /* zero out values, just in case stray * pointers are pointing here. */ @@ -840,6 +845,10 @@ xaccAccountSetType (Account *acc, int tip) /* refuse invalid account types */ if (NUM_ACCOUNT_TYPES <= tip) return; acc->type = tip; + + /* initialize the auxilliary account info as well */ + if (acc->accInfo) xaccFreeAccInfo (acc->accInfo); + acc->accInfo = xaccMallocAccountInfo (tip); } void @@ -945,6 +954,13 @@ xaccAccountSetSecurity (Account *acc, char *str) /********************************************************************\ \********************************************************************/ +AccInfo * +xaccAccountGetAccInfo (Account *acc) +{ + if (!acc) return NULL; + return (acc->accInfo); +} + AccountGroup * xaccAccountGetChildren (Account *acc) { diff --git a/src/engine/Account.h b/src/engine/Account.h index abcb4a1d90..99ea84273e 100644 --- a/src/engine/Account.h +++ b/src/engine/Account.h @@ -1,7 +1,7 @@ /********************************************************************\ * Account.h -- the Account data structure * * Copyright (C) 1997 Robin D. Clark * - * Copyright (C) 1997, 1998 Linas Vepstas * + * Copyright (C) 1997, 1998, 1999 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 * @@ -139,6 +139,7 @@ char * xaccAccountGetCurrency (Account *); char * xaccAccountGetSecurity (Account *); AccountGroup * xaccAccountGetChildren (Account *); AccountGroup * xaccAccountGetParent (Account *); +AccInfo * xaccAccountGetAccInfo (Account *); double xaccAccountGetBalance (Account *); double xaccAccountGetClearedBalance (Account *);