3 #include "familia_gedcom.h"
5 #include "../storage/storage.h"
6 #include "../storage/individual.h"
7 #include "../memory_stack.h"
11 Gedcom_ctxt familia_gedcom_header_start (Gedcom_rec rec,
17 Gedcom_val parsed_value)
19 assert(rec == REC_HEAD);
21 fprintf(stderr, "The header starts (l: %d, tag: %s, raw:%s) \n", level, tag, raw_value);
22 return (Gedcom_ctxt)1;
25 void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
27 /* context will print as "1" */
28 fprintf(stderr, "The header ends, context is %d\n", (int)self);
31 Gedcom_ctxt familia_gedcom_header_source_start(Gedcom_elt elt,
37 Gedcom_val parsed_value)
39 char *source = GEDCOM_STRING(parsed_value);
40 printf("This file was created by %s\n", source);
45 void familia_gedcom_header_source_end(Gedcom_elt elt,
48 Gedcom_val parsed_value)
50 printf("End of the source description\n");
54 Gedcom_ctxt familia_gedcom_header_version_start(Gedcom_elt elt,
60 Gedcom_val parsed_value)
62 char *source = GEDCOM_STRING(parsed_value);
63 printf("Program version: %s (l: %d, tag: %s, raw:%s) \n", source, level, tag, raw_value);
67 void familia_gedcom_header_version_end(Gedcom_elt elt,
70 Gedcom_val parsed_value)
72 printf("End of the version description\n");
75 Gedcom_ctxt familia_gedcom_family_start (Gedcom_rec rec,
81 Gedcom_val parsed_value)
83 struct familia_family * family = NULL;
84 struct xref_value *xr = GEDCOM_XREF_PTR(xref);
86 assert(rec == REC_FAM);
87 assert(parsed_tag == TAG_FAM);
89 family = familia_memory_stack_find(xr->string, FS_FAMILY);
91 return (Gedcom_ctxt)family;
94 void familia_gedcom_family_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
96 /* familia_storage_add_family(familia_storage_get_current(), self);*/
99 Gedcom_ctxt familia_gedcom_individual_start (Gedcom_rec rec,
105 Gedcom_val parsed_value)
107 struct familia_individual * individual = NULL;
108 struct xref_value *xr = GEDCOM_XREF_PTR(xref);
110 assert(rec == REC_INDI);
111 assert(parsed_tag == TAG_INDI);
113 individual = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
115 return (Gedcom_ctxt)individual;
118 void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
120 /* familia_storage_add_individual(familia_storage_get_current(), self);*/
123 Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt elt,
129 Gedcom_val parsed_value)
131 assert(elt == ELT_SUB_PERS_NAME);
132 assert(parsed_tag == TAG_NAME);
134 familia_individual_set_first_name(ctxt, GEDCOM_STRING(parsed_value));
138 void familia_gedcom_individual_first_name_end(Gedcom_elt elt,
141 Gedcom_val parsed_value)
145 Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt elt,
151 Gedcom_val parsed_value)
153 assert(elt == ELT_SUB_PERS_NAME_SURN);
155 if (GEDCOM_IS_STRING(parsed_value)) {
156 familia_individual_set_last_name(ctxt, GEDCOM_STRING(parsed_value));
161 void familia_gedcom_individual_last_name_end(Gedcom_elt elt,
164 Gedcom_val parsed_value)
169 Gedcom_ctxt familia_gedcom_individual_add_family(Gedcom_elt elt,
175 Gedcom_val parsed_value)
177 void * family = NULL;
178 struct xref_value *xr = NULL;
180 assert(elt == ELT_SUB_FAMS);
181 assert(parsed_tag == TAG_FAMS);
183 xr = GEDCOM_XREF_PTR(parsed_value);
185 family = familia_memory_stack_find(xr->string, FS_FAMILY);
186 familia_individual_add_family(ctxt, family);
192 * Set parents for child
194 Gedcom_ctxt familia_gedcom_individual_set_parents(Gedcom_elt elt,
200 Gedcom_val parsed_value)
204 struct xref_value *xr = NULL;
206 assert(elt == ELT_FAM_CHIL);
207 assert(parsed_tag == TAG_CHIL);
209 xr = GEDCOM_XREF_PTR(parsed_value);
211 child = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
212 familia_individual_set_parents(child, ctxt);
218 * Adds child to the family
220 Gedcom_ctxt familia_gedcom_family_add_child(Gedcom_elt elt,
226 Gedcom_val parsed_value)
228 void * family = NULL;
230 struct xref_value *xr = NULL;
232 assert(elt == ELT_SUB_FAMC);
233 assert(parsed_tag == TAG_FAMC);
235 xr = GEDCOM_XREF_PTR(parsed_value);
237 family = familia_memory_stack_find(xr->string, FS_FAMILY);
238 familia_family_add_child(family, ctxt);
244 * Adds first parent to the family
245 * Parent is the first individual in family. It can be e.g. Husband
247 Gedcom_ctxt familia_gedcom_family_set_parent1(Gedcom_elt elt,
253 Gedcom_val parsed_value)
255 void * parent = NULL;
257 struct xref_value *xr = NULL;
259 assert(elt == ELT_FAM_HUSB);
260 assert(parsed_tag == TAG_HUSB);
262 xr = GEDCOM_XREF_PTR(parsed_value);
264 parent = familia_memory_stack_find(xr->string, FS_FAMILY);
266 familia_family_set_parent(ctxt, parent, Individual1);
272 * Adds second parent to the family
273 * Parent is the first individual in family. It can be e.g. Wife
275 Gedcom_ctxt familia_gedcom_family_set_parent2(Gedcom_elt elt,
281 Gedcom_val parsed_value)
283 void * parent = NULL;
285 struct xref_value *xr = NULL;
287 assert(elt == ELT_FAM_WIFE);
288 assert(parsed_tag == TAG_WIFE);
290 xr = GEDCOM_XREF_PTR(parsed_value);
292 parent = familia_memory_stack_find(xr->string, FS_FAMILY);
294 familia_family_set_parent(ctxt, parent, Individual2);
300 * Sets date for the current event. Currently supports only individual birth.
301 * TODO: Implement all events.
303 Gedcom_ctxt familia_gedcom_event_set_date(Gedcom_elt elt,
309 Gedcom_val parsed_value)
311 struct fged_timestampable_ctxt * tctxt = ctxt;
313 switch(tctxt->type) {
315 struct date_value dv;
316 struct familia_individual * ind = NULL;
318 assert(GEDCOM_IS_DATE(parsed_value));
320 ind = ((struct fged_ctxt *)ctxt)->object;
321 dv = GEDCOM_DATE(parsed_value);
323 familia_individual_set_birth_date(ind, dv);
330 Gedcom_ctxt familia_gedcom_timestampable_ctxt_wrap(Gedcom_elt elt,
336 Gedcom_val parsed_value)
338 struct fged_timestampable_ctxt * tctxt = NULL;
339 tctxt = (struct fged_timestampable_ctxt *)malloc(sizeof(struct fged_timestampable_ctxt));
344 case ELT_SUB_INDIV_BIRT:
351 void familia_gedcom_timestampable_ctxt_unwrap(Gedcom_elt elt,
354 Gedcom_val parsed_value)
356 struct fged_timestampable_ctxt * tctxt = NULL;
364 void familia_gedcom_error (Gedcom_msg_type type, char *msg)
366 fprintf(stderr, "> %s\n", msg);
371 void familia_gedcom_init()
373 gedcom_set_message_handler(familia_gedcom_error);
375 gedcom_subscribe_to_record(REC_HEAD, familia_gedcom_header_start, familia_gedcom_header_end);
376 /* Create and set family as current (set to ctxt) */
377 gedcom_subscribe_to_record(REC_FAM, familia_gedcom_family_start, familia_gedcom_family_end);
378 /* Create and set individual as current (set to ctxt) */
379 gedcom_subscribe_to_record(REC_INDI, familia_gedcom_individual_start, familia_gedcom_individual_end);
380 gedcom_subscribe_to_element(ELT_HEAD_SOUR, familia_gedcom_header_source_start, NULL);
381 gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS, familia_gedcom_header_version_start, NULL);
382 /* Set in current individual first name */
383 gedcom_subscribe_to_element(ELT_SUB_PERS_NAME, familia_gedcom_individual_set_first_name, NULL);
384 /* Set in current individual last name */
385 gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN, familia_gedcom_individual_last_name_start, NULL);
386 /* Add to current individual new family */
387 gedcom_subscribe_to_element(ELT_SUB_FAMS, familia_gedcom_individual_add_family, NULL);
388 /* Set current family as parents to child */
389 gedcom_subscribe_to_element(ELT_FAM_CHIL, familia_gedcom_individual_set_parents, NULL);
390 /* Set current individual new parents */
391 gedcom_subscribe_to_element(ELT_SUB_FAMC, familia_gedcom_family_add_child, NULL);
392 /* Set in current family first parent (e.g. husband) */
393 gedcom_subscribe_to_element(ELT_FAM_HUSB, familia_gedcom_family_set_parent1, NULL);
394 /* Set in current family second parent (e.g. wife) */
395 gedcom_subscribe_to_element(ELT_FAM_WIFE, familia_gedcom_family_set_parent2, NULL);
397 gedcom_subscribe_to_element(ELT_SUB_EVT_DATE, familia_gedcom_event_set_date, NULL);
398 gedcom_subscribe_to_element(ELT_SUB_INDIV_BIRT, familia_gedcom_timestampable_ctxt_wrap, familia_gedcom_timestampable_ctxt_unwrap);
400 familia_memory_stack_init();
403 void familia_gedcom_dump_val(Gedcom_val val)
405 if (GEDCOM_IS_NULL(val)) {
406 fprintf(stderr, "| %25s |\n", "NULL");
408 else if (GEDCOM_IS_STRING(val)) {
409 fprintf(stderr, "| %4s | %20s |\n", "STRING", GEDCOM_STRING(val));
411 else if (GEDCOM_IS_DATE(val)) {
412 fprintf(stderr, "| %4s | %20s |\n", "DATE", "(@todo)");
414 else if (GEDCOM_IS_AGE(val)) {
415 fprintf(stderr, "| %4s | %20s |\n", "AGE", "(@todo)");
417 else if (GEDCOM_IS_XREF_PTR(val)) {
418 struct xref_value *xref = NULL;
419 fprintf(stderr, "| %25s | %15s | %4s |\n", "STRING", "OBJECT", "TYPE");
420 xref = GEDCOM_XREF_PTR(val);
421 fprintf(stderr, "| %25s | %15s | %4s |\n",
423 xref->object ? "initialized" : "NULL",
424 (xref->type == XREF_NONE) ? "NONE" :
425 (xref->type == XREF_FAM ) ? "FAM" :
426 (xref->type == XREF_INDI) ? "INDI" :
427 (xref->type == XREF_NOTE) ? "NOTE" :
428 (xref->type == XREF_OBJE) ? "OBJE" :
429 (xref->type == XREF_REPO) ? "REPO" :
430 (xref->type == XREF_SOUR) ? "SOUR" :
431 (xref->type == XREF_SUBM) ? "SUBM" :
432 (xref->type == XREF_SUBN) ? "SUBN" :
433 (xref->type == XREF_ANY ) ? "ANY" :
434 (xref->type == XREF_USER) ? "USER" : "???");
437 fprintf(stderr, "| %20s |\n", "???");