-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser_test.c
More file actions
77 lines (69 loc) · 2.22 KB
/
parser_test.c
File metadata and controls
77 lines (69 loc) · 2.22 KB
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
/**
* A C implementation of parsing a FamilySearch GEDCOM 7 file into a tree.
*/
#include "parser.h"
#include <stdlib.h>
#include <stdio.h>
static int dialect = 7;
void dumpStructure(GedStructure *s, int level) {
if (!s) return;
printf("%d", level);
if (s->id) printf(" @%s@", s->id);
printf(" %s", s->tag);
if (s->payloadType == GEDC_PAYLOAD_STRING) {
char *p = strchr(s->string, '\n');
if (p) *p = '\0';
if (s->string[0] == '@' && (dialect >= 7 || s->string[1] != '#')) printf(" @%s", s->string);
else printf(" %s", s->string);
while (p) {
*p = '\n';
p += 1;
char *p2 = strchr(p, '\n');
if (p2) *p2 = '\0';
printf("\n%d CONT %s%s", level+1, (*p=='@'?"@":""), p);
p = p2;
}
}
else if (s->payloadType == GEDC_PAYLOAD_POINTER) {
if (s->pointer) printf(" @%s@", s->pointer->id);
else printf(" @VOID@");
}
printf("\n");
dumpStructure(s->firstChild, level+1);
dumpStructure(s->nextSibling, level);
}
int main(int argc, const char *argv[]) {
if (argc <= 1) {
printf("USAGE: %s filename.ged\n", argv[0]);
printf("USAGE: %s -[L] filename.ged\n where [L] is a 1-digit integer (usually 1, 5, or 7)", argv[0]);
return 1;
}
int i = 1;
if (argv[i][0] == '-' && '0' <= argv[i][1] && argv[i][1] <= '9' && !argv[i][2]) {
dialect = argv[i][1] - '0';
i += 1;
}
for(; i<argc; i+=1) {
FILE *f = fopen(argv[i], "r");
if (!f) {
perror(argv[i]);
continue;
}
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
fseek(f, 0, SEEK_SET);
char *text = malloc((size+1)*sizeof(char));
size = fread(text, sizeof(char), size, f) * sizeof(char);
text[size] = '\0';
const char *errmsg = 0; size_t errline;
GedStructure *s = parseGEDCOM(text, dialect, &errmsg, &errline);
fclose(f);
if (errmsg) fprintf(stderr, "ERROR(%s %zd): %s\n", argv[i], errline, errmsg);
else {
printf(""); // BOM
dumpStructure(s, 0);
}
freeGedStructure(s);
free(text);
}
}