Skip to content

Commit

Permalink
James LewisMoss's refactoring of xml writing.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3579 57a11ea4-9604-0410-9ed3-97b8803252fd
  • Loading branch information
jdavisp3 committed Feb 3, 2001
1 parent 06b7c43 commit 5f5661e
Show file tree
Hide file tree
Showing 13 changed files with 1,144 additions and 974 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Juan Manuel Garc
Christopher Molnar <molnarc@mandrakesoft.com> build system patch
Tim Mooney <mooney@dogbert.cc.ndsu.NoDak.edu> port to alpha-dec-osf4.0f
G. Allen Morris III <gam3@ann.softgams.com> for QIF core dump
James LewisMoss <dres@phoenixdsl.com> design doc patches
James LewisMoss <dres@debian.org> design doc patches
Steven Murdoch <sjmurdoch@linuxfan.com> gnc-prices fix for London exchange
Brent Neal <brent@baton.phys.lsu.edu> TIAA-CREF support.
Stefan Nobis <stefan-ml@snobis.de> German translation patch
Expand Down
1 change: 0 additions & 1 deletion debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ binary-arch: build install
# cp src/.libs/gnucash $(id)/usr/bin/gnucash.debug
dh_compress
dh_fixperms
dh_suidregister
dh_installdeb
dh_shlibdeps
dh_gencontrol
Expand Down
94 changes: 94 additions & 0 deletions src/engine/Account-xml-parser-v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#include "sixtp.h"
#include "sixtp-utils.h"
#include "sixtp-parsers.h"
#include "sixtp-xml-write-utils.h"

#include "Account.h"
#include "AccountP.h"
#include "Group.h"

#include "sixtp-writers.h"

/****************************************************************************/
/* <account> (parent <ledger-data>)
Expand Down Expand Up @@ -552,3 +555,94 @@ gnc_account_parser_new(void)

return ret;
}


/***********************************************************************/
/***********************************************************************/
/* write out the xml for each of the fields in an account */

static gboolean
xml_add_account_restorer(xmlNodePtr p, Account* a) {
xmlNodePtr acct_xml;

g_return_val_if_fail(p, FALSE);
g_return_val_if_fail(a, FALSE);

acct_xml = xmlNewTextChild(p, NULL, "account", NULL);
g_return_val_if_fail(acct_xml, FALSE);

acct_xml = xmlNewTextChild(acct_xml, NULL, "restore", NULL);
g_return_val_if_fail(acct_xml, FALSE);

if(!xml_add_str(acct_xml, "name",
xaccAccountGetName(a), FALSE))
return(FALSE);
if(!xml_add_guid(acct_xml, "guid",
xaccAccountGetGUID(a)))
return(FALSE);
if(!xml_add_str(acct_xml, "type",
xaccAccountTypeEnumAsString(xaccAccountGetType(a)), FALSE))
return(FALSE);
if(!xml_add_str(acct_xml, "code",
xaccAccountGetCode(a), FALSE))
return(FALSE);
if(!xml_add_str(acct_xml, "description",
xaccAccountGetDescription(a), FALSE))
return(FALSE);
/* Notes field is now in kvp table. */
if(!xml_add_commodity_ref(acct_xml, "currency", xaccAccountGetCurrency(a)))
return(FALSE);
if(!xml_add_commodity_ref(acct_xml, "security", xaccAccountGetSecurity(a)))
return(FALSE);

if(a->kvp_data) {
if(!xml_add_kvp_frame(acct_xml, "slots", a->kvp_data, FALSE))
return(FALSE);
}

{
Account *parent = xaccAccountGetParentAccount(a);
if(parent) {
xmlNodePtr parent_xml = xmlNewTextChild(acct_xml, NULL, "parent", NULL);
g_return_val_if_fail(parent_xml, FALSE);
if(!xml_add_guid(parent_xml, "guid", xaccAccountGetGUID(parent)))
return(FALSE);
}
}

{
AccountGroup *g = xaccAccountGetChildren(a);
if(g) {
GList *list = xaccGroupGetAccountList (g);
GList *node;

for (node = list; node; node = node->next) {
Account *current_acc = node->data;

if(!xml_add_account_restorer(p, current_acc))
return(FALSE);
}
}
}
return(TRUE);
}

/* ============================================================== */
/* loop over all accounts in the group */

