Allow elements out of context in GOM.
[gedcom-parse.git] / gom / personal_name.c
1 /* Personal name 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 "personal_name.h"
27 #include "source_citation.h"
28 #include "note_sub.h"
29 #include "individual.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 Gedcom_ctxt sub_name_start(_ELT_PARAMS_)
36 {
37   Gom_ctxt ctxt = (Gom_ctxt)parent;
38   Gom_ctxt result = NULL;
39
40   if (ctxt) {
41     struct personal_name *name = SUB_MAKEFUNC(personal_name)();
42     if (name) {
43       name->name = strdup(GEDCOM_STRING(parsed_value));
44
45       if (! name->name) {
46         MEMORY_ERROR;
47         free(name);
48       }
49       else {
50         int type = ctxt_type(ctxt);
51         switch (type) {
52           case REC_INDI:
53             ADDFUNC2(individual,personal_name)(ctxt, name); break;
54           default:
55             UNEXPECTED_CONTEXT(type);
56         }
57         result = MAKE_GOM_CTXT(elt, personal_name, name);
58       }
59     }
60   }
61
62   return (Gedcom_ctxt)result;
63 }
64
65 DEFINE_SUB_MAKEFUNC(personal_name)
66 DEFINE_SUB_ADDFUNC(personal_name)
67 DEFINE_SUB_FINDFUNC(personal_name)
68 DEFINE_SUB_REMOVEFUNC(personal_name)
69 DEFINE_SUB_MOVEFUNC(personal_name)
70      
71 DEFINE_STRING_CB(personal_name, sub_name_npfx_start, prefix)
72 DEFINE_STRING_CB(personal_name, sub_name_givn_start, given)
73 DEFINE_STRING_CB(personal_name, sub_name_nick_start, nickname)
74 DEFINE_STRING_CB(personal_name, sub_name_spfx_start, surname_prefix)
75 DEFINE_STRING_CB(personal_name, sub_name_surn_start, surname)
76 DEFINE_STRING_CB(personal_name, sub_name_nsfx_start, suffix)
77
78 DEFINE_ADDFUNC2(personal_name, source_citation, citation)
79 DEFINE_ADDFUNC2(personal_name, note_sub, note)
80 DEFINE_ADDFUNC2(personal_name, user_data, extra)
81
82 void name_subscribe()
83 {
84   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME, sub_name_start, def_elt_end);
85   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NPFX, sub_name_npfx_start,
86                               def_elt_end);
87   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_GIVN, sub_name_givn_start,
88                               def_elt_end);
89   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NICK, sub_name_nick_start,
90                               def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SPFX, sub_name_spfx_start,
92                               def_elt_end);
93   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN, sub_name_surn_start,
94                               def_elt_end);
95   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NSFX, sub_name_nsfx_start,
96                               def_elt_end);
97 }
98
99 void UNREFALLFUNC(personal_name)(struct personal_name* obj)
100 {
101   if (obj) {
102     struct personal_name* runner;
103     for (runner = obj; runner; runner = runner->next) {
104       UNREFALLFUNC(source_citation)(runner->citation);
105       UNREFALLFUNC(note_sub)(runner->note);
106       UNREFALLFUNC(user_data)(runner->extra);
107     }
108   }
109 }
110
111 void CLEANFUNC(personal_name)(struct personal_name* name)
112 {
113   if (name) {
114     SAFE_FREE(name->name);
115     SAFE_FREE(name->prefix);
116     SAFE_FREE(name->given);
117     SAFE_FREE(name->nickname);
118     SAFE_FREE(name->surname_prefix);
119     SAFE_FREE(name->surname);
120     SAFE_FREE(name->suffix);
121     DESTROY_CHAIN_ELTS(source_citation, name->citation);
122     DESTROY_CHAIN_ELTS(note_sub, name->note);
123     DESTROY_CHAIN_ELTS(user_data, name->extra);
124   }
125 }
126
127 int write_names(Gedcom_write_hndl hndl, int parent,
128                 struct personal_name *name)
129 {
130   int result = 0;
131   struct personal_name* obj;
132
133   if (!name) return 1;
134
135   for (obj = name; obj; obj = obj->next) {
136     result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME, 0,
137                                        parent, obj->name);
138     if (obj->prefix)
139       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NPFX, 0,
140                                          ELT_SUB_PERS_NAME, obj->prefix);
141     if (obj->given)
142       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_GIVN, 0,
143                                          ELT_SUB_PERS_NAME, obj->given);
144     if (obj->nickname)
145       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NICK, 0,
146                                          ELT_SUB_PERS_NAME, obj->nickname);
147     if (obj->surname_prefix)
148       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_SPFX, 0,
149                                          ELT_SUB_PERS_NAME,
150                                          obj->surname_prefix);
151     if (obj->surname)
152       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_SURN, 0,
153                                          ELT_SUB_PERS_NAME, obj->surname);
154     if (obj->suffix)
155       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NSFX, 0,
156                                          ELT_SUB_PERS_NAME, obj->suffix);
157     if (obj->citation)
158       result |= write_citations(hndl, ELT_SUB_PERS_NAME, obj->citation);
159     if (obj->note)
160       result |= write_note_subs(hndl, ELT_SUB_PERS_NAME, obj->note);
161     if (obj->extra)
162       result |= write_user_data(hndl, obj->extra);
163   }
164
165   return result;
166 }