30 #include "sixtp-utils.h"
31 #include "sixtp-parsers.h"
32 #include "sixtp-dom-parsers.h"
33 #include "sixtp-dom-generators.h"
34 #include "io-gncxml-gen.h"
38 #include "gnc-pricedb-p.h"
89 price_parse_xml_sub_node(
GNCPrice *p, xmlNodePtr sub_node,
QofBook *book)
91 if (!p || !sub_node)
return FALSE;
93 gnc_price_begin_edit (p);
94 if (g_strcmp0(
"price:id", (
char*)sub_node->name) == 0)
96 GncGUID *c = dom_tree_to_guid(sub_node);
98 gnc_price_set_guid(p, c);
101 else if (g_strcmp0(
"price:commodity", (
char*)sub_node->name) == 0)
103 gnc_commodity *c = dom_tree_to_commodity_ref(sub_node, book);
104 if (!c)
return FALSE;
105 gnc_price_set_commodity(p, c);
107 else if (g_strcmp0(
"price:currency", (
char*)sub_node->name) == 0)
109 gnc_commodity *c = dom_tree_to_commodity_ref(sub_node, book);
110 if (!c)
return FALSE;
111 gnc_price_set_currency(p, c);
113 else if (g_strcmp0(
"price:time", (
char*)sub_node->name) == 0)
115 Timespec t = dom_tree_to_timespec(sub_node);
116 if (!dom_tree_valid_timespec(&t, sub_node->name))
return FALSE;
117 gnc_price_set_time(p, t);
119 else if (g_strcmp0(
"price:source", (
char*)sub_node->name) == 0)
121 char *text = dom_tree_to_text(sub_node);
122 if (!text)
return FALSE;
123 gnc_price_set_source(p, text);
126 else if (g_strcmp0(
"price:type", (
char*)sub_node->name) == 0)
128 char *text = dom_tree_to_text(sub_node);
129 if (!text)
return FALSE;
130 gnc_price_set_typestr(p, text);
133 else if (g_strcmp0(
"price:value", (
char*)sub_node->name) == 0)
135 gnc_numeric *value = dom_tree_to_gnc_numeric(sub_node);
136 if (!value)
return FALSE;
137 gnc_price_set_value(p, *value);
140 gnc_price_commit_edit (p);
145 price_parse_xml_end_handler(gpointer data_for_children,
146 GSList* data_from_children,
147 GSList* sibling_data,
148 gpointer parent_data,
149 gpointer global_data,
154 xmlNodePtr price_xml = (xmlNodePtr) data_for_children;
158 QofBook *book = gdata->bookdata;
161 if (parent_data)
return TRUE;
165 if (!price_xml)
return FALSE;
169 goto cleanup_and_exit;
174 goto cleanup_and_exit;
176 if (!price_xml->xmlChildrenNode)
179 goto cleanup_and_exit;
186 goto cleanup_and_exit;
189 for (child = price_xml->xmlChildrenNode; child; child = child->next)
193 case XML_COMMENT_NODE:
196 case XML_ELEMENT_NODE:
197 if (!price_parse_xml_sub_node(p, child, book))
200 goto cleanup_and_exit;
204 PERR(
"Unknown node type (%d) while parsing gnc-price xml.", child->type);
207 goto cleanup_and_exit;
222 xmlFreeNode(price_xml);
233 gnc_price_parser_new (
void)
235 return sixtp_dom_parser_new(price_parse_xml_end_handler,
258 pricedb_start_handler(GSList* sibling_data,
259 gpointer parent_data,
260 gpointer global_data,
261 gpointer *data_for_children,
267 QofBook *book = gdata->bookdata;
269 g_return_val_if_fail(db, FALSE);
276 pricedb_after_child_handler(gpointer data_for_children,
277 GSList* data_from_children,
278 GSList* sibling_data,
279 gpointer parent_data,
280 gpointer global_data,
283 const gchar *child_tag,
290 g_return_val_if_fail(db, FALSE);
293 if (!child_result)
return(FALSE);
294 if (child_result->type != SIXTP_CHILD_RESULT_NODE)
return(FALSE);
296 if (strcmp(child_result->tag,
"price") == 0)
300 g_return_val_if_fail(p, FALSE);
302 gd->counter.prices_loaded++;
308 PERR (
"unexpected tag %s\n", child_result->tag);
326 pricedb_v2_end_handler(
327 gpointer data_for_children, GSList* data_from_children,
328 GSList* sibling_data, gpointer parent_data, gpointer global_data,
329 gpointer *result,
const gchar *tag)
344 gdata->cb(tag, gdata->parsedata, db);
353 gnc_pricedb_parser_new(
void)
359 sixtp_set_any(sixtp_new(), TRUE,
360 SIXTP_START_HANDLER_ID, pricedb_start_handler,
361 SIXTP_AFTER_CHILD_HANDLER_ID, pricedb_after_child_handler,
362 SIXTP_CHARACTERS_HANDLER_ID,
363 allow_and_ignore_only_whitespace,
364 SIXTP_RESULT_FAIL_ID, pricedb_cleanup_result_handler,
365 SIXTP_CLEANUP_RESULT_ID, pricedb_cleanup_result_handler,
366 SIXTP_NO_MORE_HANDLERS);
368 if (!top_level)
return NULL;
370 price_parser = gnc_price_parser_new();
374 sixtp_destroy(top_level);
378 sixtp_add_sub_parser(top_level,
"price", price_parser);
384 gnc_pricedb_sixtp_parser_create(
void)
387 ret = gnc_pricedb_parser_new();
388 sixtp_set_end(ret, pricedb_v2_end_handler);
398 add_child_or_kill_parent(xmlNodePtr parent, xmlNodePtr child)
405 xmlAddChild(parent, child);
410 gnc_price_to_dom_tree(
const xmlChar *tag,
GNCPrice *price)
412 xmlNodePtr price_xml;
413 const gchar *typestr, *sourcestr;
420 if (!(tag && price))
return NULL;
422 price_xml = xmlNewNode(NULL, tag);
423 if (!price_xml)
return NULL;
425 commodity = gnc_price_get_commodity(price);
426 currency = gnc_price_get_currency(price);
428 if (!(commodity && currency))
return NULL;
430 tmpnode = guid_to_dom_tree(
"price:id", gnc_price_get_guid(price));
431 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
433 tmpnode = commodity_ref_to_dom_tree(
"price:commodity", commodity);
434 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
436 tmpnode = commodity_ref_to_dom_tree(
"price:currency", currency);
437 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
439 timesp = gnc_price_get_time(price);
440 tmpnode = timespec_to_dom_tree(
"price:time", ×p);
441 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
443 sourcestr = gnc_price_get_source(price);
444 if (sourcestr && (strlen(sourcestr) != 0))
446 tmpnode = text_to_dom_tree(
"price:source", sourcestr);
447 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
450 typestr = gnc_price_get_typestr(price);
451 if (typestr && (strlen(typestr) != 0))
453 tmpnode = text_to_dom_tree(
"price:type", typestr);
454 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
457 value = gnc_price_get_value(price);
458 tmpnode = gnc_numeric_to_dom_tree(
"price:value", &value);
459 if (!add_child_or_kill_parent(price_xml, tmpnode))
return NULL;
465 xml_add_gnc_price_adapter(
GNCPrice *p, gpointer data)
467 xmlNodePtr xml_node = (xmlNodePtr) data;
471 xmlNodePtr price_xml = gnc_price_to_dom_tree(BAD_CAST
"price", p);
472 if (!price_xml)
return FALSE;
473 xmlAddChild(xml_node, price_xml);
483 gnc_pricedb_to_dom_tree(
const xmlChar *tag,
GNCPriceDB *db)
485 xmlNodePtr db_xml = NULL;
487 if (!tag)
return NULL;
489 db_xml = xmlNewNode(NULL, tag);
490 if (!db_xml)
return NULL;
492 xmlSetProp(db_xml, BAD_CAST
"version", BAD_CAST
"1");
501 if (!db_xml->xmlChildrenNode)
513 return gnc_pricedb_to_dom_tree(BAD_CAST
"gnc:pricedb", db);
GNCPrice * gnc_price_create(QofBook *book)
a simple price database for gnucash
void gnc_price_unref(GNCPrice *p)
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Use a 64-bit unsigned int timespec.
#define PERR(format, args...)
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
api for GnuCash version 2 XML-based file format
void gnc_pricedb_destroy(GNCPriceDB *db)
gboolean gnc_pricedb_foreach_price(GNCPriceDB *db, gboolean(*f)(GNCPrice *p, gpointer user_data), gpointer user_data, gboolean stable_order)
void gnc_pricedb_set_bulk_update(GNCPriceDB *db, gboolean bulk_update)
void run_callback(sixtp_gdv2 *data, const char *type)
const gchar * QofLogModule