Allow elements out of context in GOM.
[gedcom-parse.git] / gom / source_description.c
1 /* Source description 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 "source_description.h"
27 #include "source.h"
28 #include "user_rec.h"
29 #include "gom.h"
30 #include "gedcom.h"
31 #include "gom_internal.h"
32
33 Gedcom_ctxt sub_sour_caln_start(_ELT_PARAMS_)
34 {
35   Gom_ctxt ctxt = (Gom_ctxt)parent;
36   Gom_ctxt result = NULL;
37
38   if (! ctxt)
39     NO_CONTEXT;
40   else {
41     struct source_description *desc = SUB_MAKEFUNC(source_description)();
42     if (desc) {
43       desc->call_number = strdup(GEDCOM_STRING(parsed_value));
44
45       if (! desc->call_number) {
46         MEMORY_ERROR;
47         free(desc);
48       }
49       else {
50         int type = ctxt_type(ctxt);
51         switch (type) {
52           case ELT_SUB_REPO:
53             ADDFUNC2(source,source_description)(ctxt, desc); break;
54           default:
55             UNEXPECTED_CONTEXT(type);
56         }
57         result = MAKE_GOM_CTXT(elt, source_description, desc);
58       }
59     }
60   }
61
62   return (Gedcom_ctxt)result;
63 }
64
65 DEFINE_SUB_MAKEFUNC(source_description)
66 DEFINE_SUB_ADDFUNC(source_description)
67 DEFINE_SUB_FINDFUNC(source_description)
68 DEFINE_SUB_REMOVEFUNC(source_description)
69 DEFINE_SUB_MOVEFUNC(source_description)
70      
71 DEFINE_STRING_CB(source_description, sub_sour_caln_medi_start, media)
72
73 DEFINE_ADDFUNC2(source_description, user_data, extra)
74      
75 void source_description_subscribe()
76 {
77   gedcom_subscribe_to_element(ELT_SUB_REPO_CALN, sub_sour_caln_start,
78                               def_elt_end);
79   gedcom_subscribe_to_element(ELT_SUB_REPO_CALN_MEDI,
80                               sub_sour_caln_medi_start, def_elt_end);
81 }
82
83 void UNREFALLFUNC(source_description)(struct source_description* obj)
84 {
85   if (obj) {
86     struct source_description* runner;
87     for (runner = obj; runner; runner = runner->next) {
88       UNREFALLFUNC(user_data)(runner->extra);
89     }
90   }
91 }
92
93 void CLEANFUNC(source_description)(struct source_description* desc)
94 {
95   if (desc) {
96     SAFE_FREE(desc->call_number);
97     SAFE_FREE(desc->media);
98     DESTROY_CHAIN_ELTS(user_data, desc->extra);
99   }
100 }
101
102 int write_source_descriptions(Gedcom_write_hndl hndl, int parent,
103                               struct source_description *desc)
104 {
105   int result = 0;
106   struct source_description* obj;
107
108   if (!desc) return 1;
109
110   for (obj = desc; obj; obj = obj->next) {
111     result |= gedcom_write_element_str(hndl, ELT_SUB_REPO_CALN, 0,
112                                        parent, obj->call_number);
113     if (obj->media)
114       result |= gedcom_write_element_str(hndl, ELT_SUB_REPO_CALN_MEDI, 0,
115                                          ELT_SUB_REPO_CALN, obj->media);
116     if (obj->extra)
117       result |= write_user_data(hndl, obj->extra);    
118   }
119
120   return result;
121 }