mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Removing timspec from some xml parsing
And several other sprawling references that are connected in one way or another.
This commit is contained in:
@@ -244,9 +244,10 @@ static inline gboolean
|
||||
set_timespec (xmlNodePtr node, GncEntry* entry,
|
||||
void (*func) (GncEntry* entry, Timespec ts))
|
||||
{
|
||||
Timespec ts = dom_tree_to_timespec (node);
|
||||
if (!dom_tree_valid_timespec (&ts, node->name)) return FALSE;
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
if (!dom_tree_valid_time64 (time, node->name)) return FALSE;
|
||||
|
||||
Timespec ts = {time, 0};
|
||||
func (entry, ts);
|
||||
return TRUE;
|
||||
}
|
||||
|
@@ -188,9 +188,10 @@ static inline gboolean
|
||||
set_timespec (xmlNodePtr node, GncInvoice* invoice,
|
||||
void (*func) (GncInvoice* invoice, Timespec ts))
|
||||
{
|
||||
Timespec ts = dom_tree_to_timespec (node);
|
||||
if (!dom_tree_valid_timespec (&ts, node->name)) return FALSE;
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
if (!dom_tree_valid_time64 (time, node->name)) return FALSE;
|
||||
|
||||
Timespec ts = {time, 0};
|
||||
func (invoice, ts);
|
||||
return TRUE;
|
||||
}
|
||||
|
@@ -134,9 +134,9 @@ static inline gboolean
|
||||
set_timespec (xmlNodePtr node, GncOrder* order,
|
||||
void (*func) (GncOrder* order, Timespec ts))
|
||||
{
|
||||
Timespec ts = dom_tree_to_timespec (node);
|
||||
if (!dom_tree_valid_timespec (&ts, node->name)) return FALSE;
|
||||
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
if (!dom_tree_valid_time64 (time, node->name)) return FALSE;
|
||||
Timespec ts {time, 0};
|
||||
func (order, ts);
|
||||
return TRUE;
|
||||
}
|
||||
|
@@ -113,9 +113,10 @@ price_parse_xml_sub_node (GNCPrice* p, xmlNodePtr sub_node, QofBook* book)
|
||||
}
|
||||
else if (g_strcmp0 ("price:time", (char*)sub_node->name) == 0)
|
||||
{
|
||||
Timespec t = dom_tree_to_timespec (sub_node);
|
||||
if (!dom_tree_valid_timespec (&t, sub_node->name)) return FALSE;
|
||||
gnc_price_set_time (p, t);
|
||||
time64 time = dom_tree_to_time64 (sub_node);
|
||||
if (!dom_tree_valid_time64 (time, sub_node->name)) return FALSE;
|
||||
Timespec ts {time, 0};
|
||||
gnc_price_set_time (p, ts);
|
||||
}
|
||||
else if (g_strcmp0 ("price:source", (char*)sub_node->name) == 0)
|
||||
{
|
||||
|
@@ -275,13 +275,9 @@ static gboolean
|
||||
spl_reconcile_date_handler (xmlNodePtr node, gpointer data)
|
||||
{
|
||||
struct split_pdata* pdata = static_cast<decltype (pdata)> (data);
|
||||
Timespec ts;
|
||||
|
||||
ts = dom_tree_to_timespec (node);
|
||||
if (!dom_tree_valid_timespec (&ts, node->name)) return FALSE;
|
||||
|
||||
xaccSplitSetDateReconciledTS (pdata->split, &ts);
|
||||
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
if (!dom_tree_valid_time64 (time, node->name)) return FALSE;
|
||||
xaccSplitSetDateReconciledSecs (pdata->split, time);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -434,14 +430,10 @@ static inline gboolean
|
||||
set_tran_date (xmlNodePtr node, Transaction* trn,
|
||||
void (*func) (Transaction* trn, const Timespec* tm))
|
||||
{
|
||||
Timespec tm;
|
||||
|
||||
tm = dom_tree_to_timespec (node);
|
||||
|
||||
if (!dom_tree_valid_timespec (&tm, node->name)) return FALSE;
|
||||
|
||||
func (trn, &tm);
|
||||
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
if (!dom_tree_valid_time64 (time, node->name)) return FALSE;
|
||||
Timespec ts {time, 0};
|
||||
func (trn, &ts);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@@ -2959,9 +2959,10 @@ price_parse_xml_sub_node (GNCPrice* p, xmlNodePtr sub_node, QofBook* book)
|
||||
}
|
||||
else if (g_strcmp0 ("price:time", (char*)sub_node->name) == 0)
|
||||
{
|
||||
Timespec t = dom_tree_to_timespec (sub_node);
|
||||
if (!dom_tree_valid_timespec (&t, sub_node->name)) return FALSE;
|
||||
gnc_price_set_time (p, t);
|
||||
time64 time = dom_tree_to_time64 (sub_node);
|
||||
if (!dom_tree_valid_time64 (time, sub_node->name)) return FALSE;
|
||||
Timespec ts = {time, 0};
|
||||
gnc_price_set_time (p, ts);
|
||||
}
|
||||
else if (g_strcmp0 ("price:source", (char*)sub_node->name) == 0)
|
||||
{
|
||||
|
@@ -131,6 +131,12 @@ commodity_ref_to_dom_tree (const char* tag, const gnc_commodity* c)
|
||||
return ret;
|
||||
}
|
||||
|
||||
char*
|
||||
time64_to_string (time64 time)
|
||||
{
|
||||
return gnc_print_time64 (time, TIMESPEC_TIME_FORMAT " %q");
|
||||
}
|
||||
|
||||
char*
|
||||
timespec_sec_to_string (const Timespec* ts)
|
||||
{
|
||||
@@ -143,6 +149,21 @@ timespec_nsec_to_string (const Timespec* ts)
|
||||
return g_strdup_printf ("%ld", ts->tv_nsec);
|
||||
}
|
||||
|
||||
xmlNodePtr
|
||||
time64_to_dom_tree (const char* tag, const time64 time)
|
||||
{
|
||||
xmlNodePtr ret;
|
||||
g_return_val_if_fail (time, NULL);
|
||||
auto date_str = gnc_print_time64 (time, TIMESPEC_TIME_FORMAT " %q");
|
||||
if (!date_str)
|
||||
return NULL;
|
||||
ret = xmlNewNode (NULL, BAD_CAST tag);
|
||||
xmlNewTextChild (ret, NULL, BAD_CAST "ts:date",
|
||||
checked_char_cast (date_str));
|
||||
g_free (date_str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
xmlNodePtr
|
||||
timespec_to_dom_tree (const char* tag, const Timespec* spec)
|
||||
{
|
||||
|
@@ -41,8 +41,10 @@ xmlNodePtr boolean_to_dom_tree (const char* tag, gboolean val);
|
||||
xmlNodePtr guid_to_dom_tree (const char* tag, const GncGUID* gid);
|
||||
xmlNodePtr commodity_ref_to_dom_tree (const char* tag, const gnc_commodity* c);
|
||||
xmlNodePtr timespec_to_dom_tree (const char* tag, const Timespec* spec);
|
||||
xmlNodePtr time64_to_dom_tree (const char* tag, time64);
|
||||
gchar* timespec_nsec_to_string (const Timespec* ts);
|
||||
gchar* timespec_sec_to_string (const Timespec* ts);
|
||||
gchar* time64_to_string (time64);
|
||||
xmlNodePtr gdate_to_dom_tree (const char* tag, const GDate* spec);
|
||||
xmlNodePtr gnc_numeric_to_dom_tree (const char* tag, const gnc_numeric* num);
|
||||
xmlNodePtr qof_instance_slots_to_dom_tree (const char* tag,
|
||||
|
@@ -233,14 +233,9 @@ dom_tree_to_guid_kvp_value (xmlNodePtr node)
|
||||
}
|
||||
|
||||
static KvpValue*
|
||||
dom_tree_to_timespec_kvp_value (xmlNodePtr node)
|
||||
dom_tree_to_time64_kvp_value (xmlNodePtr node)
|
||||
{
|
||||
Timespec ts;
|
||||
KvpValue* ret = nullptr;
|
||||
|
||||
ts = dom_tree_to_timespec (node);
|
||||
ret = new KvpValue {ts};
|
||||
return ret;
|
||||
return new KvpValue {Timespec {dom_tree_to_time64 (node), 0}};
|
||||
}
|
||||
|
||||
static KvpValue*
|
||||
@@ -360,7 +355,7 @@ struct kvp_val_converter val_converters[] =
|
||||
{ "numeric", dom_tree_to_numeric_kvp_value },
|
||||
{ "string", dom_tree_to_string_kvp_value },
|
||||
{ "guid", dom_tree_to_guid_kvp_value },
|
||||
{ "timespec", dom_tree_to_timespec_kvp_value },
|
||||
{ "timespec", dom_tree_to_time64_kvp_value },
|
||||
{ "gdate", dom_tree_to_gdate_kvp_value },
|
||||
{ "list", dom_tree_to_list_kvp_value },
|
||||
{ "frame", dom_tree_to_frame_kvp_value },
|
||||
@@ -528,17 +523,15 @@ dom_tree_to_gnc_numeric (xmlNodePtr node)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline Timespec
|
||||
timespec_failure (Timespec ts)
|
||||
static time64
|
||||
time_parse_failure ()
|
||||
{
|
||||
ts.tv_sec = 0;
|
||||
ts.tv_nsec = 0;
|
||||
return ts;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Timespec
|
||||
dom_tree_to_timespec (xmlNodePtr node)
|
||||
time64
|
||||
dom_tree_to_time64 (xmlNodePtr node)
|
||||
{
|
||||
/* Turn something like this
|
||||
|
||||
@@ -547,18 +540,15 @@ dom_tree_to_timespec (xmlNodePtr node)
|
||||
<ns>658864000</ns>
|
||||
</date-posted>
|
||||
|
||||
into a Timespec. If this returns FALSE, the effects on *ts are
|
||||
into a time64. If this returns FALSE, the effects on *ts are
|
||||
undefined. The XML is valid if it has at least one of <s> or <ns>
|
||||
and no more than one of each. Order is irrelevant. */
|
||||
|
||||
Timespec ret;
|
||||
time64 ret {0};
|
||||
gboolean seen_s = FALSE;
|
||||
gboolean seen_ns = FALSE;
|
||||
xmlNodePtr n;
|
||||
|
||||
|
||||
ret.tv_sec = 0;
|
||||
ret.tv_nsec = 0;
|
||||
for (n = node->xmlChildrenNode; n; n = n->next)
|
||||
{
|
||||
switch (n->type)
|
||||
@@ -571,52 +561,29 @@ dom_tree_to_timespec (xmlNodePtr node)
|
||||
{
|
||||
if (seen_s)
|
||||
{
|
||||
return timespec_failure (ret);
|
||||
return time_parse_failure ();
|
||||
}
|
||||
else
|
||||
{
|
||||
gchar* content = dom_tree_to_text (n);
|
||||
if (!content)
|
||||
{
|
||||
return timespec_failure (ret);
|
||||
return time_parse_failure ();
|
||||
}
|
||||
|
||||
if (!string_to_timespec_secs (content, &ret))
|
||||
if (!string_to_time64 (content, &ret))
|
||||
{
|
||||
g_free (content);
|
||||
return timespec_failure (ret);
|
||||
return time_parse_failure ();
|
||||
}
|
||||
g_free (content);
|
||||
seen_s = TRUE;
|
||||
}
|
||||
}
|
||||
else if (g_strcmp0 ("ts:ns", (char*)n->name) == 0)
|
||||
{
|
||||
if (seen_ns)
|
||||
{
|
||||
return timespec_failure (ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
gchar* content = dom_tree_to_text (n);
|
||||
if (!content)
|
||||
{
|
||||
return timespec_failure (ret);
|
||||
}
|
||||
|
||||
if (!string_to_timespec_nsecs (content, &ret))
|
||||
{
|
||||
g_free (content);
|
||||
return timespec_failure (ret);
|
||||
}
|
||||
g_free (content);
|
||||
seen_ns = TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
PERR ("unexpected sub-node.");
|
||||
return timespec_failure (ret);
|
||||
return time_parse_failure ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -624,7 +591,7 @@ dom_tree_to_timespec (xmlNodePtr node)
|
||||
if (!seen_s)
|
||||
{
|
||||
PERR ("no ts:date node found.");
|
||||
return timespec_failure (ret);
|
||||
return time_parse_failure ();
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -895,6 +862,16 @@ dom_tree_generic_parse (xmlNodePtr node, struct dom_tree_handler* handlers,
|
||||
return successful;
|
||||
}
|
||||
|
||||
gboolean
|
||||
dom_tree_valid_time64 (time64 val, const xmlChar * name)
|
||||
{
|
||||
if (val)
|
||||
return TRUE;
|
||||
g_warning ("Invalid timestamp in data file. Look for a '%s' entry "
|
||||
"with a date of 1969-12-31 or 1970-01-01.", name);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
dom_tree_valid_timespec (Timespec* ts, const xmlChar* name)
|
||||
{
|
||||
|
@@ -42,8 +42,9 @@ gnc_commodity* dom_tree_to_commodity_ref_no_engine (xmlNodePtr node, QofBook*);
|
||||
GList* dom_tree_freqSpec_to_recurrences (xmlNodePtr node, QofBook* book);
|
||||
Recurrence* dom_tree_to_recurrence (xmlNodePtr node);
|
||||
|
||||
Timespec dom_tree_to_timespec (xmlNodePtr node);
|
||||
time64 dom_tree_to_time64 (xmlNodePtr node);
|
||||
gboolean dom_tree_valid_timespec (Timespec* ts, const xmlChar* name);
|
||||
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);
|
||||
|
@@ -363,16 +363,9 @@ simple_chars_only_parser_new (sixtp_end_handler end_handler)
|
||||
*/
|
||||
|
||||
gboolean
|
||||
string_to_timespec_secs (const gchar* str, Timespec* ts)
|
||||
string_to_time64 (const gchar* str, time64* time)
|
||||
{
|
||||
*ts = gnc_iso8601_to_timespec_gmt (str);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
gboolean
|
||||
string_to_timespec_nsecs (const gchar* str, Timespec* ts)
|
||||
{
|
||||
/* We don't do nanoseconds anymore. */
|
||||
*time = gnc_iso8601_to_time64_gmt (str);
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
@@ -459,7 +452,7 @@ generic_timespec_secs_end_handler (gpointer data_for_children,
|
||||
txt = concatenate_child_result_chars (data_from_children);
|
||||
g_return_val_if_fail (txt, FALSE);
|
||||
|
||||
ok = string_to_timespec_secs (txt, & (info->ts));
|
||||
ok = string_to_time64 (txt, & (info->ts.tv_sec));
|
||||
g_free (txt);
|
||||
|
||||
g_return_val_if_fail (ok, FALSE);
|
||||
@@ -491,22 +484,7 @@ generic_timespec_nsecs_end_handler (gpointer data_for_children,
|
||||
gpointer parent_data, gpointer global_data,
|
||||
gpointer* result, const gchar* tag)
|
||||
{
|
||||
gchar* txt = NULL;
|
||||
TimespecParseInfo* info = (TimespecParseInfo*) parent_data;
|
||||
gboolean ok;
|
||||
|
||||
g_return_val_if_fail (parent_data, FALSE);
|
||||
|
||||
txt = concatenate_child_result_chars (data_from_children);
|
||||
g_return_val_if_fail (txt, FALSE);
|
||||
|
||||
ok = string_to_timespec_nsecs (txt, & (info->ts));
|
||||
g_free (txt);
|
||||
|
||||
g_return_val_if_fail (ok, FALSE);
|
||||
|
||||
info->ns_block_count++;
|
||||
return (TRUE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static sixtp*
|
||||
|
@@ -85,8 +85,7 @@ gboolean generic_return_chars_end_handler (gpointer data_for_children,
|
||||
|
||||
sixtp* simple_chars_only_parser_new (sixtp_end_handler end_handler);
|
||||
|
||||
gboolean string_to_timespec_secs (const gchar* str, Timespec* ts);
|
||||
gboolean string_to_timespec_nsecs (const gchar* str, Timespec* ts);
|
||||
gboolean string_to_time64 (const gchar* str, time64* ts);
|
||||
|
||||
gboolean generic_timespec_start_handler (GSList* sibling_data,
|
||||
gpointer parent_data,
|
||||
|
@@ -41,51 +41,26 @@ main (int argc, char** argv)
|
||||
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
Timespec* spec1;
|
||||
Timespec spec2;
|
||||
gchar* sec_str;
|
||||
gchar* nsec_str;
|
||||
|
||||
spec1 = get_random_timespec ();
|
||||
|
||||
sec_str = timespec_sec_to_string (spec1);
|
||||
nsec_str = timespec_nsec_to_string (spec1);
|
||||
|
||||
if (!string_to_timespec_secs (sec_str, &spec2))
|
||||
time64 spec2;
|
||||
auto spec1 = get_random_time ();
|
||||
auto sec_str = time64_to_string (spec1);
|
||||
if (!string_to_time64 (sec_str, &spec2))
|
||||
{
|
||||
failure_args ("string_to_timespec_secs", __FILE__, __LINE__,
|
||||
"string is %s", sec_str);
|
||||
}
|
||||
|
||||
else if (!string_to_timespec_nsecs (nsec_str, &spec2))
|
||||
{
|
||||
failure_args ("string_to_timespec_nsecs", __FILE__, __LINE__,
|
||||
"string is %s", nsec_str);
|
||||
}
|
||||
|
||||
else if (spec1->tv_sec != spec2.tv_sec)
|
||||
else if (spec1 != spec2)
|
||||
{
|
||||
failure_args ("timespec_secs", __FILE__, __LINE__,
|
||||
"not equal ints are %" G_GINT64_FORMAT
|
||||
" and %" G_GINT64_FORMAT "\n",
|
||||
spec1->tv_sec, spec2.tv_sec);
|
||||
spec1, spec2);
|
||||
}
|
||||
|
||||
else if (spec1->tv_nsec != spec2.tv_nsec)
|
||||
{
|
||||
failure_args ("timespec_nsecs", __FILE__, __LINE__,
|
||||
"not equal ints are %ld and %ld\n",
|
||||
spec1->tv_nsec, spec2.tv_nsec);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success ("timespec");
|
||||
}
|
||||
|
||||
g_free (spec1);
|
||||
g_free (sec_str);
|
||||
g_free (nsec_str);
|
||||
}
|
||||
print_test_results ();
|
||||
exit (get_rv ());
|
||||
|
@@ -132,17 +132,11 @@ test_dom_tree_to_timespec (void)
|
||||
int i;
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
Timespec* test_spec1;
|
||||
Timespec test_spec2;
|
||||
xmlNodePtr test_node;
|
||||
|
||||
test_spec1 = get_random_timespec ();
|
||||
|
||||
test_node = timespec_to_dom_tree ("test-spec", test_spec1);
|
||||
|
||||
test_spec2 = dom_tree_to_timespec (test_node);
|
||||
|
||||
if (!dom_tree_valid_timespec (&test_spec2, (const xmlChar*)"test-spec"))
|
||||
time64 test_spec1 = get_random_time ();
|
||||
test_node = time64_to_dom_tree ("test-spec", test_spec1);
|
||||
time64 test_spec2 = dom_tree_to_time64 (test_node);
|
||||
if (!dom_tree_valid_time64 (test_spec2, (const xmlChar*)"test-spec"))
|
||||
{
|
||||
failure_args ("dom_tree_to_timespec",
|
||||
__FILE__, __LINE__, "NULL return");
|
||||
@@ -150,8 +144,7 @@ test_dom_tree_to_timespec (void)
|
||||
xmlElemDump (stdout, NULL, test_node);
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
else if (timespec_cmp (test_spec1, &test_spec2) == 0)
|
||||
else if (test_spec1 == test_spec2)
|
||||
{
|
||||
success ("dom_tree_to_timespec");
|
||||
}
|
||||
@@ -161,15 +154,9 @@ test_dom_tree_to_timespec (void)
|
||||
printf ("Node looks like:\n");
|
||||
xmlElemDump (stdout, NULL, test_node);
|
||||
printf ("\n");
|
||||
printf ("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ",
|
||||
test_spec1->tv_sec,
|
||||
test_spec2.tv_sec);
|
||||
printf ("NSecs are %ld vs %ld\n",
|
||||
test_spec1->tv_nsec,
|
||||
test_spec2.tv_nsec);
|
||||
printf ("passed: %" G_GUINT64_FORMAT " got: %" G_GUINT64_FORMAT ".\n",
|
||||
test_spec1, test_spec2);
|
||||
}
|
||||
|
||||
g_free (test_spec1);
|
||||
xmlFreeNode (test_node);
|
||||
}
|
||||
}
|
||||
|
@@ -279,16 +279,8 @@ equals_node_val_vs_kvp_frame (xmlNodePtr node, const KvpFrame* frm)
|
||||
gboolean
|
||||
equals_node_val_vs_date (xmlNodePtr node, const Timespec tm)
|
||||
{
|
||||
Timespec tm_test = dom_tree_to_timespec (node);
|
||||
|
||||
if (tm_test.tv_sec == tm.tv_sec && tm_test.tv_nsec == tm.tv_nsec)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
time64 time = dom_tree_to_time64 (node);
|
||||
return time == tm.tv_sec;
|
||||
}
|
||||
|
||||
/***********************************************************************/
|
||||
|
Reference in New Issue
Block a user