/
sixtp-utils.c
699 lines (576 loc) · 19.3 KB
/
sixtp-utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/********************************************************************
* sixtp-utils.c *
* Copyright (c) 2001 Gnumatic, Inc. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
********************************************************************/
#define __EXTENSIONS__
#include "config.h"
#include <ctype.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sixtp.h"
#include "sixtp-utils.h"
#include <time.h>
#include <errno.h>
#ifdef GNUCASH_MAJOR_VERSION
#ifndef HAVE_STRPTIME
#include "strptime.h"
#endif
#include <gnc-date.h>
#endif
static QofLogModule log_module = GNC_MOD_IO;
gboolean
isspace_str(const gchar *str, int nomorethan)
{
const gchar *cursor = str;
while (*cursor && (nomorethan != 0))
{
if (!isspace(*cursor))
{
return(FALSE);
}
cursor++;
nomorethan--;
}
return(TRUE);
}
gboolean
allow_and_ignore_only_whitespace(GSList *sibling_data,
gpointer parent_data,
gpointer global_data,
gpointer *result,
const char *text,
int length)
{
return(isspace_str(text, length));
}
gboolean
generic_accumulate_chars(GSList *sibling_data,
gpointer parent_data,
gpointer global_data,
gpointer *result,
const char *text,
int length)
{
gchar *copytxt = g_strndup(text, length);
g_return_val_if_fail(result, FALSE);
*result = copytxt;
return(TRUE);
}
void
generic_free_data_for_children(gpointer data_for_children,
GSList* data_from_children,
GSList* sibling_data,
gpointer parent_data,
gpointer global_data,
gpointer *result,
const gchar *tag)
{
if (data_for_children) g_free(data_for_children);
}
gchar *
concatenate_child_result_chars(GSList *data_from_children)
{
GSList *lp;
gchar *name = g_strdup("");
g_return_val_if_fail(name, NULL);
/* child data lists are in reverse chron order */
data_from_children = g_slist_reverse(g_slist_copy(data_from_children));
for (lp = data_from_children; lp; lp = lp->next)
{
sixtp_child_result *cr = (sixtp_child_result *) lp->data;
if (cr->type != SIXTP_CHILD_RESULT_CHARS)
{
PERR ("result type is not chars");
g_slist_free (data_from_children);
g_free(name);
return(NULL);
}
else
{
char *temp;
temp = g_strconcat(name, (gchar *) cr->data, NULL);
g_free (name);
name = temp;
}
}
g_slist_free (data_from_children);
return(name);
}
/****************************************************************************/
/* string to data converters...
*/
/*********/
/* double
*/
gboolean
string_to_double(const char *str, double *result)
{
char *endptr = 0x0;
g_return_val_if_fail(str, FALSE);
g_return_val_if_fail(result, FALSE);
*result = strtod (str, &endptr);
if (endptr == str) return (FALSE);
return(TRUE);
}
/*********/
/* gint64
*/
/* Maybe there should be a comment here explaining why this function
doesn't call g_ascii_strtoull, because it's not so obvious. -CAS */
gboolean
string_to_gint64(const gchar *str, gint64 *v)
{
/* convert a string to a gint64. only whitespace allowed before and after. */
long long int v_in;
int num_read;
g_return_val_if_fail(str, FALSE);
/* must use "<" here because %n's effects aren't well defined */
if (sscanf(str, " " QOF_SCANF_LLD "%n", &v_in, &num_read) < 1)
{
return(FALSE);
}
/*
* Mac OS X version 10.1 and under has a silly bug where scanf
* returns bad values in num_read if there is a space before %n. It
* is fixed in the next release 10.2 afaik
*/
while ( (*((gchar*)str + num_read) != '\0') &&
isspace(*((unsigned char*)str + num_read)))
num_read++;
if (v)
*v = v_in;
if (!isspace_str(str + num_read, -1)) return(FALSE);
return(TRUE);
}
/*********/
/* gint32
*/
gboolean
string_to_gint32(const gchar *str, gint32 *v)
{
/* convert a string to a gint32. only whitespace allowed before and after. */
int num_read;
int v_in;
/* must use "<" here because %n's effects aren't well defined */
if (sscanf(str, " %d%n", &v_in, &num_read) < 1)
{
return(FALSE);
}
while ( (*((gchar*)str + num_read) != '\0') &&
isspace(*((unsigned char*)str + num_read)))
num_read++;
if (v)
*v = v_in;
if (!isspace_str(str + num_read, -1)) return(FALSE);
return(TRUE);
}
/************/
/* hex string
*/
gboolean
hex_string_to_binary(const gchar *str, void **v, guint64 *data_len)
{
/* Convert a hex string to binary. No whitespace allowed. */
const gchar *cursor = str;
guint64 str_len;
gboolean error = FALSE;
g_return_val_if_fail(str, FALSE);
g_return_val_if_fail(v, FALSE);
g_return_val_if_fail(data_len, FALSE);
str_len = strlen(str);
/* Since no whitespace is allowed and hex encoding is 2 text chars
per binary char, the result must be half the input size and the
input size must be even. */
if ((str_len % 2) != 0) return(FALSE);
*data_len = 0;
*v = g_new0(char, str_len / 2);
g_return_val_if_fail(*v, FALSE);
while (*cursor && *(cursor + 1))
{
gchar tmpstr[2];
int tmpint;
if (isspace(*cursor) || isspace(*(cursor + 1)))
{
error = TRUE;
}
else
{
int num_read;
tmpstr[0] = *cursor;
tmpstr[0] = *(cursor + 1);
if ((sscanf(tmpstr, "%x%n", &tmpint, &num_read) < 1)
|| (num_read != 2))
{
error = TRUE;
}
else
{
*((gchar *) (v + *data_len)) = tmpint;
*data_len += 1;
cursor += 2;
}
}
}
if (error || (*data_len != (str_len / 2)))
{
g_free(*v);
*v = NULL;
*data_len = 0;
return(FALSE);
}
return(TRUE);
}
/***************************************************************************/
/* simple chars only parser - just grabs all it's contained chars and
does what you specify in the end handler - if you pass NULL as the
end handler to simple_chars_only_parser_new, the characters are just
passed to the parent as a new string.
input: NA
returns: gchar array allocated via g_new, etc.
start: NA
chars: generic_accumulate_chars.
end: varies - default is to concatenate all accumulated chars and return.
cleanup-result: g_free (for chars)
cleanup-chars: g_free (for chars)
fail: NA
result-fail: g_free (for chars)
chars-fail: g_free (for chars)
*/
gboolean
generic_return_chars_end_handler(gpointer data_for_children,
GSList* data_from_children,
GSList* sibling_data,
gpointer parent_data,
gpointer global_data,
gpointer *result,
const gchar *tag)
{
gchar *txt = NULL;
txt = concatenate_child_result_chars(data_from_children);
g_return_val_if_fail(txt, FALSE);
*result = txt;
return(TRUE);
}
sixtp*
simple_chars_only_parser_new(sixtp_end_handler end_handler)
{
return sixtp_set_any(
sixtp_new(), FALSE,
SIXTP_END_HANDLER_ID, (end_handler
? end_handler
: generic_return_chars_end_handler),
SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
}
/****************************************************************************/
/* generic timespec handler.
A collection of node functions intended to parse a sub-node set
that looks like this:
<date-posted>
<s>Mon, 05 Jun 2000 23:16:19 -0500</s>
<ns>658864000</ns>
</date-posted>
and produce a Timespec*. The start handler for the top allocates
the Timespec * and passes it to the children. The <s> block sets
the seconds and the <ns> block (if any) sets the nanoseconds. If
all goes well, returns the Timespec* as the result.
*/
gboolean
string_to_timespec_secs(const gchar *str, Timespec *ts)
{
*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. */
return(TRUE);
}
/* Top level timespec node:
input: user end handler *
returns: Timespec*
start: Allocates TimespecParseInfo* for data_for_children.
characters: none (whitespace only).
end: g_free TimespecParseInfo + any other actions
cleanup-result: NA
cleanup-chars: NA
fail: g_free data_for_children.
result-fail: g_free data_for_children.
chars-fail: NA
*/
gboolean
generic_timespec_start_handler(GSList* sibling_data, gpointer parent_data,
gpointer global_data,
gpointer *data_for_children, gpointer *result,
const gchar *tag, gchar **attrs)
{
TimespecParseInfo *tsp = g_new0(TimespecParseInfo, 1);
g_return_val_if_fail(tsp, FALSE);
*data_for_children = tsp;
return(TRUE);
}
/* You can't use this function directly. You have to call it from
your own end handler. If it returns TRUE, *result will contain the
new timespec. Otherwise, you can presume that everything's been
cleaned up properly and return FALSE. */
gboolean
timespec_parse_ok(TimespecParseInfo *info)
{
if ((info->s_block_count > 1) || (info->ns_block_count > 1) ||
((info->s_block_count == 0) && (info->ns_block_count == 0)))
{
return(FALSE);
}
else
{
return(TRUE);
}
}
/* generic_timespec_end_handler - must be customized and provided by
the user. */
/* <s> (parent timespec-node)
input: TimespecParseInfo *
returns: NA
start: NA
characters: accumulate.
end: convert characters to secs part of input Timespec and inc s_block_count.
cleanup-result: NA
cleanup-chars: g_free data.
fail: NA
result-fail: NA
chars-fail: g_free data.
*/
gboolean
generic_timespec_secs_end_handler(gpointer data_for_children,
GSList *data_from_children, GSList *sibling_data,
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_secs(txt, &(info->ts));
g_free(txt);
g_return_val_if_fail(ok, FALSE);
info->s_block_count++;
return(TRUE);
}
/* <s> (parent timespec-node)
input: TimespecParseInfo *
returns: NA
start: NA
characters: accumulate.
end: convert characters to secs part of input Timespec and inc s_block_count.
cleanup-result: NA
cleanup-chars: g_free data.
fail: NA
result-fail: NA
chars-fail: g_free data.
*/
gboolean
generic_timespec_nsecs_end_handler(gpointer data_for_children,
GSList *data_from_children, GSList *sibling_data,
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);
}
static sixtp*
timespec_sixtp_new(sixtp_end_handler ender)
{
return sixtp_set_any(
sixtp_new(), FALSE,
SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
SIXTP_END_HANDLER_ID, ender,
SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
}
sixtp *
generic_timespec_parser_new(sixtp_end_handler end_handler)
{
sixtp *top_level =
sixtp_set_any(sixtp_new(), FALSE,
SIXTP_START_HANDLER_ID, generic_timespec_start_handler,
SIXTP_CHARACTERS_HANDLER_ID, allow_and_ignore_only_whitespace,
SIXTP_END_HANDLER_ID, end_handler,
SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
SIXTP_FAIL_HANDLER_ID, generic_free_data_for_children,
SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
g_return_val_if_fail(top_level, NULL);
if (!sixtp_add_some_sub_parsers(
top_level, TRUE,
"s", timespec_sixtp_new(generic_timespec_secs_end_handler),
"ns", timespec_sixtp_new(generic_timespec_nsecs_end_handler),
NULL, NULL))
{
return NULL;
}
return(top_level);
}
/****************************************************************************/
/* <?> generic guid handler...
Attempts to parse the current accumulated characters data as a guid
and return it.
input: NA
returns: GncGUID*
start: NA
characters: return string copy for accumulation in end handler.
end: concatenate all chars and create and return GncGUID*, if possible.
cleanup-result: g_free the GncGUID*
cleanup-chars: g_free the result string.
fail: NA
result-fail: g_free the GncGUID*
chars-fail: g_free the result string.
*/
gboolean
generic_guid_end_handler(gpointer data_for_children,
GSList *data_from_children, GSList *sibling_data,
gpointer parent_data, gpointer global_data,
gpointer *result, const gchar *tag)
{
gchar *txt = NULL;
GncGUID *gid;
gboolean ok;
txt = concatenate_child_result_chars(data_from_children);
g_return_val_if_fail(txt, FALSE);
gid = g_new(GncGUID, 1);
if (!gid)
{
g_free(txt);
return(FALSE);
}
ok = string_to_guid(txt, gid);
g_free(txt);
if (!ok)
{
PERR ("couldn't parse GncGUID");
g_free(gid);
return(FALSE);
}
*result = gid;
return(TRUE);
}
sixtp*
generic_guid_parser_new(void)
{
return sixtp_set_any(
sixtp_new(), FALSE,
SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
SIXTP_END_HANDLER_ID, generic_guid_end_handler,
SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
}
/****************************************************************************/
/* <?> generic gnc_numeric handler...
Attempts to parse the current accumulated characters data as a
gnc_numeric and return it.
input: NA
returns: gnc_numeric*
start: NA
characters: return string copy for accumulation in end handler.
end: concatenate all chars and create and return gnc_numeric*, if possible.
cleanup-result: g_free the gnc_numeric*
cleanup-chars: g_free the result string.
fail: NA
result-fail: g_free the gnc_numeric*
chars-fail: g_free the result string.
*/
gboolean
generic_gnc_numeric_end_handler(gpointer data_for_children,
GSList *data_from_children, GSList *sibling_data,
gpointer parent_data, gpointer global_data,
gpointer *result, const gchar *tag)
{
gnc_numeric *num = NULL;
gchar *txt = NULL;
gboolean ok = FALSE;
txt = concatenate_child_result_chars(data_from_children);
if (txt)
{
num = g_new(gnc_numeric, 1);
if (num)
{
if (string_to_gnc_numeric(txt, num))
{
ok = TRUE;
*result = num;
}
}
}
g_free(txt);
if (!ok)
{
PERR ("couldn't parse numeric quantity");
g_free(num);
}
return(ok);
}
sixtp*
generic_gnc_numeric_parser_new(void)
{
return sixtp_set_any(
sixtp_new(), FALSE,
SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
SIXTP_END_HANDLER_ID, generic_gnc_numeric_end_handler,
SIXTP_RESULT_FAIL_ID, sixtp_child_free_data,
SIXTP_CLEANUP_RESULT_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
}
/***************************************************************************/
sixtp*
restore_char_generator(sixtp_end_handler ender)
{
return sixtp_set_any(
sixtp_new(), FALSE,
SIXTP_CHARACTERS_HANDLER_ID, generic_accumulate_chars,
SIXTP_END_HANDLER_ID, ender,
SIXTP_CLEANUP_CHARS_ID, sixtp_child_free_data,
SIXTP_CHARS_FAIL_ID, sixtp_child_free_data,
SIXTP_NO_MORE_HANDLERS);
}
/***************************** END OF FILE *********************************/