b547857c128312c2697a82f8867f768a36c27a07
[familia.git] / src / gedcom / familia_gedcom.c
1 #include <assert.h>
2
3 #include "familia_gedcom.h"
4
5 #include "../storage/storage.h"
6 #include "../storage/individual.h"
7 #include "../memory_stack.h"
8
9 Gedcom_ctxt familia_gedcom_header_start (Gedcom_rec rec,
10                                         int level,
11                                         Gedcom_val xref,
12                                         char *tag,
13                                         char *raw_value,
14                                         int parsed_tag,
15                                         Gedcom_val parsed_value)
16 {
17         fprintf(stderr, "The header starts (l: %d, tag: %s, raw:%s) \n", level, tag, raw_value);
18         return (Gedcom_ctxt)1;
19 }
20
21 void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self)
22 {
23         /* context will print as "1" */
24         fprintf(stderr, "The header ends, context is %d\n", (int)self);
25 }
26
27 Gedcom_ctxt familia_gedcom_header_source_start(Gedcom_elt  elt,
28                                         Gedcom_ctxt ctxt,
29                                         int         level,
30                                         char*       tag,
31                                         char*       raw_value,
32                                         int         parsed_tag,
33                                         Gedcom_val  parsed_value)
34 {
35         char *source = GEDCOM_STRING(parsed_value);
36         printf("This file was created by %s\n", source);
37         return ctxt;
38 }
39
40 /*
41 void familia_gedcom_header_source_end(Gedcom_elt  elt,
42                                         Gedcom_ctxt ctxt,
43                                         Gedcom_ctxt self,
44                                         Gedcom_val  parsed_value)
45 {
46         printf("End of the source description\n");
47 }
48 */
49
50 Gedcom_ctxt familia_gedcom_header_version_start(Gedcom_elt  elt,
51                                         Gedcom_ctxt ctxt,
52                                         int         level,
53                                         char*       tag,
54                                         char*       raw_value,
55                                         int         parsed_tag,
56                                         Gedcom_val  parsed_value)
57 {
58         char *source = GEDCOM_STRING(parsed_value);
59         printf("Program version: %s (l: %d, tag: %s, raw:%s) \n", source, level, tag, raw_value);
60         return ctxt;
61 }
62 /*
63 void familia_gedcom_header_version_end(Gedcom_elt  elt,
64                                         Gedcom_ctxt ctxt,
65                                         Gedcom_ctxt self,
66                                         Gedcom_val  parsed_value)
67 {
68         printf("End of the version description\n");
69 }*/
70
71 Gedcom_ctxt familia_gedcom_family_start (Gedcom_rec rec,
72                                         int level,
73                                         Gedcom_val xref,
74                                         char *tag,
75                                         char *raw_value,
76                                         int parsed_tag,
77                                         Gedcom_val parsed_value)
78 {
79         struct familia_family * family = NULL;
80         struct xref_value *xr = GEDCOM_XREF_PTR(xref);
81
82         family = familia_memory_stack_find(xr->string, FS_FAMILY);
83
84         return (Gedcom_ctxt)family;
85 }
86 /*
87 void familia_gedcom_family_end (Gedcom_rec rec, Gedcom_ctxt self)
88 {
89         familia_storage_add_family(familia_storage_get_current(), self);
90 }
91 */
92 Gedcom_ctxt familia_gedcom_individual_start (Gedcom_rec rec,
93                                         int level,
94                                         Gedcom_val xref,
95                                         char *tag,
96                                         char *raw_value,
97                                         int parsed_tag,
98                                         Gedcom_val parsed_value)
99 {
100         struct familia_individual * individual = NULL;
101         struct xref_value *xr = GEDCOM_XREF_PTR(xref);
102
103         individual = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
104
105         return (Gedcom_ctxt)individual;
106 }
107 /*
108 void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self)
109 {
110         familia_storage_add_individual(familia_storage_get_current(), self);
111 }
112 */
113 Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt  elt,
114                                         Gedcom_ctxt ctxt,
115                                         int         level,
116                                         char*       tag,
117                                         char*       raw_value,
118                                         int         parsed_tag,
119                                         Gedcom_val  parsed_value)
120 {
121         familia_individual_set_first_name(ctxt, GEDCOM_STRING(parsed_value));
122         return ctxt;
123 }
124 /*
125 void familia_gedcom_individual_first_name_end(Gedcom_elt  elt,
126                                         Gedcom_ctxt ctxt,
127                                         Gedcom_ctxt self,
128                                         Gedcom_val  parsed_value)
129 {
130 }
131 */
132 Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt  elt,
133                                         Gedcom_ctxt ctxt,
134                                         int         level,
135                                         char*       tag,
136                                         char*       raw_value,
137                                         int         parsed_tag,
138                                         Gedcom_val  parsed_value)
139 {
140         if (GEDCOM_IS_STRING(parsed_value)) {
141                 familia_individual_set_last_name(ctxt, GEDCOM_STRING(parsed_value));
142         }
143         return ctxt;
144 }
145 /*
146 void familia_gedcom_individual_last_name_end(Gedcom_elt  elt,
147                                         Gedcom_ctxt ctxt,
148                                         Gedcom_ctxt self,
149                                         Gedcom_val  parsed_value)
150 {
151 }
152 */
153
154 Gedcom_ctxt familia_gedcom_individual_add_family(Gedcom_elt  elt,
155                                         Gedcom_ctxt ctxt,
156                                         int         level,
157                                         char*       tag,
158                                         char*       raw_value,
159                                         int         parsed_tag,
160                                         Gedcom_val  parsed_value)
161 {
162         void * family = NULL;
163         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
164
165         family = familia_memory_stack_find(xr->string, FS_FAMILY);
166         familia_individual_add_family(ctxt, family);
167
168         return ctxt;
169 }
170
171 /**
172  * Set parents for child
173  */
174 Gedcom_ctxt familia_gedcom_individual_set_parents(Gedcom_elt  elt,
175                                         Gedcom_ctxt ctxt,
176                                         int         level,
177                                         char*       tag,
178                                         char*       raw_value,
179                                         int         parsed_tag,
180                                         Gedcom_val  parsed_value)
181 {
182         void * child = NULL;
183         /* XREF_PTR(FAM) */
184         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
185
186         child = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
187         familia_individual_set_parents(child, ctxt);
188
189         return ctxt;
190 }
191
192 /**
193  * Adds child to the family
194  */
195 Gedcom_ctxt familia_gedcom_family_add_child(Gedcom_elt  elt,
196                                         Gedcom_ctxt ctxt,
197                                         int         level,
198                                         char*       tag,
199                                         char*       raw_value,
200                                         int         parsed_tag,
201                                         Gedcom_val  parsed_value)
202 {
203         void * family = NULL;
204         /* XREF_PTR(FAM) */
205         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
206
207         family = familia_memory_stack_find(xr->string, FS_FAMILY);
208         familia_family_add_child(family, ctxt);
209
210         return ctxt;
211 }
212
213 /**
214  * Adds first parent to the family
215  * Parent is the first individual in family. It can be e.g. Husband
216  */
217 Gedcom_ctxt familia_gedcom_family_set_parent1(Gedcom_elt  elt,
218                                         Gedcom_ctxt ctxt,
219                                         int         level,
220                                         char*       tag,
221                                         char*       raw_value,
222                                         int         parsed_tag,
223                                         Gedcom_val  parsed_value)
224 {
225         void * parent = NULL;
226         /* XREF_PTR(INDI) */
227         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
228
229         parent = familia_memory_stack_find(xr->string, FS_FAMILY);
230         /* ctxt: REC_FAM */
231         familia_family_set_parent(ctxt, parent, Individual1);
232
233         return ctxt;
234 }
235
236 /**
237  * Adds second parent to the family
238  * Parent is the first individual in family. It can be e.g. Wife
239  */
240 Gedcom_ctxt familia_gedcom_family_set_parent2(Gedcom_elt  elt,
241                                         Gedcom_ctxt ctxt,
242                                         int         level,
243                                         char*       tag,
244                                         char*       raw_value,
245                                         int         parsed_tag,
246                                         Gedcom_val  parsed_value)
247 {
248         void * parent = NULL;
249         /* XREF_PTR(INDI) */
250         struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
251
252         parent = familia_memory_stack_find(xr->string, FS_FAMILY);
253         /* ctxt: REC_FAM */
254         familia_family_set_parent(ctxt, parent, Individual2);
255
256         return ctxt;
257 }
258
259 /*
260 void familia_gedcom_individual_family_end(Gedcom_elt  elt,
261                                         Gedcom_ctxt ctxt,
262                                         Gedcom_ctxt self,
263                                         Gedcom_val  parsed_value)
264 {
265 }*/
266
267 void familia_gedcom_error (Gedcom_msg_type type, char *msg)
268 {
269         fprintf(stderr, "> %s\n", msg);
270 }
271
272
273
274 void familia_gedcom_init()
275 {
276         gedcom_set_message_handler(familia_gedcom_error);
277
278         gedcom_subscribe_to_record(REC_HEAD, familia_gedcom_header_start, familia_gedcom_header_end);
279         /* Create and set family as current (set to ctxt) */
280         gedcom_subscribe_to_record(REC_FAM, familia_gedcom_family_start, NULL);
281         /* Create and set individual as current (set to ctxt) */
282         gedcom_subscribe_to_record(REC_INDI, familia_gedcom_individual_start, NULL);
283         gedcom_subscribe_to_element(ELT_HEAD_SOUR, familia_gedcom_header_source_start, NULL);
284         gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS, familia_gedcom_header_version_start, NULL);
285         /* Set in current individual first name */
286         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME, familia_gedcom_individual_set_first_name, NULL);
287         /* Set in current individual last name */
288         gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN, familia_gedcom_individual_last_name_start, NULL);
289         /* Add to current individual new family */
290         gedcom_subscribe_to_element(ELT_SUB_FAMS, familia_gedcom_individual_add_family, NULL);
291         /* Set current family as parents to child */
292         gedcom_subscribe_to_element(ELT_FAM_CHIL, familia_gedcom_individual_set_parents, NULL);
293         /* Set current individual new parents */
294         gedcom_subscribe_to_element(ELT_SUB_FAMC, familia_gedcom_family_add_child, NULL);
295         /* Set in current family first parent (e.g. husband) */
296         gedcom_subscribe_to_element(ELT_FAM_HUSB, familia_gedcom_family_set_parent1, NULL);
297         /* Set in current family second parent (e.g. wife) */
298         gedcom_subscribe_to_element(ELT_FAM_WIFE, familia_gedcom_family_set_parent2, NULL);
299
300         familia_memory_stack_init();
301 }
302
303 void familia_gedcom_dump_val(Gedcom_val val)
304 {
305         if (GEDCOM_IS_NULL(val)) {
306                 fprintf(stderr, "| %25s |\n", "NULL");
307         }
308         else if (GEDCOM_IS_STRING(val)) {
309                 fprintf(stderr, "| %4s | %20s |\n", "STRING", GEDCOM_STRING(val));
310         }
311         else if (GEDCOM_IS_DATE(val)) {
312                 fprintf(stderr, "| %4s | %20s |\n", "DATE", "(@todo)");
313         }
314         else if (GEDCOM_IS_AGE(val)) {
315                 fprintf(stderr, "| %4s | %20s |\n", "AGE", "(@todo)");
316         }
317         else if (GEDCOM_IS_XREF_PTR(val)) {
318                 struct xref_value *xref = NULL;
319                 fprintf(stderr, "| %25s | %15s | %4s |\n", "STRING", "OBJECT", "TYPE");
320                 xref = GEDCOM_XREF_PTR(val);
321                 fprintf(stderr, "| %25s | %15s | %4s |\n",
322                         xref->string,
323                         xref->object ? "initialized" : "NULL",
324                         (xref->type == XREF_NONE) ? "NONE" :
325                                 (xref->type == XREF_FAM ) ? "FAM" :
326                                 (xref->type == XREF_INDI) ? "INDI" :
327                                 (xref->type == XREF_NOTE) ? "NOTE" :
328                                 (xref->type == XREF_OBJE) ? "OBJE" :
329                                 (xref->type == XREF_REPO) ? "REPO" :
330                                 (xref->type == XREF_SOUR) ? "SOUR" :
331                                 (xref->type == XREF_SUBM) ? "SUBM" :
332                                 (xref->type == XREF_SUBN) ? "SUBN" :
333                                 (xref->type == XREF_ANY ) ? "ANY" :
334                                 (xref->type == XREF_USER) ? "USER" : "???");
335         } 
336         else {
337                 fprintf(stderr, "| %20s |\n", "???");
338         }
339 }