gboolean
xml_add_account_restorers(xmlNodePtr p, AccountGroup *g) {
GList *list;
GList *node;

g_return_val_if_fail(p, FALSE);
g_return_val_if_fail(g, FALSE);

list = xaccGroupGetAccountList (g);

for (node = list; node; node = node->next) {
Account *current_acc = node->data;
xml_add_account_restorer(p, current_acc);
}
return(TRUE);
}
12 changes: 2 additions & 10 deletions src/engine/Account.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ xaccAccountEqual(Account *aa, Account *ab, gboolean check_guids) {
if(!aa) return FALSE;
if(!ab) return FALSE;

if(aa->type != ab->type) {
PERR ("Account types don't match (%d != %d)\n",
aa->type, ab->type);
return FALSE;
}
if(aa->type != ab->type) return FALSE;

if(safe_strcmp(aa->accountName, ab->accountName) != 0) return FALSE;
if(safe_strcmp(aa->accountCode, ab->accountCode) != 0) return FALSE;
Expand All @@ -233,11 +229,7 @@ xaccAccountEqual(Account *aa, Account *ab, gboolean check_guids) {
if(!gnc_commodity_equiv(aa->security, ab->security)) return FALSE;

if(check_guids) {
if(!guid_equal(&aa->guid, &ab->guid)) {
PERR ("Account guids don't match for %s ?= %s\n",
aa->accountName, ab->accountName);
return FALSE;
}
if(!guid_equal(&aa->guid, &ab->guid)) return FALSE;
}

if(kvp_frame_compare(aa->kvp_data, ab->kvp_data) != 0) return FALSE;
Expand Down
143 changes: 143 additions & 0 deletions src/engine/Commodity-xml-parser-v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#include "sixtp.h"
#include "sixtp-utils.h"
#include "sixtp-parsers.h"
#include "sixtp-writers.h"
#include "sixtp-xml-write-utils.h"

#include "gnc-commodity.h"
#include "gnc-engine.h"
#include "gnc-engine-util.h"

/****************************************************************************/
/* Commodity restorer.
Expand Down Expand Up @@ -358,3 +361,143 @@ generic_gnc_commodity_lookup_parser_new(void)

return(top_level);
}

/***********************************************************************/
/***********************************************************************/
/* WRITING */

gboolean
xml_add_commodity_ref(xmlNodePtr p, const char *tag, const gnc_commodity *c) {
xmlNodePtr c_xml = NULL;
gboolean ok = FALSE;

if(p && tag) {
if(!c) {
ok = TRUE;
} else {
c_xml= xmlNewTextChild(p, NULL, tag, NULL);
if(c_xml) {
const gchar *namestr = gnc_commodity_get_namespace(c);
if(namestr) {
xmlNodePtr namespace_xml = xmlNewTextChild(c_xml, NULL, "space", namestr);
if(namespace_xml) {
const gchar *idstr = gnc_commodity_get_mnemonic(c);
xmlNodePtr id_xml = xmlNewTextChild(c_xml, NULL, "id", idstr);
if(id_xml) ok = TRUE;
}
}
}
}
}

if(!ok && c_xml) xmlFreeNode(c_xml);
return(TRUE);
}

/* ============================================================== */

static gboolean
xml_add_commodity_restorer(xmlNodePtr p, gnc_commodity *c) {
xmlNodePtr comm_xml;
xmlNodePtr rst_xml;

g_return_val_if_fail(p, FALSE);
g_return_val_if_fail(c, FALSE);

comm_xml = xmlNewTextChild(p, NULL, "commodity", NULL);
g_return_val_if_fail(comm_xml, FALSE);

rst_xml = xmlNewTextChild(comm_xml, NULL, "restore", NULL);
if(!rst_xml) {
xmlFreeNode(comm_xml);
return(FALSE);
}

if(!xml_add_str(rst_xml, "space", gnc_commodity_get_namespace(c), FALSE)) {
xmlFreeNode(comm_xml);
return(FALSE);
}
if(!xml_add_str(rst_xml, "id", gnc_commodity_get_mnemonic(c), FALSE)) {
xmlFreeNode(comm_xml);
return(FALSE);
}
if(!xml_add_str(rst_xml, "name", gnc_commodity_get_fullname(c), FALSE)) {
xmlFreeNode(comm_xml);
return(FALSE);
}
if(!xml_add_str(rst_xml, "xcode", gnc_commodity_get_exchange_code(c), FALSE)) {
xmlFreeNode(comm_xml);
return(FALSE);
}
if(!xml_add_gint64(rst_xml, "fraction", gnc_commodity_get_fraction(c))) {
xmlFreeNode(comm_xml);
return(FALSE);
}

return(TRUE);
}


static gint
compare_namespaces(gconstpointer a, gconstpointer b) {
const gchar *sa = (const gchar *) a;
const gchar *sb = (const gchar *) b;
return(safe_strcmp(sa, sb));
}

static gint
compare_commodity_ids(gconstpointer a, gconstpointer b) {
const gnc_commodity *ca = (const gnc_commodity *) a;
const gnc_commodity *cb = (const gnc_commodity *) b;
return(safe_strcmp(gnc_commodity_get_mnemonic(ca),
gnc_commodity_get_mnemonic(cb)));
}

gboolean
xml_add_commodity_restorers(xmlNodePtr p) {
gnc_commodity_table *commodities;
GList *namespaces;
GList *lp;

g_return_val_if_fail(p, FALSE);

commodities = gnc_engine_commodities();
g_return_val_if_fail(commodities, FALSE);

namespaces = g_list_sort(gnc_commodity_table_get_namespaces(commodities),
compare_namespaces);


for(lp = namespaces; lp; lp = lp->next) {
gchar *space;

if(!lp->data) {
g_list_free (namespaces);
return(FALSE);
}

space = (gchar *) lp->data;
if(strcmp(GNC_COMMODITY_NS_ISO, space) != 0) {
GList *comms = gnc_commodity_table_get_commodities(commodities, space);
GList *lp2;

comms = g_list_sort(comms, compare_commodity_ids);

for(lp2 = comms; lp2; lp2 = lp2->next) {
gnc_commodity *com = (gnc_commodity *) lp2->data;

if(!xml_add_commodity_restorer(p, com)) {
g_list_free (comms);
g_list_free (namespaces);
return(FALSE);
}
}

g_list_free (comms);
}
}

g_list_free (namespaces);

return(TRUE);
}
3 changes: 3 additions & 0 deletions src/engine/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ libgncengine_la_SOURCES = \
sixtp-kvp-parser.c \
sixtp-stack.c \
sixtp-utils.c \
sixtp-xml-write-utils.c \
sixtp.c \
Account-xml-parser-v1.c \
Commodity-xml-parser-v1.c \
Expand Down Expand Up @@ -78,7 +79,9 @@ noinst_HEADERS = \
gnc-event-p.h \
gnc-numeric.h \
sixtp-parsers.h \
sixtp-writers.h \
sixtp-stack.h \
sixtp-xml-write-utils.h \
sixtp.h

EXTRA_DIST = \
Expand Down

0 comments on commit 5f5661e

Please sign in to comment.