More checking on validity of strings.
[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 XREF_CB(multimedia, obje_obje_start, continued, make_multimedia_record)
43
44 Gedcom_ctxt obje_blob_cont_start(_ELT_PARAMS_)
45 {
46   Gom_ctxt ctxt = (Gom_ctxt)parent;
47   Gom_ctxt result = NULL;
48
49   if (! ctxt)
50     NO_CONTEXT;
51   else {
52     struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
53     if (obj) {
54       char *str = GEDCOM_STRING(parsed_value);
55       if (obj->data) {
56         char *newvalue = concat_strings (WITHOUT_NL, obj->data, str);
57         if (newvalue)
58           obj->data = newvalue;
59         else {
60           free(obj->data);
61           obj->data = NULL;
62         }
63       }
64       else
65         obj->data = strdup(str);
66       
67       if (! obj->data) {
68         MEMORY_ERROR;
69         free(obj);
70       }
71       else
72         result = make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
73     }
74   }
75   return (Gedcom_ctxt)result;
76 }
77
78 void multimedia_subscribe()
79 {
80   gedcom_subscribe_to_record(REC_OBJE, obje_start, def_rec_end);
81   gedcom_subscribe_to_element(ELT_OBJE_FORM, obje_form_start, def_elt_end);
82   gedcom_subscribe_to_element(ELT_OBJE_TITL, obje_titl_start, def_elt_end);
83   gedcom_subscribe_to_element(ELT_OBJE_BLOB, obje_blob_start, def_elt_end);
84   gedcom_subscribe_to_element(ELT_OBJE_BLOB_CONT, obje_blob_cont_start,
85                               def_elt_end);
86   gedcom_subscribe_to_element(ELT_OBJE_OBJE, obje_obje_start, def_elt_end);
87 }
88
89 void multimedia_add_note(Gom_ctxt ctxt, struct note_sub* note)
90 {
91   struct multimedia* obj = SAFE_CTXT_CAST(multimedia, ctxt);
92   if (obj)
93     LINK_CHAIN_ELT(note_sub, obj->note, note);
94 }
95
96 void multimedia_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
97 {
98   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
99   if (obj)
100     LINK_CHAIN_ELT(user_ref_number, obj->ref, ref);
101 }
102
103 void multimedia_set_record_id(Gom_ctxt ctxt, const char *rin)
104 {
105   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
106   if (obj) {
107     obj->record_id = strdup(rin);
108     if (! obj->record_id) MEMORY_ERROR;
109   }
110 }
111
112 void multimedia_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
113 {
114   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
115   if (obj)
116     obj->change_date = chan;
117 }
118
119 void multimedia_add_user_data(Gom_ctxt ctxt, struct user_data* data)
120 {
121   struct multimedia *obj = SAFE_CTXT_CAST(multimedia, ctxt);
122   if (obj)
123     LINK_CHAIN_ELT(user_data, obj->extra, data);
124 }
125
126 void multimedia_cleanup(struct multimedia* obj)
127 {
128   if (obj) {
129     SAFE_FREE(obj->xrefstr);
130     SAFE_FREE(obj->form);
131     SAFE_FREE(obj->title);
132     DESTROY_CHAIN_ELTS(note_sub, obj->note, note_sub_cleanup);
133     SAFE_FREE(obj->data);
134     DESTROY_CHAIN_ELTS(user_ref_number, obj->ref, user_ref_cleanup);
135     SAFE_FREE(obj->record_id);
136     change_date_cleanup(obj->change_date);
137     DESTROY_CHAIN_ELTS(user_data, obj->extra, user_data_cleanup);
138   }
139 }
140
141 void multimedias_cleanup()
142 {
143   DESTROY_CHAIN_ELTS(multimedia, gom_first_multimedia, multimedia_cleanup);
144 }
145
146 struct multimedia* gom_get_first_multimedia()
147 {
148   return gom_first_multimedia;
149 }
150
151 struct multimedia* make_multimedia_record(const char* xrefstr)
152 {
153   struct multimedia* multi = NULL;
154   MAKE_CHAIN_ELT(multimedia, gom_first_multimedia, multi);
155   if (multi) {
156     multi->xrefstr = strdup(xrefstr);
157     if (! multi->xrefstr) MEMORY_ERROR;
158   }
159   return multi;
160 }