Moved get/set string to gom_modify.c.
[gedcom-parse.git] / gom / submitter.c
1 /* Submitter object 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 "submitter.h"
27 #include "address.h"
28 #include "multimedia_link.h"
29 #include "change_date.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 struct submitter* gom_first_submitter = NULL;
36
37 REC_CB(submitter, subm_start, make_submitter_record)
38 GET_REC_BY_XREF(submitter, XREF_SUBM, gom_get_submitter_by_xref)
39 STRING_CB(submitter, subm_name_start, name)
40 STRING_CB(submitter, subm_rfn_start, record_file_nr)
41 STRING_CB(submitter, subm_rin_start, record_id)
42
43 Gedcom_ctxt subm_lang_start(_ELT_PARAMS_)
44 {
45   Gom_ctxt ctxt = (Gom_ctxt)parent;
46   Gom_ctxt result = NULL;
47
48   if (! ctxt)
49     NO_CONTEXT;
50   else {
51     struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
52
53     if (subm) {
54       int err = 0;
55       char *str = GEDCOM_STRING(parsed_value);
56       int i = 0;
57
58       while (i<2 && subm->language[i]) i++;
59       if (! subm->language[i]) {
60         subm->language[i] = strdup(str);
61         if (! subm->language[i]) {
62           MEMORY_ERROR;
63           err = 1;
64         }
65       }
66       if (! err)
67         result = MAKE_GOM_CTXT(elt, submitter, subm);
68     }
69   }
70   
71   return (Gedcom_ctxt)result;
72 }
73
74 void submitter_subscribe()
75 {
76   gedcom_subscribe_to_record(REC_SUBM, subm_start, def_rec_end);
77   gedcom_subscribe_to_element(ELT_SUBM_NAME, subm_name_start, def_elt_end);
78   gedcom_subscribe_to_element(ELT_SUBM_LANG, subm_lang_start, def_elt_end);
79   gedcom_subscribe_to_element(ELT_SUBM_RFN, subm_rfn_start, def_elt_end);
80   gedcom_subscribe_to_element(ELT_SUBM_RIN, subm_rin_start, def_elt_end);
81 }
82
83 void submitter_add_address(Gom_ctxt ctxt, struct address* address)
84 {
85   struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
86   if (subm)
87     subm->address = address;
88 }
89
90 void submitter_add_phone(Gom_ctxt ctxt, const char *phone)
91 {
92   struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
93   if (subm) {
94     int i = 0;
95     while (i<2 && subm->phone[i]) i++;
96     if (! subm->phone[i]) {
97       subm->phone[i] = strdup(phone);
98       if (! subm->phone[i]) MEMORY_ERROR;
99     }
100   }
101 }
102
103 void submitter_add_mm_link(Gom_ctxt ctxt, struct multimedia_link* link)
104 {
105   struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
106   if (subm)
107     LINK_CHAIN_ELT(multimedia_link, subm->mm_link, link);
108 }
109
110 void submitter_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
111 {
112   struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
113   if (subm)
114     subm->change_date = chan;
115 }
116
117 void submitter_add_user_data(Gom_ctxt ctxt, struct user_data* data)
118 {
119   struct submitter *obj = SAFE_CTXT_CAST(submitter, ctxt);
120   if (obj)
121     LINK_CHAIN_ELT(user_data, obj->extra, data);
122 }
123
124 void submitter_cleanup(struct submitter* rec)
125 {
126   if (rec) {
127     SAFE_FREE(rec->xrefstr);
128     SAFE_FREE(rec->name);
129     address_cleanup(rec->address);
130     SAFE_FREE(rec->phone[0]);
131     SAFE_FREE(rec->phone[1]);
132     SAFE_FREE(rec->phone[2]);
133     DESTROY_CHAIN_ELTS(multimedia_link, rec->mm_link, multimedia_link_cleanup);
134     SAFE_FREE(rec->language[0]);
135     SAFE_FREE(rec->language[1]);
136     SAFE_FREE(rec->language[2]);
137     SAFE_FREE(rec->record_file_nr);
138     SAFE_FREE(rec->record_id);
139     change_date_cleanup(rec->change_date);
140     DESTROY_CHAIN_ELTS(user_data, rec->extra, user_data_cleanup);
141   }
142 }
143
144 void submitters_cleanup()
145 {
146   DESTROY_CHAIN_ELTS(submitter, gom_first_submitter, submitter_cleanup);
147 }
148
149 struct submitter* gom_get_first_submitter()
150 {
151   return gom_first_submitter;
152 }
153
154 struct submitter* make_submitter_record(const char* xrefstr)
155 {
156   struct submitter* subm = NULL;
157   MAKE_CHAIN_ELT(submitter, gom_first_submitter, subm);
158   if (subm) {
159     subm->xrefstr = strdup(xrefstr);
160     if (!subm->xrefstr) MEMORY_ERROR;
161   }
162   return subm;
163 }