GnuCash 7574033+
Loading...
Searching...
No Matches
sixtp-utils.cpp
1/********************************************************************
2 * sixtp-utils.c *
3 * Copyright (c) 2001 Gnumatic, Inc. *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21 * *
22 ********************************************************************/
23
24#include <guid.hpp>
25#include <config.h>
26
27#include <ctype.h>
28#include <glib.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include <time.h>
34#include <errno.h>
35
36#ifndef HAVE_STRPTIME
37#include "strptime.h"
38#endif
39#include <gnc-date.h>
40
41#include "sixtp.h"
42#include "sixtp-utils.h"
43
44#include <charconv>
45#include <cctype>
46
47static QofLogModule log_module = GNC_MOD_IO;
48
49gboolean
50isspace_str (const gchar* str, int nomorethan)
51{
52 const gchar* cursor = str;
53 while (*cursor && (nomorethan != 0))
54 {
55 if (!isspace (*cursor))
56 {
57 return (FALSE);
58 }
59 cursor++;
60 nomorethan--;
61 }
62 return (TRUE);
63}
64
65gboolean
66allow_and_ignore_only_whitespace (GSList* sibling_data,
67 gpointer parent_data,
68 gpointer global_data,
69 gpointer* result,
70 const char* text,
71 int length)
72{
73 return (isspace_str (text, length));
74}
75
76gboolean
77generic_accumulate_chars (GSList* sibling_data,
78 gpointer parent_data,
79 gpointer global_data,
80 gpointer* result,
81
82 const char* text,
83 int length)
84{
85 gchar* copytxt = g_strndup (text, length);
86 g_return_val_if_fail (result, FALSE);
87
88 *result = copytxt;
89 return (TRUE);
90}
91
92
93void
94generic_free_data_for_children (gpointer data_for_children,
95 GSList* data_from_children,
96 GSList* sibling_data,
97 gpointer parent_data,
98 gpointer global_data,
99 gpointer* result,
100 const gchar* tag)
101{
102 if (data_for_children) g_free (data_for_children);
103}
104
105gchar*
106concatenate_child_result_chars (GSList* data_from_children)
107{
108 GSList* lp;
109 gchar* name = g_strdup ("");
110
111 g_return_val_if_fail (name, NULL);
112
113 /* child data lists are in reverse chron order */
114 data_from_children = g_slist_reverse (g_slist_copy (data_from_children));
115
116 for (lp = data_from_children; lp; lp = lp->next)
117 {
118 sixtp_child_result* cr = (sixtp_child_result*) lp->data;
119 if (cr->type != SIXTP_CHILD_RESULT_CHARS)
120 {
121 PERR ("result type is not chars");
122 g_slist_free (data_from_children);
123 g_free (name);
124 return (NULL);
125 }
126 else
127 {
128 char* temp;
129 temp = g_strconcat (name, (gchar*) cr->data, nullptr);
130 g_free (name);
131 name = temp;
132 }
133 }
134 g_slist_free (data_from_children);
135 return (name);
136}
137
138/****************************************************************************/
139/* string to data converters...
140 */
141
142
143template <typename T>
144static bool parse_chars_into_num (std::string_view sv, T* num_ptr)
145{
146 if (!num_ptr)
147 return false;
148
149 while (!sv.empty() && std::isspace (sv.front ())) sv.remove_prefix (1);
150 while (!sv.empty() && std::isspace (sv.back ())) sv.remove_suffix (1);
151
152 auto res = std::from_chars (sv.begin(), sv.end(), *num_ptr);
153 return (res.ec == std::errc{} && res.ptr == sv.end());
154}
155
156/*********/
157/* double
158 */
159
160gboolean
161string_to_double (std::string_view sv, double* result)
162{
163#if __cpp_lib_to_chars >= 201611L
164 return parse_chars_into_num<double>(sv, result);
165#else
166 // because from_chars in cpp < 201611L cannot parse floats
167 g_return_val_if_fail (result, false);
168 std::string str{sv};
169 char* endptr = nullptr;
170 *result = std::strtod (str.c_str(), &endptr);
171 return (endptr != str.c_str());
172#endif
173}
174
175/*********/
176/* gint64
177 */
178gboolean
179string_to_gint64 (std::string_view sv, gint64* v)
180{
181 return parse_chars_into_num<gint64>(sv, v);
182}
183
184/*********/
185/* guint16
186 */
187gboolean
188string_to_guint16 (std::string_view sv, guint16* v)
189{
190 return parse_chars_into_num<guint16>(sv, v);
191}
192
193/*********/
194/* guint
195 */
196gboolean
197string_to_guint (std::string_view sv, guint* v)
198{
199 return parse_chars_into_num<guint>(sv, v);
200}
201
202/***************************************************************************/
203/* simple chars only parser - just grabs all it's contained chars and
204 does what you specify in the end handler - if you pass NULL as the
205 end handler to simple_chars_only_parser_new, the characters are just
206 passed to the parent as a new string.
207
208 input: NA
209 returns: gchar array allocated via g_new, etc.
210
211 start: NA
212 chars: generic_accumulate_chars.
213 end: varies - default is to concatenate all accumulated chars and return.
214
215 cleanup-result: g_free (for chars)
216 cleanup-chars: g_free (for chars)
217 fail: NA
218 result-fail: g_free (for chars)
219 chars-fail: g_free (for chars)
220
221 */
222
223gboolean
224generic_return_chars_end_handler (gpointer data_for_children,
225 GSList* data_from_children,
226 GSList* sibling_data,
227 gpointer parent_data,
228 gpointer global_data,
229 gpointer* result,
230 const gchar* tag)
231{
232 gchar* txt = NULL;
233
234 txt = concatenate_child_result_chars (data_from_children);
235 g_return_val_if_fail (txt, FALSE);
236 *result = txt;
237 return (TRUE);
238}
239
240
241sixtp*
242simple_chars_only_parser_new (sixtp_end_handler end_handler)
243{
244 return sixtp_set_any (
245 sixtp_new (), FALSE,
246 SIXTP_END_HANDLER_ID, (end_handler
247 ? end_handler
248 : generic_return_chars_end_handler),
249 SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
250 SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
251 SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
252 SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
253 SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
254 SIXTP_NO_MORE_HANDLERS);
255}
256
257/****************************************************************************/
258/* generic timespec handler for XML Version 1 files.
259
260 A collection of node functions intended to parse a sub-node set
261 that looks like this:
262
263 <date-posted>
264 <s>Mon, 05 Jun 2000 23:16:19 -0500</s>
265 <ns>658864000</ns>
266 </date-posted>
267
268 and produce a time64. The start handler for the top allocates
269 the time64 and passes it to the children. The <s> block sets
270 the seconds and the <ns> block is ignored. If
271 all goes well, returns the time64 as the result.
272*/
273
274/* Top level timespec node:
275
276 input: user end handler *
277 returns: time64
278
279 start: Allocates Time64ParseInfo* for data_for_children.
280 characters: none (whitespace only).
281 end: g_free Time64ParseInfo + any other actions
282
283 cleanup-result: NA
284 cleanup-chars: NA
285 fail: g_free data_for_children.
286 result-fail: g_free data_for_children.
287 chars-fail: NA
288
289 */
290
291gboolean
292generic_timespec_start_handler (GSList* sibling_data, gpointer parent_data,
293 gpointer global_data,
294 gpointer* data_for_children, gpointer* result,
295 const gchar* tag, gchar** attrs)
296{
297 Time64ParseInfo* tsp = g_new0 (Time64ParseInfo, 1);
298 g_return_val_if_fail (tsp, FALSE);
299 *data_for_children = tsp;
300 return (TRUE);
301}
302
303/* You can't use this function directly. You have to call it from
304 your own end handler. If it returns TRUE, *result will contain the
305 new timespec. Otherwise, you can presume that everything's been
306 cleaned up properly and return FALSE. */
307gboolean
308timespec_parse_ok (Time64ParseInfo* info)
309{
310 return info->s_block_count != 1;
311}
312
313/* generic_timespec_end_handler - must be customized and provided by
314 the user. */
315
316/* <s> (parent timespec-node)
317
318 input: Time64ParseInfo *
319 returns: NA
320
321 start: NA
322 characters: accumulate.
323 end: convert characters to secs part of input time64 and inc s_block_count.
324
325 cleanup-result: NA
326 cleanup-chars: g_free data.
327 fail: NA
328 result-fail: NA
329 chars-fail: g_free data.
330
331 */
332
333gboolean
334generic_timespec_secs_end_handler (gpointer data_for_children,
335 GSList* data_from_children, GSList* sibling_data,
336 gpointer parent_data, gpointer global_data,
337 gpointer* result, const gchar* tag)
338{
339 gchar* txt = NULL;
340 Time64ParseInfo* info = (Time64ParseInfo*) parent_data;
341
342 g_return_val_if_fail (parent_data, FALSE);
343
344 txt = concatenate_child_result_chars (data_from_children);
345 g_return_val_if_fail (txt, FALSE);
346
347 info->time = gnc_iso8601_to_time64_gmt (txt);
348 g_free (txt);
349
350// gnc_iso8601_to_time64_gmt returns INT64_MAX on failure.
351 g_return_val_if_fail (info->time < INT64_MAX, FALSE);
352
353 info->s_block_count++;
354 return (TRUE);
355}
356
357/* <s> (parent timespec-node)
358
359 input: Time64ParseInfo *
360 returns: NA
361
362 start: NA
363 characters: accumulate.
364 end: NA
365
366 cleanup-result: NA
367 cleanup-chars: NA.
368 fail: NA
369 result-fail: NA
370 chars-fail: g_free data.
371
372 */
373
374gboolean
375generic_timespec_nsecs_end_handler (gpointer data_for_children,
376 GSList* data_from_children, GSList* sibling_data,
377 gpointer parent_data, gpointer global_data,
378 gpointer* result, const gchar* tag)
379{
380 return TRUE;
381}
382
383static sixtp*
384timespec_sixtp_new (sixtp_end_handler ender)
385{
386 return sixtp_set_any (
387 sixtp_new (), FALSE,
388 SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
389 SIXTP_END_HANDLER_ID, ender,
390 SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
391 SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
392 SIXTP_NO_MORE_HANDLERS);
393}
394
395sixtp*
396generic_timespec_parser_new (sixtp_end_handler end_handler)
397{
398 sixtp* top_level =
399 sixtp_set_any (sixtp_new (), FALSE,
400 SIXTP_START_HANDLER_ID, generic_timespec_start_handler,
401 SIXTP_CHARACTERS_HANDLER_ID, allow_and_ignore_only_whitespace,
402 SIXTP_END_HANDLER_ID, end_handler,
403 SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
404 SIXTP_FAIL_HANDLER_ID, generic_free_data_for_children,
405 SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
406 SIXTP_NO_MORE_HANDLERS);
407 g_return_val_if_fail (top_level, NULL);
408
409 if (!sixtp_add_some_sub_parsers (
410 top_level, TRUE,
411 "s", timespec_sixtp_new (generic_timespec_secs_end_handler),
412 "ns", timespec_sixtp_new (generic_timespec_nsecs_end_handler),
413 NULL, NULL))
414 {
415 return NULL;
416 }
417
418 return (top_level);
419}
420
421/****************************************************************************/
422/* <?> generic guid handler...
423
424 Attempts to parse the current accumulated characters data as a guid
425 and return it.
426
427 input: NA
428 returns: GncGUID*
429
430 start: NA
431 characters: return string copy for accumulation in end handler.
432 end: concatenate all chars and create and return GncGUID*, if possible.
433
434 cleanup-result: g_free the GncGUID*
435 cleanup-chars: g_free the result string.
436 fail: NA
437 result-fail: g_free the GncGUID*
438 chars-fail: g_free the result string.
439
440 */
441
442gboolean
443generic_guid_end_handler (gpointer data_for_children,
444 GSList* data_from_children, GSList* sibling_data,
445 gpointer parent_data, gpointer global_data,
446 gpointer* result, const gchar* tag)
447{
448 gchar* txt = NULL;
449 GncGUID* gid;
450 gboolean ok;
451
452 txt = concatenate_child_result_chars (data_from_children);
453 g_return_val_if_fail (txt, FALSE);
454
455 gid = g_new (GncGUID, 1);
456 if (!gid)
457 {
458 g_free (txt);
459 return (FALSE);
460 }
461
462 ok = string_to_guid (txt, gid);
463 g_free (txt);
464
465 if (!ok)
466 {
467 PERR ("couldn't parse GncGUID");
468 g_free (gid);
469 return (FALSE);
470 }
471
472 *result = gid;
473 return (TRUE);
474}
475
476sixtp*
477generic_guid_parser_new (void)
478{
479 return sixtp_set_any (
480 sixtp_new (), FALSE,
481 SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
482 SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
483 SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
484 SIXTP_END_HANDLER_ID, generic_guid_end_handler,
485 SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
486 SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
487 SIXTP_NO_MORE_HANDLERS);
488}
489
490/****************************************************************************/
491/* <?> generic gnc_numeric handler...
492
493 Attempts to parse the current accumulated characters data as a
494 gnc_numeric and return it.
495
496 input: NA
497 returns: gnc_numeric*
498
499 start: NA
500 characters: return string copy for accumulation in end handler.
501 end: concatenate all chars and create and return gnc_numeric*, if possible.
502
503 cleanup-result: g_free the gnc_numeric*
504 cleanup-chars: g_free the result string.
505 fail: NA
506 result-fail: g_free the gnc_numeric*
507 chars-fail: g_free the result string.
508
509 */
510
511gboolean
512generic_gnc_numeric_end_handler (gpointer data_for_children,
513 GSList* data_from_children, GSList* sibling_data,
514 gpointer parent_data, gpointer global_data,
515 gpointer* result, const gchar* tag)
516{
517 gnc_numeric* num = NULL;
518 gchar* txt = NULL;
519 gboolean ok = FALSE;
520
521 txt = concatenate_child_result_chars (data_from_children);
522
523 if (txt)
524 {
525 num = g_new (gnc_numeric, 1);
526 if (num)
527 {
528 *num = gnc_numeric_from_string (txt);
529 if (!gnc_numeric_check (*num))
530 {
531 ok = TRUE;
532 *result = num;
533 }
534 }
535 }
536
537 g_free (txt);
538 if (!ok)
539 {
540 PERR ("couldn't parse numeric quantity");
541 g_free (num);
542 }
543
544 return (ok);
545}
546
547sixtp*
548generic_gnc_numeric_parser_new (void)
549{
550 return sixtp_set_any (
551 sixtp_new (), FALSE,
552 SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
553 SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
554 SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
555 SIXTP_END_HANDLER_ID, generic_gnc_numeric_end_handler,
556 SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
557 SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
558 SIXTP_NO_MORE_HANDLERS);
559}
560
561/***************************************************************************/
562
563sixtp*
564restore_char_generator (sixtp_end_handler ender)
565{
566 return sixtp_set_any (
567 sixtp_new (), FALSE,
568 SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
569 SIXTP_END_HANDLER_ID, ender,
570 SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
571 SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
572 SIXTP_NO_MORE_HANDLERS);
573}
574
575/***************************** END OF FILE *********************************/
Date and Time handling routines.
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
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.
Definition guid.h:75
Definition sixtp.h:130