[backend/xml] dom_tree_to_text returns std::optional<std::string>

pull/2157/head
Christopher Lam 6 months ago
parent e79db945b2
commit 455ea3e71a

@ -135,21 +135,18 @@ fs_uift_handler (xmlNodePtr node, gpointer data)
{
fsParseData* fspd = static_cast<decltype (fspd)> (data);
int i;
char* nodeTxt;
nodeTxt = dom_tree_to_text (node);
auto nodeTxt = dom_tree_to_text (node);
g_return_val_if_fail (nodeTxt, FALSE);
for (i = 0; uiFreqTypeStrs[i].str != NULL; i++)
{
if (g_strcmp0 (nodeTxt, uiFreqTypeStrs[i].str) == 0)
if (g_strcmp0 (nodeTxt->c_str(), uiFreqTypeStrs[i].str) == 0)
{
fspd->uift = uiFreqTypeStrs[i].uift;
g_free (nodeTxt);
return TRUE;
}
}
g_free (nodeTxt);
return FALSE;
}

@ -210,7 +210,9 @@ squash_extra_whitespace (char* text)
static char*
grab_clean_string (xmlNodePtr tree)
{
return squash_extra_whitespace (g_strstrip (dom_tree_to_text (tree)));
auto txt = dom_tree_to_text (tree);
auto str = g_strdup (txt ? txt->c_str() : "");
return squash_extra_whitespace (g_strstrip (str));
}
static gboolean

@ -395,7 +395,7 @@ gnc_counter_end_handler (gpointer data_for_children,
{
auto strval = dom_tree_to_text (tree);
PERR ("string_to_gint64 failed with input: %s",
strval ? strval : "(null)");
strval ? strval->c_str() : "(null)");
ret = FALSE;
}
else if (g_strcmp0 (type, "transaction") == 0)

@ -320,19 +320,19 @@ dom_tree_to_kvp_frame_given (xmlNodePtr node, KvpFrame* frame)
if (g_strcmp0 ((char*)mark->name, "slot") == 0)
{
xmlNodePtr mark2;
gchar* key = NULL;
const gchar* key = NULL;
std::optional<std::string> maybe_key;
KvpValue* val = NULL;
bool must_free{false};
for (mark2 = mark->xmlChildrenNode; mark2; mark2 = mark2->next)
{
if (g_strcmp0 ((char*)mark2->name, "slot:key") == 0)
{
key = const_cast<char*>(dom_node_to_text (mark2));
key = dom_node_to_text (mark2);
if (!key)
{
key = dom_tree_to_text (mark2);
must_free = true;
maybe_key = dom_tree_to_text (mark2);
key = maybe_key ? maybe_key->c_str() : nullptr;
}
}
else if (g_strcmp0 ((char*)mark2->name, "slot:value") == 0)
@ -357,8 +357,6 @@ dom_tree_to_kvp_frame_given (xmlNodePtr node, KvpFrame* frame)
{
/* FIXME: should put some error here */
}
if (must_free)
g_free (key);
}
}
}
@ -388,7 +386,7 @@ dom_tree_create_instance_slots (xmlNodePtr node, QofInstance* inst)
return dom_tree_to_kvp_frame_given (node, frame);
}
gchar*
std::optional<std::string>
dom_tree_to_text (xmlNodePtr tree)
{
/* Expect *only* text and comment sibling nodes in the given tree --
@ -400,29 +398,29 @@ dom_tree_to_text (xmlNodePtr tree)
Ignores comment nodes and collapse text nodes into one string.
Returns NULL if expectations are unsatisfied.
*/
gchar* result;
std::string rv;
gchar* temp;
g_return_val_if_fail (tree, NULL);
g_return_val_if_fail (tree, std::nullopt);
/* no nodes means it's an empty string text */
if (!tree->xmlChildrenNode)
{
DEBUG ("No children");
return g_strdup ("");
return "";
}
temp = (char*)xmlNodeListGetString (NULL, tree->xmlChildrenNode, TRUE);
if (!temp)
{
DEBUG ("Null string");
return NULL;
return std::nullopt;
}
DEBUG ("node string [%s]", (temp == NULL ? "(null)" : temp));
result = g_strdup (temp);
rv = temp;
xmlFree (temp);
return result;
return rv;
}
gnc_numeric

