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
|
############################################################################
#
# File: gedcom.icn
#
# Subject: Procedures for reading GEDCOM files
#
# Author: Gregg M. Townsend
#
# Date: March 25, 2002
#
############################################################################
#
# This file is in the public domain.
#
############################################################################
#
# These procedures read and interpret GEDCOM files, a standard
# format for genealogy databases.
#
############################################################################
#
# gedload(f) loads GEDCOM data from file f and returns a gedcom
# record containing the following fields:
# tree root of tree of gednode records
# id table of labeled nodes, indexed by @ID@
# fam list of FAM nodes (marriages)
# ind list of INDI nodes (individuals)
#
# The tree is composed of gednode records R containing these fields:
# level level
# id ID (label), including @...@ delimiters
# tag tag
# data data
# lnum line number
# parent parent node in tree
# ref referenced node, if any
# sub sub-entry list
# hcode unique hashcode, if INDI node
#
# gedwalk(tree) generates the nodes of the tree in preorder.
#
# Three procedures find descendants of a node based on a sequence
# of identifying tag strings:
# gedsub(R, tag...) generates subnodes specified by tag sequence
# gedval(R, tag...) generates data values of those subnodes
# gedref(R, tag...) generates nodes referenced by those subnodes
#
# Three procedures extract a person's name from an INDI record:
# gedfnf(R) produces "John Quincy Adams" form
# gedlnf(R) produces "Adams, John Quincy" form
# gednmf(R,f) produces an arbitrary format, substituting
# prefix, firstname, lastname, suffix for
# "P", "F", "L", "S" (respectively) in f
#
# geddate(R) finds the DATE subnode of a node and returns a string
# of at least 12 characters in a standard form such as "11 Jul 1767"
# or "abt 1810". It is assumed that the input is in English.
#
# gedyear(R) returns the year from the DATE subnode of a node.
#
# gedfind(g,s) generates the individuals under gedcom record g
# that are named by s, a string of whitespace-separated words.
# gedfind() generates each INDI node for which every word of s
# is matched by either a word of the individual's name or by
# the birth year. Matching is case-insensitive.
#
############################################################################
record gedcom(
tree, # tree of data records
id, # table of labeled nodes, indexed by @ID@
fam, # list of FAM nodes
ind # list of INDI nodes
)
record gednode(
level, # level
id, # ID (label), including @...@ delimiters
tag, # tag
data, # data
lnum, # line number
parent, # parent node in tree
ref, # referenced node, if any
sub, # sub-entry list
hcode # hashcode, if INDI node
)
$define WHITESPACE ' \t\n\r'
# gedload(f) -- load GEDCOM data from file f, returning gedcom record.
procedure gedload(f) #: load GEDCOM data from file f
local line, lnum, r, curr
local root, id, fam, ind
local hset, h1, h2, c
lnum := 0
root := curr := gednode(-1, , "ROOT", "", lnum, , , [])
id := table()
fam := []
ind := []
while line := read(f) do {
lnum +:= 1
if *line = 0 then
next
if not (r := gedscan(line)) then {
write(&errout, "ERR, line ", lnum, ": ", line)
next
}
r.lnum := lnum
r.sub := []
if r.tag == "CONC" then { # continuation line (no \n)
curr.data ||:= r.data
next
}
if r.tag == "CONT" then { # continuation line (with \n)
curr.data ||:= "\n" || r.data
next
}
while curr.level >= r.level do
curr := curr.parent
put(curr.sub, r)
r.parent := curr
curr := r
id[\r.id] := r
case r.tag of {
"FAM": put(fam, r)
"INDI": put(ind, r)
}
}
every r := gedwalk(root) do
r.ref := id[r.data]
hset := set()
every r := !ind do {
h1 := h2 := gedhi(r)
every c := !"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" do
if member(hset, h2) then
h2 := h1 || c # add disambiguating suffix if needed
else
break
insert(hset, r.hcode := h2)
}
return gedcom(root, id, fam, ind)
end
# gedscan(f) -- scan one line of a GEDCOM record, returning gednode record
procedure gedscan(s) # (internal procedure)
local level, id, tag, data
static alnum
initial alnum := &letters ++ &digits ++ '_'
s ? {
tab(many(WHITESPACE))
level := tab(many(&digits)) | fail
tab(many(WHITESPACE))
if id := (="@" || tab(upto('@') + 1)) then
tab(many(WHITESPACE))
tag := tab(many(alnum)) | fail
tab(many(WHITESPACE))
data := tab(0)
return gednode(level, id, tag, data)
}
end
# gedwalk(r) -- walk GEDCOM tree, generating nodes in preorder
procedure gedwalk(r) #: generate GEDCOM tree nodes in preorder
suspend r | gedwalk(!r.sub)
fail
end
# gedsub(r, field...) -- generate subrecords with given tags
# gedval(r, field...) -- generate values of subrecords with given tags
# gedref(r, field...) -- generate nodes referenced by given tags
procedure gedsub(r, f[]) #: find subrecords
local tag, x
tag := get(f) | fail
every x := !r.sub do {
if x.tag == tag then
if *f > 0 then
suspend gedsub ! push(f, x)
else
suspend x
}
end
procedure gedval(a[]) #: find subrecord values
suspend (gedsub ! a).data
end
procedure gedref(a[]) #: find referenced nodes
suspend \(gedsub ! a).ref
end
# gedfnf(r) -- get name from individual record, first name first
procedure gedfnf(r) #: get first name first
return gednmf(r, "P F L S")
end
# gedlnf(r) -- get name from individual record, last name first
procedure gedlnf(r) #: get last name first
local s
s := gednmf(r, "L, P F S")
s ? {
=", "
return tab(0)
}
end
# gednmf(r, f) -- general name formatter
#
# substitutes the first name, last name, prefix, and suffix
# for the letters F, L, P, S respectively in string f.
# multiple spaces are suppressed.
procedure gednmf(r, f) #: format name
local c, s, prefix, first, last, suffix
prefix := gedval(r, "TITL" | "NPFX") | gedval(r, "NAME", "NPFX")
s := gedval(r, "NAME") | fail
s ? {
first := trim(tab(upto('/') | 0))
="/"
last := trim(tab(upto('/') | 0))
="/"
suffix := gedval(r, "NSFX") | ("" ~== tab(0))
}
s := ""
f ? {
while s ||:= tab(upto('PFLS ')) do {
while c := tab(any('PFLS ')) do {
s ||:= case c of {
"P": \prefix
"F": \first
"L": \last
"S": \suffix
" ": s[-1] ~== " "
}
}
}
s ||:= tab(0)
}
return trim(s)
end
# geddate(r) -- get date from record in standard form
procedure geddate(r) #: get canonical date
local s, t, w
static ftab
initial {
ftab := table()
ftab["JAN"] := "Jan"; ftab["FEB"] := "Feb"; ftab["MAR"] := "Mar"
ftab["APR"] := "Apr"; ftab["MAY"] := "May"; ftab["JUN"] := "Jun"
ftab["JUL"] := "Jul"; ftab["AUG"] := "Aug"; ftab["SEP"] := "Sep"
ftab["OCT"] := "Oct"; ftab["NOV"] := "Nov"; ftab["DEC"] := "Dec"
ftab["ABT"] := "abt"; ftab["BEF"] := "bef"; ftab["AFT"] := "aft"
ftab["CAL"] := "cal"; ftab["EST"] := "est"
}
s := trim(gedval(r, "DATE"), WHITESPACE) | fail
t := ""
s ? while not pos(0) do {
tab(many(WHITESPACE))
w := tab(upto(WHITESPACE) | 0)
t ||:= " " || (\ftab[w] | w)
}
if *t > 13 then
return t[2:0]
else
return right(t, 12)
end
# gedyear(r) -- get year from event record
procedure gedyear(r) #: get year
local d, y
d := gedval(r, "DATE") | fail
d ? while tab(upto(&digits)) do
if (y := tab(many(&digits)) \ 1) >= 1000 then
return y
end
# gedhi -- generate hashcode for individual record
#
# The hashcode uses two initials, final digits of birth year,
# and a 3-letter hashing of the full name and birthdate fields.
procedure gedhi(r) # (internal procedure)
local s, name, bdate, bd
static lc, uc
initial {
uc := string(&ucase)
lc := string(&lcase)
}
s := ""
name := gedval(r, "NAME") | ""
name ? {
# prefer initial of nickname; else skip unused firstname in parens
tab(upto('"') + 1) | (="(" & tab(upto(')') + 1))
tab(any(' \t'))
s ||:= tab(any(&letters)) | "X" # first initial
tab(upto('/') + 1)
tab(any(' \t'))
s ||:= tab(any(&letters)) | "X" # second initial
}
bdate := geddate(gedsub(r, "BIRT")) | ""
bd := bdate[-2:0] | "00"
if not (bd ? (tab(many(&digits)) & pos(0))) then
bd := "99"
s ||:= bd || gedh3a(name || bdate)
return map(s, lc, uc)
end
# gedh3a(s) -- hash arbitrary string into three alphabetic characters
procedure gedh3a(s) # (internal procedure)
local n, d1, d2, d3, c
n := 0
every c := !map(s) do
if not upto(' \t\f\r\n', c) then
n := 37 * n + ord(c) - 32
d1 := 97 + (n / 676) % 26
d2 := 97 + (n / 26) % 26
d3 := 97 + n % 26
return char(d1) || char(d2) || char(d3)
end
# gedfind(g, s) -- find records by name from gedcom record
#
# g is a gedcom record; s is a string of whitespace-separated words.
# gedfind() generates each INDI node for which every word of s
# is matched by either a word of the individual's name or by
# the birth year. Matching is case-insensitive.
procedure gedfind(g, s) #: find individual by name
local r
every r := !g.ind do
if gedmatch(r, s) then
suspend r
end
# gedmatch(r, s) -- match record against name
#
# s is a string of words to match name field and/or birth year.
# Matching is case sensitive.
procedure gedmatch(r, s) # (internal procedure)
local w
every w := gedlcw(s) do
(w == (gedlcw(gedval(r, "NAME")) | gedyear(gedsub(r, "BIRT")))) | fail
return r
end
# gedlcw(s, c) -- generate words from string s separated by chars from c
#
# words are mapped to lower-case to allow case-insensitive comparisons
procedure gedlcw(s, c) # (internal procedure)
/c := '/ \t\r\n\v\f'
map(s) ? {
tab(many(c))
while not pos(0) do {
suspend tab(upto(c) | 0) \ 1
tab(many(c))
}
}
fail
end
|