Removed dup_date and dup_age.
[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
42       = (struct personal_name *)malloc(sizeof(struct personal_name));
43     if (! name)
44       MEMORY_ERROR;
45     else {
46       memset (name, 0, sizeof(struct personal_name));
47       name->name = strdup(GEDCOM_STRING(parsed_value));
48
49       if (! name->name) {
50         MEMORY_ERROR;
51         free(name);
52       }
53       else {
54         switch (ctxt->ctxt_type) {
55           case REC_INDI:
56             individual_add_name(ctxt, name); break;
57           default:
58             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
59         }
60         result = MAKE_GOM_CTXT(elt, personal_name, name);
61       }
62     }
63   }
64
65   return (Gedcom_ctxt)result;
66 }
67
68 STRING_CB(personal_name, sub_name_npfx_start, prefix)
69 STRING_CB(personal_name, sub_name_givn_start, given)
70 STRING_CB(personal_name, sub_name_nick_start, nickname)
71 STRING_CB(personal_name, sub_name_spfx_start, surname_prefix)
72 STRING_CB(personal_name, sub_name_surn_start, surname)
73 STRING_CB(personal_name, sub_name_nsfx_start, suffix)
74
75 void name_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
76 {
77   struct personal_name *name = SAFE_CTXT_CAST(personal_name, ctxt);
78   if (name)
79     LINK_CHAIN_ELT(source_citation, name->citation, cit);  
80 }
81
82 void name_add_note(Gom_ctxt ctxt, struct note_sub* note)
83 {
84   struct personal_name *name = SAFE_CTXT_CAST(personal_name, ctxt);
85   if (name)
86     LINK_CHAIN_ELT(note_sub, name->note, note);  
87 }
88
89 void name_add_user_data(Gom_ctxt ctxt, struct user_data* data)
90 {
91   struct personal_name *obj = SAFE_CTXT_CAST(personal_name, ctxt);
92   if (obj)
93     LINK_CHAIN_ELT(user_data, obj->extra, data);
94 }
95
96 void name_subscribe()
97 {
98   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME, sub_name_start, def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NPFX, sub_name_npfx_start,
100                               def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_GIVN, sub_name_givn_start,
102                               def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NICK, sub_name_nick_start,
104                               def_elt_end);
105   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SPFX, sub_name_spfx_start,
106                               def_elt_end);
107   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN, sub_name_surn_start,
108                               def_elt_end);
109   gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_NSFX, sub_name_nsfx_start,
110                               def_elt_end);
111 }
112
113 void name_cleanup(struct personal_name* name)
114 {
115   if (name) {
116     SAFE_FREE(name->name);
117     SAFE_FREE(name->prefix);
118     SAFE_FREE(name->given);
119     SAFE_FREE(name->nickname);
120     SAFE_FREE(name->surname_prefix);
121     SAFE_FREE(name->surname);
122     SAFE_FREE(name->suffix);
123     DESTROY_CHAIN_ELTS(source_citation, name->citation, citation_cleanup);
124     DESTROY_CHAIN_ELTS(note_sub, name->note, note_sub_cleanup);
125     DESTROY_CHAIN_ELTS(user_data, name->extra, user_data_cleanup);
126   }
127 }
128
129 int write_names(Gedcom_write_hndl hndl, int parent,
130                 struct personal_name *name)
131 {
132   int result = 0;
133   struct personal_name* obj;
134
135   if (!name) return 1;
136
137   for (obj = name; obj; obj = obj->next) {
138     result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME, 0,
139                                        parent, obj->name);
140     if (obj->prefix)
141       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NPFX, 0,
142                                          parent, obj->prefix);
143     if (obj->given)
144       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_GIVN, 0,
145                                          parent, obj->given);
146     if (obj->nickname)
147       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NICK, 0,
148                                          parent, obj->nickname);
149     if (obj->surname_prefix)
150       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_SPFX, 0,
151                                          parent, obj->surname_prefix);
152     if (obj->surname)
153       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_SURN, 0,
154                                          parent, obj->surname);
155     if (obj->suffix)
156       result |= gedcom_write_element_str(hndl, ELT_SUB_PERS_NAME_NSFX, 0,
157                                          parent, obj->suffix);
158     if (obj->citation)
159       result |= write_citations(hndl, ELT_SUB_PERS_NAME, obj->citation);
160     if (obj->note)
161       result |= write_note_subs(hndl, ELT_SUB_PERS_NAME, obj->note);
162     if (obj->extra)
163       result |= write_user_data(hndl, obj->extra);
164   }
165
166   return result;
167 }