@ -46,7 +46,7 @@ time64 dom_tree_to_time64 (xmlNodePtr node);
gboolean dom_tree_valid_time64 (time64 ts, const xmlChar* name);
GDate* dom_tree_to_gdate (xmlNodePtr node);
gnc_numeric dom_tree_to_gnc_numeric (xmlNodePtr node);
gchar* dom_tree_to_text (xmlNodePtr tree);
std::optional<std::string> dom_tree_to_text (xmlNodePtr tree);
const char* dom_node_to_text (xmlNodePtr node) noexcept;
gboolean string_to_binary (const gchar* str, void** v, guint64* data_len);
gboolean dom_tree_create_instance_slots (xmlNodePtr node, QofInstance* inst);
@ -85,11 +85,7 @@ apply_xmlnode_text (F&& f, xmlNodePtr node, T default_val = T{})
return f(txt);
if (auto txt = dom_tree_to_text(node))
{
auto rv = f(txt);
g_free(txt);
return rv;
}
return f(txt->c_str());
return default_val;
}

@ -90,7 +90,6 @@ test_dom_tree_to_text (void)
for (i = 0; i < 20; i++)
{
gchar* test_string1;
gchar* test_string2;
xmlNodePtr test_node;
test_node = xmlNewNode (NULL, BAD_CAST "test-node");
@ -98,7 +97,7 @@ test_dom_tree_to_text (void)
xmlNodeAddContent (test_node, BAD_CAST test_string1);
test_string2 = dom_tree_to_text (test_node);
auto test_string2 = dom_tree_to_text (test_node);
if (!test_string2)
{
@ -106,7 +105,7 @@ test_dom_tree_to_text (void)
"null return from dom_tree_to_text");
xmlElemDump (stdout, NULL, test_node);
}
else if (g_strcmp0 (test_string1, test_string2) == 0)
else if (g_strcmp0 (test_string1, test_string2->c_str()) == 0)
{
success_args ("dom_tree_to_text", __FILE__, __LINE__, "with string %s",
test_string1);
@ -119,7 +118,6 @@ test_dom_tree_to_text (void)
xmlFreeNode (test_node);
g_free (test_string1);
if (test_string2) g_free (test_string2);
}
}

@ -142,26 +142,22 @@ check_dom_tree_version (xmlNodePtr node, const char* verstr)
gboolean
equals_node_val_vs_string (xmlNodePtr node, const gchar* str)
{
gchar* cmp1;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (str, FALSE);
cmp1 = dom_tree_to_text (node);
auto cmp1 = dom_tree_to_text (node);
if (!cmp1)
{
return FALSE;
}
else if (g_strcmp0 (cmp1, str) == 0)
else if (g_strcmp0 (cmp1->c_str(), str) == 0)
{
g_free (cmp1);
return TRUE;
}
else
{
printf ("Differing types: node:`%s' vs string:`%s'\n", cmp1, str);
g_free (cmp1);
printf ("Differing types: node:`%s' vs string:`%s'\n", cmp1->c_str(), str);
return FALSE;
}
}
@ -169,21 +165,17 @@ equals_node_val_vs_string (xmlNodePtr node, const gchar* str)
gboolean
equals_node_val_vs_int (xmlNodePtr node, gint64 val)
{
gchar* text;
gint64 test_val;
g_return_val_if_fail (node, FALSE);
text = dom_tree_to_text (node);
auto text = dom_tree_to_text (node);
if (!string_to_gint64 (text, &test_val))
if (!text || !string_to_gint64 (*text, &test_val))
{
g_free (text);
return FALSE;
}
g_free (text);
return val == test_val;
}

