1 /*
2    Copyright (c) 1991-1999 Thomas T. Wetmore IV
3 
4    Permission is hereby granted, free of charge, to any person
5    obtaining a copy of this software and associated documentation
6    files (the "Software"), to deal in the Software without
7    restriction, including without limitation the rights to use, copy,
8    modify, merge, publish, distribute, sublicense, and/or sell copies
9    of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be
13    included in all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22    SOFTWARE.
23 */
24 /* modified 05 Jan 2000 by Paul B. McBride (pmcbride@tiac.net) */
25 /*=============================================================
26  * replace.c -- Replace persons and families
27  * Copyright(c) 1995-96 by T.T. Wetmore IV; all rights reserved
28  * pre-SourceForge version information:
29  *   3.0.3 - 20 Jan 96
30  *===========================================================*/
31 
32 #include "llstdlib.h"
33 #include "table.h"
34 #include "translat.h"
35 #include "gedcom.h"
36 
37 /*===================================================================
38  * replace_indi -- Replace a person in database with modified version
39  *  indi1 = current record (copy from database, may or may not be in cache)
40  *  indi2 = new data
41  *  replaces all children nodes of indi1 with children nodes of indi2
42  *  consumes indi2 (calls free_node on it)
43  *=================================================================*/
44 void
replace_indi(NODE indi1,NODE indi2)45 replace_indi (NODE indi1, NODE indi2)
46 {
47 	NODE name1, name2, refn1, refn2, sex, body, famc, fams;
48 	NODE node, namen, refnn, name1n, refn1n, indi0;
49 	STRING key;
50 
51 
52 	/* Move indi1 data into indi0 & delete it (saving names & refns */
53 	split_indi_old(indi1, &name1, &refn1, &sex, &body, &famc, &fams);
54 	indi0 = copy_node(indi1);
55 	join_indi(indi0, NULL, NULL, sex, body, famc, fams);
56 	free_nodes(indi0);
57 	/* Move indi2 data into indi1, also copy out lists of names & refns */
58 	split_indi_old(indi2, &name2, &refn2, &sex, &body, &famc, &fams);
59 	namen = copy_nodes(name2, TRUE, TRUE);
60 	refnn = copy_nodes(refn2, TRUE, TRUE);
61 	join_indi(indi1, name2, refn2, sex, body, famc, fams);
62 	free_node(indi2);
63 	nodechk(indi1, "replace_indi");
64 
65 	/* Write data to database */
66 
67 	indi_to_dbase(indi1);
68 	key = rmvat(nxref(indi1));
69 	/* update name & refn info */
70 	/* classify does a diff on its first two arguments, repopulating all three
71 	arguments -- first is left-only, second is right-only, third is shared */
72 	/* Note: classify eliminates duplicates */
73 	classify_nodes(&name1, &namen, &name1n);
74 	classify_nodes(&refn1, &refnn, &refn1n);
75 	for (node = name1; node; node = nsibling(node))
76 		remove_name(nval(node), key);
77 	for (node = namen; node; node = nsibling(node))
78 		add_name(nval(node), key);
79 	rename_from_browse_lists(key);
80 	for (node = refn1; node; node = nsibling(node))
81 		if (nval(node)) remove_refn(nval(node), key);
82 	for (node = refnn; node; node = nsibling(node))
83 		if (nval(node)) add_refn(nval(node), key);
84 
85 /* now cleanup (indi1 tree is now composed of indi2 data) */
86 	free_nodes(name1);
87 	free_nodes(namen);
88 	free_nodes(name1n);
89 	free_nodes(refn1);
90 	free_nodes(refnn);
91 	free_nodes(refn1n);
92 }
93 /*==================================================================
94  * replace_fam -- Replace a family in database with modified version
95  *  fam1 = current record (copy from database, may or may not be in cache)
96  *  fam2 = new data
97  *  replaces all children nodes of fam1 with children nodes of fam2
98  *  consumes fam2 (calls free_node on it)
99  *================================================================*/
100 void
replace_fam(NODE fam1,NODE fam2)101 replace_fam (NODE fam1, NODE fam2)
102 {
103 	NODE refn1, refn2, husb, wife, chil, body;
104 	NODE refnn, refn1n, node, fam0;
105 	STRING key;
106 
107 
108 	/* Move fam1 data into fam0 & delete it (saving refns) */
109 	split_fam(fam1, &refn1, &husb, &wife, &chil, &body);
110 	fam0 = copy_node(fam1);
111 	join_fam(fam0, NULL, husb, wife, chil, body);
112 	free_nodes(fam0);
113 	/* Move fam2 data into fam1, also copy out list of refns */
114 	split_fam(fam2, &refn2, &husb, &wife, &chil, &body);
115 	refnn = copy_nodes(refn2, TRUE, TRUE);
116 	join_fam(fam1, refn2, husb, wife, chil, body);
117 	free_node(fam2);
118 
119 	/* Write data to database */
120 
121 
122 	fam_to_dbase(fam1);
123 	key = rmvat(nxref(fam1));
124 	/* remove deleted refns & add new ones */
125 	classify_nodes(&refn1, &refnn, &refn1n);
126 	for (node = refn1; node; node = nsibling(node))
127 		if (nval(node)) remove_refn(nval(node), key);
128 	for (node = refnn; node; node = nsibling(node))
129 		if (nval(node)) add_refn(nval(node), key);
130 	free_nodes(refn1);
131 	free_nodes(refnn);
132 	free_nodes(refn1n);
133 }
134