Add compatibility for Lifelines.
[gedcom-parse.git] / gedcom / compat.c
1 /* Compatibility handling for the GEDCOM parser.
2    Copyright (C) 2001, 2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include "compat.h"
25 #include "interface.h"
26 #include "encoding.h"
27 #include "xref.h"
28 #include "gedcom_internal.h"
29 #include "gedcom.h"
30
31 int compat_at = 0;
32
33 #define SUBMITTER_LINK         "@__COMPAT__SUBM__@"
34 #define DEFAULT_SUBMITTER_NAME "Submitter"
35 #define DEFAULT_GEDCOM_VERS    "5.5"
36 #define DEFAULT_GEDCOM_FORM    "LINEAGE-LINKED"
37 /* Make default character set ANSI, for all 'special characters'
38    typically used in non-compliant gedcom generators */
39 #define DEFAULT_CHAR           "ANSI"
40
41 /* Incompatibily list (with GEDCOM 5.5):
42
43     - ftree:
44         - no submitter record, no submitter link in the header
45         - INDI.ADDR instead of INDI.RESI.ADDR
46         - NOTE doesn't have a value
47
48     - Lifelines (3.0.2):
49         - no submitter record, no submitter link in the header
50         - no GEDC field in the header
51         - no CHAR field in the header
52         - TIME field outside of DATE field in the header (will be ignored here)
53         - '@' not written as '@@' in values
54  */
55
56 void compat_generate_submitter_link(Gedcom_ctxt parent)
57 {
58   struct xref_value *xr = gedcom_parse_xref(SUBMITTER_LINK, XREF_USED,
59                                             XREF_SUBM);
60   struct tag_struct ts;
61   Gedcom_ctxt self;
62   
63   ts.string = "SUBM";
64   ts.value  = TAG_SUBM;
65   self = start_element(ELT_HEAD_SUBM,
66                        parent, 1, ts, SUBMITTER_LINK,
67                        GEDCOM_MAKE_XREF_PTR(val1, xr));
68   end_element(ELT_HEAD_SUBM, parent, self, NULL);
69 }
70
71 void compat_generate_submitter()
72 {
73   struct xref_value *xr = gedcom_parse_xref(SUBMITTER_LINK, XREF_DEFINED,
74                                             XREF_SUBM);
75   struct tag_struct ts;
76   Gedcom_ctxt self1, self2;
77
78   /* first generate "0 SUBM" */
79   ts.string = "SUBM";
80   ts.value  = TAG_SUBM;
81   self1 = start_record(REC_SUBM, 0, GEDCOM_MAKE_XREF_PTR(val1, xr), ts,
82                        NULL, GEDCOM_MAKE_NULL(val2));
83
84   /* then generate "1 NAME ..." */
85   ts.string = "NAME";
86   ts.value  = TAG_NAME;
87   self2 = start_element(ELT_SUBM_NAME, self1, 1, ts, DEFAULT_SUBMITTER_NAME,
88                         GEDCOM_MAKE_STRING(val1, DEFAULT_SUBMITTER_NAME));
89
90   /* close "1 NAME ..." */
91   end_element(ELT_SUBM_NAME, self1, self2, NULL);
92
93   /* close "0 SUBM" */
94   end_record(REC_SUBM, self1);
95 }
96
97 void compat_generate_gedcom(Gedcom_ctxt parent)
98 {
99   struct tag_struct ts;
100   Gedcom_ctxt self1, self2;
101   
102   /* first generate "1 GEDC" */
103   ts.string = "GEDC";
104   ts.value  = TAG_GEDC;
105   self1 = start_element(ELT_HEAD_GEDC, parent, 1, ts, NULL,
106                         GEDCOM_MAKE_NULL(val1));
107   
108   /* then generate "2 VERS <DEFAULT_GEDC_VERS>" */
109   ts.string = "VERS";
110   ts.value  = TAG_VERS;
111   self2 = start_element(ELT_HEAD_GEDC_VERS, self1, 2, ts,
112                         DEFAULT_GEDCOM_VERS,
113                         GEDCOM_MAKE_STRING(val1, DEFAULT_GEDCOM_VERS));
114   
115   /* close "2 VERS" */
116   end_element(ELT_HEAD_GEDC_VERS, self1, self2, NULL);
117   
118   /* then generate "2 FORM <DEFAULT_GEDCOM_FORM> */
119   ts.string = "FORM";
120   ts.value  = TAG_FORM;
121   self2 = start_element(ELT_HEAD_GEDC_FORM, self1, 2, ts,
122                         DEFAULT_GEDCOM_FORM,
123                         GEDCOM_MAKE_STRING(val1, DEFAULT_GEDCOM_FORM));
124   
125   /* close "2 FORM" */
126   end_element(ELT_HEAD_GEDC_FORM, self1, self2, NULL);
127   
128   /* close "1 GEDC" */
129   end_element(ELT_HEAD_GEDC, parent, self1, NULL);
130 }
131
132 int compat_generate_char(Gedcom_ctxt parent)
133 {
134   struct tag_struct ts;
135   Gedcom_ctxt self1;
136   
137   /* first generate "1 CHAR <DEFAULT_CHAR>" */
138   ts.string = "CHAR";
139   ts.value  = TAG_CHAR;
140   self1 = start_element(ELT_HEAD_CHAR, parent, 1, ts, DEFAULT_CHAR,
141                         GEDCOM_MAKE_STRING(val1, DEFAULT_CHAR));
142   
143   /* close "1 CHAR" */
144   end_element(ELT_HEAD_CHAR, parent, self1, NULL);
145   if (open_conv_to_internal(DEFAULT_CHAR) == 0)
146     return 1;
147   else
148     return 0;
149 }
150
151 Gedcom_ctxt compat_generate_resi_start(Gedcom_ctxt parent)
152 {
153   Gedcom_ctxt self;
154   struct tag_struct ts;
155
156   ts.string = "RESI";
157   ts.value  = TAG_RESI;
158   self = start_element(ELT_SUB_INDIV_RESI, parent, 1, ts, NULL,
159                        GEDCOM_MAKE_NULL(val1));
160   return self;
161 }
162
163 void compat_generate_resi_end(Gedcom_ctxt parent, Gedcom_ctxt self)
164 {
165   end_element(ELT_SUB_INDIV_RESI, parent, self, NULL);
166 }