31#include "gnc-xml-helper.h"
32#include "sixtp-utils.h"
33#include "sixtp-dom-parsers.h"
34#include <kvp-frame.hpp>
36static QofLogModule log_module = GNC_MOD_IO;
39dom_node_to_text (xmlNodePtr node)
noexcept
41 if (node && node->children && node->children->type == XML_TEXT_NODE
42 && !node->children->next)
43 return reinterpret_cast<const char*
>(node->children->content);
48dom_tree_to_guid (xmlNodePtr node)
50 auto type = xmlGetProp (node, BAD_CAST
"type");
54 bool ok = !g_strcmp0 ((
char*)type,
"guid") || !g_strcmp0 ((
char*)type,
"new");
61 auto extract_guid = [](
auto str) -> std::optional<GncGUID>
63 if (
GncGUID guid; string_to_guid (str, &guid))
69 return apply_xmlnode_text<std::optional<GncGUID>>(extract_guid, node);
73dom_tree_to_integer_kvp_value (xmlNodePtr node)
75 auto node_to_int_kvp = [](
auto txt) -> KvpValue*
77 if (gint64 daint; string_to_gint64 (txt, &daint))
78 return new KvpValue{daint};
82 return apply_xmlnode_text<KvpValue*> (node_to_int_kvp, node,
nullptr);
87dom_tree_to_num (xmlNodePtr node, std::function<
bool(
const char*, T*)>string_to_num, T* num_ptr)
89 return apply_xmlnode_text<T>([&](
auto txt){
return string_to_num (txt, num_ptr);}, node,
false);
93dom_tree_to_integer (xmlNodePtr node, gint64* daint)
95 return dom_tree_to_num<gint64>(node, string_to_gint64, daint);
99dom_tree_to_guint16 (xmlNodePtr node, guint16* i)
101 return dom_tree_to_num<guint16>(node, string_to_guint16, i);
105dom_tree_to_guint (xmlNodePtr node, guint* i)
107 return dom_tree_to_num<guint>(node, string_to_guint, i);
111dom_tree_to_boolean (xmlNodePtr node, gboolean* b)
113 auto set_bool = [b](
auto text) -> gboolean
115 if (g_ascii_strncasecmp (text,
"true", 4) == 0)
120 else if (g_ascii_strncasecmp (text,
"false", 5) == 0)
131 return apply_xmlnode_text<gboolean> (set_bool, node);
135dom_tree_to_double_kvp_value (xmlNodePtr node)
137 auto node_to_double_kvp = [](
auto txt) -> KvpValue*
139 if (
double dadoub; string_to_double (txt, &dadoub))
return new KvpValue{dadoub};
142 return apply_xmlnode_text<KvpValue*> (node_to_double_kvp, node,
nullptr);
146dom_tree_to_numeric_kvp_value (xmlNodePtr node)
148 return new KvpValue {dom_tree_to_gnc_numeric (node)};
152dom_tree_to_string_kvp_value (xmlNodePtr node)
154 auto node_to_string_kvp = [](
auto txt) -> KvpValue*
156 return new KvpValue {g_strdup (txt)};
158 return apply_xmlnode_text<KvpValue*> (node_to_string_kvp, node,
nullptr);
162dom_tree_to_guid_kvp_value (xmlNodePtr node)
164 auto daguid = dom_tree_to_guid (node);
165 return daguid ?
new KvpValue {
guid_copy (&*daguid)} :
nullptr;
169dom_tree_to_time64_kvp_value (xmlNodePtr node)
171 Time64 t{dom_tree_to_time64 (node)};
172 return new KvpValue {t};
176dom_tree_to_gdate_kvp_value (xmlNodePtr node)
178 auto date = dom_tree_to_gdate (node);
179 if (!date)
return nullptr;
180 return new KvpValue {*date};
183static KvpValue* dom_tree_to_kvp_value (xmlNodePtr node);
185KvpFrame* dom_tree_to_kvp_frame (xmlNodePtr node);
188dom_tree_to_list_kvp_value (xmlNodePtr node)
192 KvpValue* ret = NULL;
194 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
198 if (g_strcmp0 ((
char*)mark->name,
"text") == 0)
201 new_val = dom_tree_to_kvp_value (mark);
204 list = g_list_prepend (list, (gpointer)new_val);
208 list = g_list_reverse (list);
210 ret =
new KvpValue {list};
216dom_tree_to_frame_kvp_value (xmlNodePtr node)
218 KvpFrame* frame = dom_tree_to_kvp_frame (node);
219 return frame ?
new KvpValue {frame} :
nullptr;
226 KvpValue* (*converter) (xmlNodePtr node);
233 {
"integer", dom_tree_to_integer_kvp_value },
234 {
"double", dom_tree_to_double_kvp_value },
235 {
"numeric", dom_tree_to_numeric_kvp_value },
236 {
"string", dom_tree_to_string_kvp_value },
237 {
"guid", dom_tree_to_guid_kvp_value },
238 {
"timespec", dom_tree_to_time64_kvp_value },
239 {
"gdate", dom_tree_to_gdate_kvp_value },
240 {
"list", dom_tree_to_list_kvp_value },
241 {
"frame", dom_tree_to_frame_kvp_value },
246dom_tree_to_kvp_value (xmlNodePtr node)
250 KvpValue* ret = NULL;
252 xml_type = xmlGetProp (node, BAD_CAST
"type");
254 for (mark = val_converters; mark->tag; mark++)
256 if (g_strcmp0 (
reinterpret_cast<char*
>(xml_type), mark->tag) == 0)
258 ret = (mark->converter) (node);
273dom_tree_to_kvp_frame_given (xmlNodePtr node, KvpFrame* frame)
277 g_return_val_if_fail (node, FALSE);
278 g_return_val_if_fail (frame, FALSE);
280 for (mark = node->xmlChildrenNode; mark; mark = mark->next)
282 if (g_strcmp0 ((
char*)mark->name,
"slot") == 0)
285 const gchar* key = NULL;
286 std::optional<std::string> maybe_key;
287 KvpValue* val = NULL;
289 for (mark2 = mark->xmlChildrenNode; mark2; mark2 = mark2->next)
291 if (g_strcmp0 ((
char*)mark2->name,
"slot:key") == 0)
293 key = dom_node_to_text (mark2);
296 maybe_key = dom_tree_to_text (mark2);
297 key = maybe_key ? maybe_key->c_str() :
nullptr;
300 else if (g_strcmp0 ((
char*)mark2->name,
"slot:value") == 0)
302 val = dom_tree_to_kvp_value (mark2);
316 delete frame->set ({key}, val);
331dom_tree_to_kvp_frame (xmlNodePtr node)
333 g_return_val_if_fail (node, NULL);
335 auto ret =
new KvpFrame;
337 if (dom_tree_to_kvp_frame_given (node, ret))
345dom_tree_create_instance_slots (xmlNodePtr node, QofInstance* inst)
347 KvpFrame* frame = qof_instance_get_slots (inst);
348 return dom_tree_to_kvp_frame_given (node, frame);
351std::optional<std::string>
352dom_tree_to_text (xmlNodePtr tree)
366 g_return_val_if_fail (tree, std::nullopt);
369 if (!tree->xmlChildrenNode)
371 DEBUG (
"No children");
375 temp = (
char*)xmlNodeListGetString (NULL, tree->xmlChildrenNode, TRUE);
378 DEBUG (
"Null string");
382 DEBUG (
"node string [%s]", (temp == NULL ?
"(null)" : temp));
389dom_tree_to_gnc_numeric (xmlNodePtr node)
391 auto node_to_numeric = [](
auto txt)
396 return apply_xmlnode_text<gnc_numeric> (node_to_numeric, node, gnc_numeric_zero());
401dom_tree_to_time64 (xmlNodePtr node)
415 gboolean seen = FALSE;
418 for (n = node->xmlChildrenNode; n; n = n->next)
422 case XML_COMMENT_NODE:
425 case XML_ELEMENT_NODE:
426 if (g_strcmp0 (
"ts:date", (
char*)n->name) == 0)
433 ret = apply_xmlnode_text<time64> (gnc_iso8601_to_time64_gmt, n, INT64_MAX);
437 PERR (
"unexpected sub-node.");
445 PERR (
"no ts:date node found.");
453dom_tree_to_gdate (xmlNodePtr node)
464 gboolean seen_date = FALSE;
467 auto try_setting_date = [&ret](
const char *content) ->
bool
469 gint year = 0, month = 0, day = 0;
470 if (sscanf (content,
"%d-%d-%d", &year, &month, &day) != 3)
return false;
471 g_date_set_dmy (&ret, day,
static_cast<GDateMonth
>(month), year);
472 return (g_date_valid (&ret));
476 g_date_clear (&ret, 1);
478 for (n = node->xmlChildrenNode; n; n = n->next)
482 case XML_COMMENT_NODE:
485 case XML_ELEMENT_NODE:
486 if (g_strcmp0 (
"gdate", (
char*)n->name) == 0)
488 if (seen_date || !apply_xmlnode_text<bool> (try_setting_date, n))
494 PERR (
"unexpected sub-node.");
501 PWARN (
"no gdate node found.");
505 return std::make_optional<GDate>(ret);
515gnc_strstrip (std::string_view sv)
517 while (!sv.empty () && g_ascii_isspace (sv.front())) sv.remove_prefix (1);
518 while (!sv.empty () && g_ascii_isspace (sv.back())) sv.remove_suffix (1);
519 return std::string (sv);
522static std::optional<CommodityRef>
523parse_commodity_ref (xmlNodePtr node,
QofBook* book)
536 bool space_set{
false};
540 if (!node)
return {};
541 if (!node->xmlChildrenNode)
return {};
543 for (n = node->xmlChildrenNode; n; n = n->next)
547 case XML_COMMENT_NODE:
550 case XML_ELEMENT_NODE:
551 if (g_strcmp0 (
"cmdty:space", (
char*)n->name) == 0)
557 rv.space = apply_xmlnode_text<std::string> (gnc_strstrip, n);
560 else if (g_strcmp0 (
"cmdty:id", (
char*)n->name) == 0)
566 rv.id = apply_xmlnode_text<std::string> (gnc_strstrip, n);
571 PERR (
"unexpected sub-node.");
576 if (space_set && id_set)
583dom_tree_to_commodity_ref_no_engine (xmlNodePtr node,
QofBook* book)
585 auto ref = parse_commodity_ref (node, book);
595dom_tree_to_commodity_ref (xmlNodePtr node,
QofBook* book)
598 gnc_commodity_table*
table;
600 auto ref = parse_commodity_ref (node, book);
607 g_return_val_if_fail (
table != NULL, NULL);
609 ret = gnc_commodity_table_lookup (
table, ref->space.c_str(), ref->id.c_str());
611 g_return_val_if_fail (ret != NULL, NULL);
622 for (; handlers->tag != NULL; handlers++)
624 handlers->gotten = 0;
628static inline gboolean
632 for (; handlers->tag != NULL; handlers++)
634 if (handlers->required && ! handlers->gotten)
636 PERR (
"Not defined and it should be: %s",
637 handlers->tag ? handlers->tag :
"(null)");
645static inline gboolean
646gnc_xml_set_data (
const gchar* tag, xmlNodePtr node, gpointer item,
649 for (; handlers->tag != NULL; handlers++)
651 if (g_strcmp0 (tag, handlers->tag) == 0)
653 (handlers->handler) (node, item);
654 handlers->gotten = TRUE;
661 PERR (
"Unhandled tag: %s",
662 tag ? tag :
"(null)");
674 gboolean successful = TRUE;
676 dom_tree_handlers_reset (handlers);
678 for (achild = node->xmlChildrenNode; achild; achild = achild->next)
681 if (g_strcmp0 ((
char*)achild->name,
"text") == 0)
684 if (!gnc_xml_set_data ((
char*)achild->name, achild, data, handlers))
686 PERR (
"gnc_xml_set_data failed");
692 if (!dom_tree_handlers_all_gotten_p (handlers))
694 PERR (
"didn't find all of the expected tags in the input");
702dom_tree_valid_time64 (
time64 val,
const xmlChar * name)
704 if (val != INT64_MAX)
706 g_warning (
"Invalid timestamp in data file. Look for a '%s' entry "
707 "with a year outside of the valid range: 1400..10000", name);
All type declarations for the whole Gnucash engine.
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
Create a new commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
GncGUID * guid_copy(const GncGUID *guid)
Returns a newly allocated GncGUID that matches the passed-in GUID.
#define DEBUG(format, args...)
Print a debugging message.
#define PERR(format, args...)
Log a serious error.
#define PWARN(format, args...)
Log a warning.
gnc_numeric gnc_numeric_from_string(const gchar *str)
Read a gnc_numeric from str, skipping any leading whitespace.
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
The type used to store guids in C.