319dbb7f8c0b854d2d2fe9d26bbca7ca709e5633
[gedcom-parse.git] / gom / source_event.c
1 /* Source event sub-structure in the gedcom object model.
2    Copyright (C) 2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2002.
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 <stdlib.h>
25 #include <string.h>
26 #include "source_event.h"
27 #include "source.h"
28 #include "user_rec.h"
29 #include "gom.h"
30 #include "gedcom.h"
31 #include "gom_internal.h"
32
33 Gedcom_ctxt sub_sour_even_start(_ELT_PARAMS_)
34 {
35   Gom_ctxt ctxt = (Gom_ctxt)parent;
36   Gom_ctxt result = NULL;
37
38   if (! ctxt)
39     NO_CONTEXT;
40   else {
41     struct source_event *evt = SUB_MAKEFUNC(source_event)();
42     if (evt) {
43       evt->recorded_events = strdup(GEDCOM_STRING(parsed_value));
44
45       if (! evt->recorded_events) {
46         MEMORY_ERROR;
47         free(evt);
48       }
49       else {
50         switch (ctxt->ctxt_type) {
51           case ELT_SOUR_DATA:
52             ADDFUNC2(source,source_event)(ctxt, evt); break;
53           default:
54             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
55         }
56         result = MAKE_GOM_CTXT(elt, source_event, evt);
57       }
58     }
59   }
60
61   return (Gedcom_ctxt)result;
62 }
63
64 DEFINE_SUB_MAKEFUNC(source_event)
65 DEFINE_SUB_ADDFUNC(source_event)
66 DEFINE_SUB_FINDFUNC(source_event)
67 DEFINE_SUB_REMOVEFUNC(source_event)
68 DEFINE_SUB_MOVEFUNC(source_event)
69      
70 DEFINE_DATE_CB(source_event, sub_sour_even_date_start, date_period)
71 DEFINE_STRING_CB(source_event, sub_sour_even_plac_start, jurisdiction)
72
73 DEFINE_ADDFUNC2(source_event, user_data, extra)
74      
75 void source_event_subscribe()
76 {
77   gedcom_subscribe_to_element(ELT_SOUR_DATA_EVEN, sub_sour_even_start,
78                               def_elt_end);
79   gedcom_subscribe_to_element(ELT_SOUR_DATA_EVEN_DATE,
80                               sub_sour_even_date_start, def_elt_end);
81   gedcom_subscribe_to_element(ELT_SOUR_DATA_EVEN_PLAC,
82                               sub_sour_even_plac_start, def_elt_end);
83 }
84
85 void UNREFALLFUNC(source_event)(struct source_event* obj)
86 {
87   if (obj) {
88     struct source_event* runner;
89     for (runner = obj; runner; runner = runner->next) {
90       UNREFALLFUNC(user_data)(runner->extra);
91     }
92   }
93 }
94
95 void CLEANFUNC(source_event)(struct source_event* evt)
96 {
97   if (evt) {
98     SAFE_FREE(evt->recorded_events);
99     SAFE_FREE(evt->date_period);
100     SAFE_FREE(evt->jurisdiction);
101     DESTROY_CHAIN_ELTS(user_data, evt->extra);
102   }
103 }
104
105 int write_source_events(Gedcom_write_hndl hndl, int parent,
106                         struct source_event *evt)
107 {
108   int result = 0;
109   struct source_event* obj;
110
111   if (!evt) return 1;
112
113   for (obj = evt; obj; obj = obj->next) {
114     result |= gedcom_write_element_str(hndl, ELT_SOUR_DATA_EVEN, 0,
115                                        parent, obj->recorded_events);
116     if (obj->date_period)
117       result |= gedcom_write_element_date(hndl, ELT_SOUR_DATA_EVEN_DATE, 0,
118                                          ELT_SOUR_DATA_EVEN, obj->date_period);
119     if (obj->jurisdiction)
120       result |= gedcom_write_element_str(hndl, ELT_SOUR_DATA_EVEN_PLAC, 0,
121                                         ELT_SOUR_DATA_EVEN, obj->jurisdiction);
122     if (obj->extra)
123       result |= write_user_data(hndl, obj->extra);    
124   }
125
126   return result;
127 }