Added functions to add, remove and move substructs.
[gedcom-parse.git] / gom / multimedia_link.c
1 /* Multimedia link 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 "multimedia_link.h"
27 #include "event.h"
28 #include "source_citation.h"
29 #include "note_sub.h"
30 #include "family.h"
31 #include "individual.h"
32 #include "source.h"
33 #include "submitter.h"
34 #include "user_rec.h"
35 #include "gom.h"
36 #include "gedcom.h"
37 #include "gom_internal.h"
38
39 Gedcom_ctxt sub_obje_start(_ELT_PARAMS_)
40 {
41   Gom_ctxt ctxt = (Gom_ctxt)parent;
42   Gom_ctxt result = NULL;
43
44   if (! ctxt)
45     NO_CONTEXT;
46   else {
47     struct multimedia_link *mm = SUB_MAKEFUNC(multimedia_link)();
48     if (mm) {
49       if (GEDCOM_IS_XREF_PTR(parsed_value))
50         mm->reference = GEDCOM_XREF_PTR(parsed_value);
51       
52       switch (ctxt->ctxt_type) {
53         case ELT_SUB_FAM_EVT:
54         case ELT_SUB_FAM_EVT_EVEN:
55         case ELT_SUB_INDIV_ATTR:
56         case ELT_SUB_INDIV_RESI:
57         case ELT_SUB_INDIV_BIRT:
58         case ELT_SUB_INDIV_GEN:
59         case ELT_SUB_INDIV_ADOP:
60         case ELT_SUB_INDIV_EVEN:
61           ADDFUNC2(event,multimedia_link)(ctxt, mm); break;
62         case ELT_SUB_SOUR:
63           ADDFUNC2(source_citation,multimedia_link)(ctxt, mm); break;
64         case REC_FAM:
65           ADDFUNC2(family,multimedia_link)(ctxt, mm); break;
66         case REC_INDI:
67           ADDFUNC2(individual,multimedia_link)(ctxt, mm); break;
68         case REC_SOUR:
69           ADDFUNC2(source,multimedia_link)(ctxt, mm); break;
70         case REC_SUBM:
71           ADDFUNC2(submitter,multimedia_link)(ctxt, mm); break;
72         default:
73           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
74       }
75       result = MAKE_GOM_CTXT(elt, multimedia_link, mm);
76     }
77   }
78
79   return (Gedcom_ctxt)result;
80 }
81
82 DEFINE_SUB_MAKEFUNC(multimedia_link)
83 DEFINE_SUB_ADDFUNC(multimedia_link)
84 DEFINE_SUB_FINDFUNC(multimedia_link)
85 DEFINE_SUB_REMOVEFUNC(multimedia_link)
86 DEFINE_SUB_MOVEFUNC(multimedia_link)
87      
88 DEFINE_STRING_CB(multimedia_link, sub_obje_form_start, form)
89 DEFINE_STRING_CB(multimedia_link, sub_obje_titl_start, title)
90 DEFINE_STRING_CB(multimedia_link, sub_obje_file_start, file)
91
92 DEFINE_ADDFUNC2(multimedia_link, note_sub, note)
93 DEFINE_ADDFUNC2(multimedia_link, user_data, extra)
94      
95 void multimedia_link_subscribe()
96 {
97   gedcom_subscribe_to_element(ELT_SUB_MULTIM_OBJE,
98                               sub_obje_start, def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_MULTIM_OBJE_FORM,
100                               sub_obje_form_start, def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_MULTIM_OBJE_TITL,
102                               sub_obje_titl_start, def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_MULTIM_OBJE_FILE,
104                               sub_obje_file_start, def_elt_end);
105 }
106
107 void UNREFALLFUNC(multimedia_link)(struct multimedia_link* obj)
108 {
109   if (obj) {
110     struct multimedia_link* runner;
111     for (runner = obj; runner; runner = runner->next) {
112       unref_xref_value(runner->reference);
113       UNREFALLFUNC(note_sub)(runner->note);
114       UNREFALLFUNC(user_data)(runner->extra);
115     }
116   }
117 }
118
119 void CLEANFUNC(multimedia_link)(struct multimedia_link* mm)
120 {
121   if (mm) {
122     SAFE_FREE(mm->form);
123     SAFE_FREE(mm->title);
124     SAFE_FREE(mm->file);
125     DESTROY_CHAIN_ELTS(note_sub, mm->note);
126     DESTROY_CHAIN_ELTS(user_data, mm->extra);
127   }
128 }
129
130 int write_multimedia_links(Gedcom_write_hndl hndl, int parent,
131                            struct multimedia_link* mm)
132 {
133   int result = 0;
134   struct multimedia_link* obj;
135
136   if (!mm) return 1;
137
138   for (obj = mm; obj; obj = obj->next) {
139     if (obj->reference) {
140       result |= gedcom_write_element_xref(hndl, ELT_SUB_MULTIM_OBJE, 0,
141                                           parent, obj->reference);
142     }
143     else {
144       result |= gedcom_write_element_str(hndl, ELT_SUB_MULTIM_OBJE, 0,
145                                          parent, NULL);
146       if (obj->form)
147         result |= gedcom_write_element_str(hndl, ELT_SUB_MULTIM_OBJE_FORM, 0,
148                                            ELT_SUB_MULTIM_OBJE, obj->form);
149       if (obj->title)
150         result |= gedcom_write_element_str(hndl, ELT_SUB_MULTIM_OBJE_TITL, 0,
151                                            ELT_SUB_MULTIM_OBJE, obj->title);
152       if (obj->file)
153         result |= gedcom_write_element_str(hndl, ELT_SUB_MULTIM_OBJE_FILE, 0,
154                                            ELT_SUB_MULTIM_OBJE, obj->file);
155       if (obj->note)
156         result |= write_note_subs(hndl, ELT_SUB_MULTIM_OBJE, obj->note);
157       if (obj->extra)
158         result |= write_user_data(hndl, obj->extra);
159     }
160   }
161
162   return result;
163 }