Allow elements out of context in GOM.
[gedcom-parse.git] / gom / user_ref.c
1 /* User reference number 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 "user_ref.h"
27 #include "family.h"
28 #include "individual.h"
29 #include "multimedia.h"
30 #include "note.h"
31 #include "repository.h"
32 #include "source.h"
33 #include "user_rec.h"
34 #include "gom.h"
35 #include "gedcom.h"
36 #include "gom_internal.h"
37
38 Gedcom_ctxt sub_user_ref_start(_ELT_PARAMS_)
39 {
40   Gom_ctxt ctxt = (Gom_ctxt)parent;
41   Gom_ctxt result = NULL;
42
43   if (! ctxt)
44     NO_CONTEXT;
45   else {
46     struct user_ref_number *refn = SUB_MAKEFUNC(user_ref_number)();
47     if (refn) {
48       refn->value = strdup(GEDCOM_STRING(parsed_value));
49       if (! refn->value) {
50         MEMORY_ERROR;
51         free(refn);
52       }
53       else {
54         int type = ctxt_type(ctxt);
55         switch (type) {
56           case REC_FAM:
57             ADDFUNC2(family,user_ref_number)(ctxt, refn); break;
58           case REC_INDI:
59             ADDFUNC2(individual,user_ref_number)(ctxt, refn); break;
60           case REC_OBJE:
61             ADDFUNC2(multimedia,user_ref_number)(ctxt, refn); break;
62           case REC_NOTE:
63             ADDFUNC2(note,user_ref_number)(ctxt, refn); break;
64           case REC_REPO:
65             ADDFUNC2(repository,user_ref_number)(ctxt, refn); break;
66           case REC_SOUR:
67             ADDFUNC2(source,user_ref_number)(ctxt, refn); break;
68           default:
69             UNEXPECTED_CONTEXT(type);
70         }
71         
72         result = MAKE_GOM_CTXT(elt, user_ref_number, refn);
73       }
74     }
75   }
76
77   return (Gedcom_ctxt)result;
78 }
79
80 DEFINE_SUB_MAKEFUNC(user_ref_number)
81 DEFINE_SUB_ADDFUNC(user_ref_number)
82 DEFINE_SUB_FINDFUNC(user_ref_number)
83 DEFINE_SUB_REMOVEFUNC(user_ref_number)
84 DEFINE_SUB_MOVEFUNC(user_ref_number)
85      
86 DEFINE_STRING_CB(user_ref_number, sub_user_ref_type_start, type)
87
88 DEFINE_ADDFUNC2(user_ref_number, user_data, extra)
89      
90 Gedcom_ctxt sub_user_rin_start(_ELT_PARAMS_)
91 {
92   Gom_ctxt ctxt = (Gom_ctxt)parent;
93   Gom_ctxt result = NULL;
94   
95   if (! ctxt)
96     NO_CONTEXT;
97   else {
98     char *str = GEDCOM_STRING(parsed_value);
99     int type = ctxt_type(ctxt);
100
101     switch (type) {
102       case REC_FAM:
103         ADDFUNC2_STR(family,record_id)(ctxt, str); break;
104       case REC_INDI:
105         ADDFUNC2_STR(individual,record_id)(ctxt, str); break;
106       case REC_OBJE:
107         ADDFUNC2_STR(multimedia,record_id)(ctxt, str); break;
108       case REC_NOTE:
109         ADDFUNC2_STR(note,record_id)(ctxt, str); break;
110       case REC_REPO:
111         ADDFUNC2_STR(repository,record_id)(ctxt, str); break;
112       case REC_SOUR:
113         ADDFUNC2_STR(source,record_id)(ctxt, str); break;
114       default:
115         UNEXPECTED_CONTEXT(type);
116     }
117     result = dup_gom_ctxt(ctxt, elt);
118   }
119   return (Gedcom_ctxt)result;
120 }
121
122 void user_ref_subscribe()
123 {
124   gedcom_subscribe_to_element(ELT_SUB_IDENT_REFN, sub_user_ref_start,
125                               def_elt_end);
126   gedcom_subscribe_to_element(ELT_SUB_IDENT_REFN_TYPE, sub_user_ref_type_start,
127                               def_elt_end);
128   gedcom_subscribe_to_element(ELT_SUB_IDENT_RIN, sub_user_rin_start,
129                               def_elt_end);
130 }
131
132 void UNREFALLFUNC(user_ref_number)(struct user_ref_number* obj)
133 {
134   if (obj) {
135     struct user_ref_number* runner;
136     for (runner = obj; runner; runner = runner->next)
137       UNREFALLFUNC(user_data)(runner->extra);
138   }
139 }
140
141 void CLEANFUNC(user_ref_number)(struct user_ref_number* refn)
142 {
143   if (refn) {
144     SAFE_FREE(refn->value);
145     SAFE_FREE(refn->type);
146     DESTROY_CHAIN_ELTS(user_data, refn->extra);
147   }
148 }
149
150 int write_user_refs(Gedcom_write_hndl hndl, int parent,
151                     struct user_ref_number *refn)
152 {
153   int result = 0;
154   struct user_ref_number* obj;
155
156   if (!refn) return 1;
157
158   for (obj = refn; obj; obj = obj->next) {
159     result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_REFN, 0,
160                                        parent, obj->value);
161     if (obj->type)
162       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_REFN_TYPE, 0,
163                                          ELT_SUB_IDENT_REFN, obj->type);
164     if (obj->extra)
165       result |= write_user_data(hndl, obj->extra);    
166   }
167
168   return result;
169 }