Line data Source code
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 :
47 : static QofLogModule log_module = GNC_MOD_IO;
48 :
49 : gboolean
50 767 : isspace_str (const gchar* str, int nomorethan)
51 : {
52 767 : const gchar* cursor = str;
53 3026 : while (*cursor && (nomorethan != 0))
54 : {
55 2259 : if (!isspace (*cursor))
56 : {
57 0 : return (FALSE);
58 : }
59 2259 : cursor++;
60 2259 : nomorethan--;
61 : }
62 767 : return (TRUE);
63 : }
64 :
65 : gboolean
66 767 : allow_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 767 : return (isspace_str (text, length));
74 : }
75 :
76 : gboolean
77 0 : generic_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 0 : gchar* copytxt = g_strndup (text, length);
86 0 : g_return_val_if_fail (result, FALSE);
87 :
88 0 : *result = copytxt;
89 0 : return (TRUE);
90 : }
91 :
92 :
93 : void
94 0 : generic_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 0 : if (data_for_children) g_free (data_for_children);
103 0 : }
104 :
105 : gchar*
106 0 : concatenate_child_result_chars (GSList* data_from_children)
107 : {
108 : GSList* lp;
109 0 : gchar* name = g_strdup ("");
110 :
111 0 : g_return_val_if_fail (name, NULL);
112 :
113 : /* child data lists are in reverse chron order */
114 0 : data_from_children = g_slist_reverse (g_slist_copy (data_from_children));
115 :
116 0 : for (lp = data_from_children; lp; lp = lp->next)
117 : {
118 0 : sixtp_child_result* cr = (sixtp_child_result*) lp->data;
119 0 : if (cr->type != SIXTP_CHILD_RESULT_CHARS)
120 : {
121 0 : PERR ("result type is not chars");
122 0 : g_slist_free (data_from_children);
123 0 : g_free (name);
124 0 : return (NULL);
125 : }
126 : else
127 : {
128 : char* temp;
129 0 : temp = g_strconcat (name, (gchar*) cr->data, nullptr);
130 0 : g_free (name);
131 0 : name = temp;
132 : }
133 : }
134 0 : g_slist_free (data_from_children);
135 0 : return (name);
136 : }
137 :
138 : /****************************************************************************/
139 : /* string to data converters...
140 : */
141 :
142 :
143 : template <typename T>
144 2268 : static bool parse_chars_into_num (std::string_view sv, T* num_ptr)
145 : {
146 2268 : if (!num_ptr)
147 0 : return false;
148 :
149 2356 : while (!sv.empty() && std::isspace (sv.front ())) sv.remove_prefix (1);
150 2364 : while (!sv.empty() && std::isspace (sv.back ())) sv.remove_suffix (1);
151 :
152 2268 : auto res = std::from_chars (sv.begin(), sv.end(), *num_ptr);
153 2268 : return (res.ec == std::errc{} && res.ptr == sv.end());
154 : }
155 :
156 : /*********/
157 : /* double
158 : */
159 :
160 : gboolean
161 22 : string_to_double (std::string_view sv, double* result)
162 : {
163 : #if __cpp_lib_to_chars >= 201611L
164 22 : 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 : */
178 : gboolean
179 2205 : string_to_gint64 (std::string_view sv, gint64* v)
180 : {
181 2205 : return parse_chars_into_num<gint64>(sv, v);
182 : }
183 :
184 : /*********/
185 : /* guint16
186 : */
187 : gboolean
188 23 : string_to_guint16 (std::string_view sv, guint16* v)
189 : {
190 23 : return parse_chars_into_num<guint16>(sv, v);
191 : }
192 :
193 : /*********/
194 : /* guint
195 : */
196 : gboolean
197 18 : string_to_guint (std::string_view sv, guint* v)
198 : {
199 18 : 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 :
223 : gboolean
224 0 : generic_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 0 : gchar* txt = NULL;
233 :
234 0 : txt = concatenate_child_result_chars (data_from_children);
235 0 : g_return_val_if_fail (txt, FALSE);
236 0 : *result = txt;
237 0 : return (TRUE);
238 : }
239 :
240 :
241 : sixtp*
242 0 : simple_chars_only_parser_new (sixtp_end_handler end_handler)
243 : {
244 0 : 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 0 : 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 :
291 : gboolean
292 0 : generic_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 0 : Time64ParseInfo* tsp = g_new0 (Time64ParseInfo, 1);
298 0 : g_return_val_if_fail (tsp, FALSE);
299 0 : *data_for_children = tsp;
300 0 : 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. */
307 : gboolean
308 0 : timespec_parse_ok (Time64ParseInfo* info)
309 : {
310 0 : 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 :
333 : gboolean
334 0 : generic_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 0 : gchar* txt = NULL;
340 0 : Time64ParseInfo* info = (Time64ParseInfo*) parent_data;
341 :
342 0 : g_return_val_if_fail (parent_data, FALSE);
343 :
344 0 : txt = concatenate_child_result_chars (data_from_children);
345 0 : g_return_val_if_fail (txt, FALSE);
346 :
347 0 : info->time = gnc_iso8601_to_time64_gmt (txt);
348 0 : g_free (txt);
349 :
350 : // gnc_iso8601_to_time64_gmt returns INT64_MAX on failure.
351 0 : g_return_val_if_fail (info->time < INT64_MAX, FALSE);
352 :
353 0 : info->s_block_count++;
354 0 : 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 :
374 : gboolean
375 0 : generic_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 0 : return TRUE;
381 : }
382 :
383 : static sixtp*
384 0 : timespec_sixtp_new (sixtp_end_handler ender)
385 : {
386 0 : 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 0 : SIXTP_NO_MORE_HANDLERS);
393 : }
394 :
395 : sixtp*
396 0 : generic_timespec_parser_new (sixtp_end_handler end_handler)
397 : {
398 : sixtp* top_level =
399 0 : 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 0 : g_return_val_if_fail (top_level, NULL);
408 :
409 0 : 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 0 : return NULL;
416 : }
417 :
418 0 : 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 :
442 : gboolean
443 0 : generic_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 0 : gchar* txt = NULL;
449 : GncGUID* gid;
450 : gboolean ok;
451 :
452 0 : txt = concatenate_child_result_chars (data_from_children);
453 0 : g_return_val_if_fail (txt, FALSE);
454 :
455 0 : gid = g_new (GncGUID, 1);
456 0 : if (!gid)
457 : {
458 0 : g_free (txt);
459 0 : return (FALSE);
460 : }
461 :
462 0 : ok = string_to_guid (txt, gid);
463 0 : g_free (txt);
464 :
465 0 : if (!ok)
466 : {
467 0 : PERR ("couldn't parse GncGUID");
468 0 : g_free (gid);
469 0 : return (FALSE);
470 : }
471 :
472 0 : *result = gid;
473 0 : return (TRUE);
474 : }
475 :
476 : sixtp*
477 0 : generic_guid_parser_new (void)
478 : {
479 0 : 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 0 : 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 :
511 : gboolean
512 0 : generic_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 0 : gnc_numeric* num = NULL;
518 0 : gchar* txt = NULL;
519 0 : gboolean ok = FALSE;
520 :
521 0 : txt = concatenate_child_result_chars (data_from_children);
522 :
523 0 : if (txt)
524 : {
525 0 : num = g_new (gnc_numeric, 1);
526 0 : if (num)
527 : {
528 0 : *num = gnc_numeric_from_string (txt);
529 0 : if (!gnc_numeric_check (*num))
530 : {
531 0 : ok = TRUE;
532 0 : *result = num;
533 : }
534 : }
535 : }
536 :
537 0 : g_free (txt);
538 0 : if (!ok)
539 : {
540 0 : PERR ("couldn't parse numeric quantity");
541 0 : g_free (num);
542 : }
543 :
544 0 : return (ok);
545 : }
546 :
547 : sixtp*
548 0 : generic_gnc_numeric_parser_new (void)
549 : {
550 0 : 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 0 : SIXTP_NO_MORE_HANDLERS);
559 : }
560 :
561 : /***************************************************************************/
562 :
563 : sixtp*
564 0 : restore_char_generator (sixtp_end_handler ender)
565 : {
566 0 : 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 0 : SIXTP_NO_MORE_HANDLERS);
573 : }
574 :
575 : /***************************** END OF FILE *********************************/
|