@ -54,13 +54,12 @@ test_string_converters (void)
{
const char* mark = test_strings[i];
xmlNodePtr test_node = text_to_dom_tree ("test-string", mark);
char* backout = dom_tree_to_text (test_node);
auto backout = dom_tree_to_text (test_node);
do_test_args (
g_strcmp0 (backout, mark) == 0,
g_strcmp0 (backout->c_str(), mark) == 0,
"string converting", __FILE__, __LINE__, "with string %s", mark);
g_free (backout);
xmlFreeNode (test_node);
}
}
@ -72,12 +71,11 @@ test_bad_string (void)
const char* sanitized = "foo?bar";
xmlNodePtr test_node = text_to_dom_tree ("test-string", badstr);
char* backout = dom_tree_to_text (test_node);
do_test_args (g_strcmp0 (backout, sanitized) == 0,
auto backout = dom_tree_to_text (test_node);
do_test_args (g_strcmp0 (backout->c_str(), sanitized) == 0,
"string sanitizing", __FILE__, __LINE__,
"with string %s", badstr);
g_free (backout);
xmlFreeNode (test_node);
}

@ -88,29 +88,22 @@ node_and_account_equal (xmlNodePtr node, Account* act)
}
else if (g_strcmp0 ((char*)mark->name, "act:type") == 0)
{
gchar* txt;
GNCAccountType type;
txt = dom_tree_to_text (mark);
auto txt = dom_tree_to_text (mark);
if (!txt)
{
return g_strdup ("couldn't get type string");
}
else if (!xaccAccountStringToType (txt, &type))
else if (!xaccAccountStringToType (txt->c_str(), &type))
{
g_free (txt);
return g_strdup ("couldn't convert type string to int");
}
else if (type != xaccAccountGetType (act))
{
g_free (txt);
return g_strdup ("types differ");
}
else
{
g_free (txt);
}
}
else if (g_strcmp0 ((char*)mark->name, "act:commodity") == 0)
{

@ -105,30 +105,23 @@ node_and_commodity_equal (xmlNodePtr node, const gnc_commodity* com)
}
else if (g_strcmp0 ((char*)mark->name, "cmdty:fraction") == 0)
{
gchar* txt;
gint64 type;
txt = dom_tree_to_text (mark);
auto txt = dom_tree_to_text (mark);
if (!txt)
{
return "couldn't get fraction string";
}
else if (!string_to_gint64 (txt, &type))
else if (!string_to_gint64 (*txt, &type))
{
g_free (txt);
return "couldn't convert fraction string to int";
}
else if (type != gnc_commodity_get_fraction (com))
{
g_free (txt);
return "fractions differ";
}
else
{
g_free (txt);
}
}
else if (g_strcmp0 ((char*)mark->name, "cmdty:slots") == 0)
{

@ -116,25 +116,21 @@ equals_node_val_vs_split_internal (xmlNodePtr node, Split* spl)
}
else if (g_strcmp0 ((char*)mark->name, "split:memo") == 0)
{
char* memo = dom_tree_to_text (mark);
auto memo = dom_tree_to_text (mark);
if (g_strcmp0 (memo, xaccSplitGetMemo (spl)) != 0)
if (g_strcmp0 (memo->c_str(), xaccSplitGetMemo (spl)) != 0)
{
g_free (memo);
return "memos differ";
}
g_free (memo);
}
else if (g_strcmp0 ((char*)mark->name, "split:reconciled-state") == 0)
{
char* rs = dom_tree_to_text (mark);
auto rs = dom_tree_to_text (mark);
if (rs[0] != xaccSplitGetReconcile (spl))
if (!rs || rs->front() != xaccSplitGetReconcile (spl))
{
g_free (rs);
return "states differ";
}
g_free (rs);
}
else if (g_strcmp0 ((char*)mark->name, "split:value") == 0)
{

Loading…
Cancel
Save