A little simplification in the write interface.
[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 REC_CB(multimedia, obje_start, make_multimedia_record)
38 GET_REC_BY_XREF(multimedia, XREF_OBJE, gom_get_multimedia_by_xref)
39 STRING_CB(multimedia, obje_form_start, form)
40 STRING_CB(multimedia, obje_titl_start, title)     
41 NULL_CB(multimedia, obje_blob_start)
42 STRING_END_CB(multimedia, obje_blob_end, data)
43 XREF_CB(multimedia, obje_obje_start, continued, make_multimedia_record)
44
45 Gedcom_ctxt obje_blob_cont_start(_ELT_PARAMS_)
46 {
47   Gom_ctxt ctxt = (Gom_ctxt)parent;
48   Gom_ctxt result = NULL;
49
50   if (! ctxt)
51     NO_CONTEXT;
52   else
53     result = make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
54   
55   return (Gedcom_ctxt)result;
56 }
57
58 void multimedia_subscribe()
59 {
60   gedcom_subscribe_to_record(REC_OBJE, obje_start, def_rec_end);
61   gedcom_subscribe_to_element(ELT_OBJE_FORM, obje_form_start, def_elt_end);
62   gedcom_subscribe_to_element(ELT_OBJE_TITL, obje_titl_start, def_elt_end);
63   gedcom_subscribe_to_element(ELT_OBJE_BLOB, obje_blob_start, obje_blob_end);
64   gedcom_subscribe_to_element(ELT_OBJE_BLOB_CONT, obje_blob_cont_start,
65                               def_elt_end);
66   gedcom_subscribe_to_element(ELT_OBJE_OBJE, obje_obje_start, def_elt_end);
67 }
68
69 void multimedia_add_note(Gom_ctxt ctxt, struct note_sub* note)
70 {
71   struct multimedia* obj = SAFE_CTXT_CAST(multimedia, ctxt);
72   if (obj)
73     LINK_CHAIN_ELT(note_sub, obj->note, note);
74 }
75
76 void multimedia_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
77 {
78   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
79   if (obj)
80     LINK_CHAIN_ELT(user_ref_number, obj->ref, ref);
81 }
82
83 void multimedia_set_record_id(Gom_ctxt ctxt, const char *rin)
84 {
85   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
86   if (obj) {
87     obj->record_id = strdup(rin);
88     if (! obj->record_id) MEMORY_ERROR;
89   }
90 }
91
92 void multimedia_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
93 {
94   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
95   if (obj)
96     obj->change_date = chan;
97 }
98
99 void multimedia_add_user_data(Gom_ctxt ctxt, struct user_data* data)
100 {
101   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
102   if (obj)
103     LINK_CHAIN_ELT(user_data, obj->extra, data);
104 }
105
106 void multimedia_cleanup(struct multimedia* obj)
107 {
108   if (obj) {
109     SAFE_FREE(obj->xrefstr);
110     SAFE_FREE(obj->form);
111     SAFE_FREE(obj->title);
112     DESTROY_CHAIN_ELTS(note_sub, obj->note, note_sub_cleanup);
113     SAFE_FREE(obj->data);
114     DESTROY_CHAIN_ELTS(user_ref_number, obj->ref, user_ref_cleanup);
115     SAFE_FREE(obj->record_id);
116     change_date_cleanup(obj->change_date);
117     DESTROY_CHAIN_ELTS(user_data, obj->extra, user_data_cleanup);
118   }
119 }
120
121 void multimedias_cleanup()
122 {
123   DESTROY_CHAIN_ELTS(multimedia, gom_first_multimedia, multimedia_cleanup);
124 }
125
126 struct multimedia* gom_get_first_multimedia()
127 {
128   return gom_first_multimedia;
129 }
130
131 struct multimedia* make_multimedia_record(const char* xrefstr)
132 {
133   struct multimedia* multi = NULL;
134   MAKE_CHAIN_ELT(multimedia, gom_first_multimedia, multi);
135   if (multi) {
136     multi->xrefstr = strdup(xrefstr);
137     if (! multi->xrefstr) MEMORY_ERROR;
138   }
139   return multi;
140 }
141
142 int write_multimedia_recs(Gedcom_write_hndl hndl)
143 {
144   int result = 0;
145   struct multimedia* obj;
146
147   for (obj = gom_first_multimedia; obj; obj = obj->next) {
148     result |= gedcom_write_record_str(hndl, REC_OBJE, obj->xrefstr, NULL);
149     if (obj->form)
150       result |= gedcom_write_element_str(hndl, ELT_OBJE_FORM, 0,
151                                          REC_OBJE, obj->form);
152     if (obj->title)
153       result |= gedcom_write_element_str(hndl, ELT_OBJE_TITL, 0,
154                                          REC_OBJE, obj->title);
155     if (obj->note)
156       result |= write_note_subs(hndl, REC_OBJE, obj->note);
157     if (obj->data)
158       result |= gedcom_write_element_str(hndl, ELT_OBJE_BLOB, 0,
159                                          REC_OBJE, obj->data);
160     if (obj->continued)
161       result |= gedcom_write_element_xref(hndl, ELT_OBJE_OBJE, 0,
162                                           REC_OBJE, obj->continued);
163     if (obj->ref)
164       result |= write_user_refs(hndl, REC_OBJE, obj->ref);
165     if (obj->record_id)
166       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
167                                          REC_OBJE, obj->record_id);
168     if (obj->change_date)
169       result |= write_change_date(hndl, REC_OBJE, obj->change_date);
170     if (obj->extra)
171       result |= write_user_data(hndl, obj->extra);
172   }
173   
174   return result;
175 }
176