Allow elements out of context in GOM.
[gedcom-parse.git] / gom / multimedia.c
1 /* Multimedia object 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.h"
27 #include "note_sub.h"
28 #include "user_ref.h"
29 #include "change_date.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 struct multimedia* gom_first_multimedia = NULL;
36
37 DEFINE_MAKEFUNC(multimedia, gom_first_multimedia)
38 DEFINE_DESTROYFUNC(multimedia, gom_first_multimedia)
39 DEFINE_ADDFUNC(multimedia, XREF_OBJE)
40 DEFINE_DELETEFUNC(multimedia)
41 DEFINE_GETXREFFUNC(multimedia, XREF_OBJE)
42      
43 DEFINE_REC_CB(multimedia, obje_start)
44 DEFINE_STRING_CB(multimedia, obje_form_start, form)
45 DEFINE_STRING_CB(multimedia, obje_titl_start, title)     
46 DEFINE_NULL_CB(multimedia, obje_blob_start)
47 DEFINE_STRING_END_CB(multimedia, obje_blob_end, data)
48 DEFINE_XREF_CB(multimedia, obje_obje_start, continued, multimedia)
49
50 DEFINE_ADDFUNC2(multimedia, note_sub, note)
51 DEFINE_ADDFUNC2(multimedia, user_ref_number, ref)
52 DEFINE_ADDFUNC2(multimedia, user_data, extra)
53 DEFINE_ADDFUNC2_NOLIST(multimedia, change_date, change_date)
54 DEFINE_ADDFUNC2_STR(multimedia, record_id)
55
56 Gedcom_ctxt obje_blob_cont_start(_ELT_PARAMS_)
57 {
58   Gom_ctxt ctxt = (Gom_ctxt)parent;
59   Gom_ctxt result = NULL;
60
61   if (! ctxt)
62     NO_CONTEXT;
63   else
64     result = dup_gom_ctxt(ctxt, elt);
65   
66   return (Gedcom_ctxt)result;
67 }
68
69 void multimedia_subscribe()
70 {
71   gedcom_subscribe_to_record(REC_OBJE, obje_start, def_rec_end);
72   gedcom_subscribe_to_element(ELT_OBJE_FORM, obje_form_start, def_elt_end);
73   gedcom_subscribe_to_element(ELT_OBJE_TITL, obje_titl_start, def_elt_end);
74   gedcom_subscribe_to_element(ELT_OBJE_BLOB, obje_blob_start, obje_blob_end);
75   gedcom_subscribe_to_element(ELT_OBJE_BLOB_CONT, obje_blob_cont_start,
76                               def_elt_end);
77   gedcom_subscribe_to_element(ELT_OBJE_OBJE, obje_obje_start, def_elt_end);
78 }
79
80 void UNREFALLFUNC(multimedia)(struct multimedia *obj)
81 {
82   if (obj) {
83     UNREFALLFUNC(note_sub)(obj->note);
84     unref_xref_value(obj->continued);
85     UNREFALLFUNC(user_ref_number)(obj->ref);
86     UNREFALLFUNC(change_date)(obj->change_date);
87     UNREFALLFUNC(user_data)(obj->extra);
88   }
89 }
90
91 void CLEANFUNC(multimedia)(struct multimedia* obj)
92 {
93   if (obj) {
94     SAFE_FREE(obj->xrefstr);
95     SAFE_FREE(obj->form);
96     SAFE_FREE(obj->title);
97     DESTROY_CHAIN_ELTS(note_sub, obj->note);
98     SAFE_FREE(obj->data);
99     DESTROY_CHAIN_ELTS(user_ref_number, obj->ref);
100     SAFE_FREE(obj->record_id);
101     CLEANFUNC(change_date)(obj->change_date);
102     DESTROY_CHAIN_ELTS(user_data, obj->extra);
103   }
104 }
105
106 void multimedias_cleanup()
107 {
108   DESTROY_CHAIN_ELTS(multimedia, gom_first_multimedia);
109 }
110
111 struct multimedia* gom_get_first_multimedia()
112 {
113   return gom_first_multimedia;
114 }
115
116 int write_multimedia_recs(Gedcom_write_hndl hndl)
117 {
118   int result = 0;
119   struct multimedia* obj;
120
121   for (obj = gom_first_multimedia; obj; obj = obj->next) {
122     result |= gedcom_write_record_str(hndl, REC_OBJE, obj->xrefstr, NULL);
123     if (obj->form)
124       result |= gedcom_write_element_str(hndl, ELT_OBJE_FORM, 0,
125                                          REC_OBJE, obj->form);
126     if (obj->title)
127       result |= gedcom_write_element_str(hndl, ELT_OBJE_TITL, 0,
128                                          REC_OBJE, obj->title);
129     if (obj->note)
130       result |= write_note_subs(hndl, REC_OBJE, obj->note);
131     if (obj->data)
132       result |= gedcom_write_element_str(hndl, ELT_OBJE_BLOB, 0,
133                                          REC_OBJE, obj->data);
134     if (obj->continued)
135       result |= gedcom_write_element_xref(hndl, ELT_OBJE_OBJE, 0,
136                                           REC_OBJE, obj->continued);
137     if (obj->ref)
138       result |= write_user_refs(hndl, REC_OBJE, obj->ref);
139     if (obj->record_id)
140       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
141                                          REC_OBJE, obj->record_id);
142     if (obj->change_date)
143       result |= write_change_date(hndl, REC_OBJE, obj->change_date);
144     if (obj->extra)
145       result |= write_user_data(hndl, obj->extra);
146   }
147   
148   return result;
149 }
150