Better checking of library result values.
[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   struct user_ref_number *refn = NULL;
42
43   if (ctxt) {
44     refn = (struct user_ref_number *)malloc(sizeof(struct user_ref_number));
45     memset (refn, 0, sizeof(struct user_ref_number));
46     refn->value = strdup(GEDCOM_STRING(parsed_value));
47
48     switch (ctxt->ctxt_type) {
49       case REC_FAM:
50         family_add_user_ref(ctxt, refn); break;
51       case REC_INDI:
52         individual_add_user_ref(ctxt, refn); break;
53       case REC_OBJE:
54         multimedia_add_user_ref(ctxt, refn); break;
55       case REC_NOTE:
56         note_add_user_ref(ctxt, refn); break;
57       case REC_REPO:
58         repository_add_user_ref(ctxt, refn); break;
59       case REC_SOUR:
60         source_add_user_ref(ctxt, refn); break;
61       default:
62         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
63     }
64   }
65
66   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, user_ref_number, refn);
67 }
68
69 STRING_CB(user_ref_number, sub_user_ref_type_start, type)
70      
71 Gedcom_ctxt sub_user_rin_start(_ELT_PARAMS_)
72 {
73   Gom_ctxt ctxt = (Gom_ctxt)parent;
74   if (ctxt) {
75     char *str = GEDCOM_STRING(parsed_value);
76
77     switch (ctxt->ctxt_type) {
78       case REC_FAM:
79         family_set_record_id(ctxt, str); break;
80       case REC_INDI:
81         individual_set_record_id(ctxt, str); break;
82       case REC_OBJE:
83         multimedia_set_record_id(ctxt, str); break;
84       case REC_NOTE:
85         note_set_record_id(ctxt, str); break;
86       case REC_REPO:
87         repository_set_record_id(ctxt, str); break;
88       case REC_SOUR:
89         source_set_record_id(ctxt, str); break;
90       default:
91         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
92     }
93   }
94   return (Gedcom_ctxt) make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
95 }
96
97 void user_ref_subscribe()
98 {
99   gedcom_subscribe_to_element(ELT_SUB_IDENT_REFN, sub_user_ref_start,
100                               def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_IDENT_REFN_TYPE, sub_user_ref_type_start,
102                               def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_IDENT_RIN, sub_user_rin_start,
104                               def_elt_end);
105 }
106
107 void user_ref_add_user_data(Gom_ctxt ctxt, struct user_data* data)
108 {
109   struct user_ref_number *obj = SAFE_CTXT_CAST(user_ref_number, ctxt);
110   LINK_CHAIN_ELT(user_data, obj->extra, data)
111 }
112
113 void user_ref_cleanup(struct user_ref_number* refn)
114 {
115   if (refn) {
116     SAFE_FREE(refn->value);
117     SAFE_FREE(refn->type);
118     DESTROY_CHAIN_ELTS(user_data, refn->extra, user_data_cleanup)
119   }
120 }