0e568691afe2401b5e45630a3c4533a5b7a53e49
[gedcom-parse.git] / gom / change_date.c
1 /* Change date 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 "change_date.h"
27 #include "family.h"
28 #include "individual.h"
29 #include "note_sub.h"
30 #include "multimedia.h"
31 #include "note.h"
32 #include "repository.h"
33 #include "source.h"
34 #include "submitter.h"
35 #include "user_rec.h"
36 #include "gom_internal.h"
37 #include "gom.h"
38 #include "gedcom.h"
39
40 Gedcom_ctxt sub_chan_start(_ELT_PARAMS_)
41 {
42   Gom_ctxt ctxt = (Gom_ctxt)parent;
43   Gom_ctxt result = NULL;
44
45   if (! ctxt)
46     NO_CONTEXT;
47   else {
48     struct change_date *chan
49       = (struct change_date *)malloc(sizeof(struct change_date));
50     if (! chan)
51       MEMORY_ERROR;
52     else {
53       memset (chan, 0, sizeof(struct change_date));
54       
55       switch (ctxt->ctxt_type) {
56         case REC_FAM:
57           ADDFUNC2_NOLIST(family,change_date)(ctxt, chan); break;
58         case REC_INDI:
59           ADDFUNC2_NOLIST(individual,change_date)(ctxt, chan); break;
60         case REC_OBJE:
61           ADDFUNC2_NOLIST(multimedia,change_date)(ctxt, chan); break;
62         case REC_NOTE:
63           ADDFUNC2_NOLIST(note,change_date)(ctxt, chan); break;
64         case REC_REPO:
65           ADDFUNC2_NOLIST(repository,change_date)(ctxt, chan); break;
66         case REC_SOUR:
67           ADDFUNC2_NOLIST(source,change_date)(ctxt, chan); break;
68         case REC_SUBM:
69           ADDFUNC2_NOLIST(submitter,change_date)(ctxt, chan); break;
70         default:
71           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
72       }
73       result = MAKE_GOM_CTXT(elt, change_date, chan);
74     }
75   }
76   
77   return (Gedcom_ctxt)result;
78 }
79
80 DEFINE_DATE_CB(change_date, sub_chan_date_start, date)
81 DEFINE_STRING_CB(change_date, sub_chan_time_start, time)
82
83 DEFINE_ADDFUNC2(change_date, note_sub, note)
84 DEFINE_ADDFUNC2(change_date, user_data, extra)
85
86 void change_date_subscribe()
87 {
88   gedcom_subscribe_to_element(ELT_SUB_CHAN, sub_chan_start, def_elt_end);
89   gedcom_subscribe_to_element(ELT_SUB_CHAN_DATE, sub_chan_date_start,
90                               def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUB_CHAN_TIME, sub_chan_time_start,
92                               def_elt_end);
93 }
94
95 void CLEANFUNC(change_date)(struct change_date *chan)
96 {
97   if (chan) {
98     SAFE_FREE(chan->date);
99     SAFE_FREE(chan->time);
100     DESTROY_CHAIN_ELTS(note_sub, chan->note);
101     DESTROY_CHAIN_ELTS(user_data, chan->extra);
102   }
103   SAFE_FREE(chan);
104 }
105
106 int write_change_date(Gedcom_write_hndl hndl, int parent,
107                       struct change_date *chan)
108 {
109   int result = 0;
110
111   if (!chan) return 1;
112
113   result |= gedcom_write_element_str(hndl, ELT_SUB_CHAN, 0, parent, NULL);
114   if (chan->date)
115     result |= gedcom_write_element_date(hndl, ELT_SUB_CHAN_DATE, 0,
116                                         ELT_SUB_CHAN, chan->date);
117   if (chan->time)
118     result |= gedcom_write_element_str(hndl, ELT_SUB_CHAN_TIME, 0,
119                                        ELT_SUB_CHAN_DATE, chan->time);
120   if (chan->note)
121     result |= write_note_subs(hndl, ELT_SUB_CHAN, chan->note);
122   if (chan->extra)
123     result |= write_user_data(hndl, chan->extra);
124   
125   return result;
126 }