Little updates.
[gedcom-parse.git] / gom / gom.c
1 /* Main file for building 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 <stdio.h>
27 #include "gedcom.h"
28 #include "header.h"
29 #include "submitter.h"
30 #include "submission.h"
31 #include "family.h"
32 #include "individual.h"
33 #include "multimedia.h"
34 #include "note.h"
35 #include "repository.h"
36 #include "source.h"
37 #include "user_rec.h"
38 #include "address.h"
39 #include "event.h"
40 #include "place.h"
41 #include "source_citation.h"
42 #include "multimedia_link.h"
43 #include "note_sub.h"
44 #include "lds_event.h"
45 #include "user_ref.h"
46 #include "change_date.h"
47 #include "personal_name.h"
48 #include "family_link.h"
49 #include "association.h"
50 #include "source_event.h"
51 #include "source_description.h"
52 #include "gom.h"
53 #include "gom_internal.h"
54
55 void gom_default_callback (Gedcom_elt elt, Gedcom_ctxt parent, int level,
56                            char* tag, char* raw_value, int parsed_tag);
57
58 void gom_cleanup()
59 {
60   header_cleanup();
61   submission_cleanup();
62   families_cleanup();
63   individuals_cleanup();
64   multimedias_cleanup();
65   notes_cleanup();
66   repositories_cleanup();
67   sources_cleanup();
68   submitters_cleanup();
69   user_recs_cleanup();
70 }
71
72 void subscribe_all()
73 {
74   gedcom_set_default_callback(gom_default_callback);
75   header_subscribe();
76   submission_subscribe();
77   family_subscribe();
78   individual_subscribe();
79   multimedia_subscribe();
80   note_subscribe();
81   repository_subscribe();
82   source_subscribe();
83   submitter_subscribe();
84   user_rec_subscribe();
85   
86   address_subscribe();
87   event_subscribe();
88   place_subscribe();
89   citation_subscribe();
90   note_sub_subscribe();
91   multimedia_link_subscribe();
92   lds_event_subscribe();
93   user_ref_subscribe();
94   change_date_subscribe();
95   name_subscribe();
96   family_link_subscribe();
97   association_subscribe();
98   source_event_subscribe();
99   source_description_subscribe();
100
101   if (atexit(gom_cleanup) != 0) {
102     gedcom_warning(_("Could not register gom cleanup function"));
103   }
104 }
105
106 int gom_parse_file(const char* file_name)
107 {
108   subscribe_all();
109   return gedcom_parse_file(file_name);
110 }
111
112 int gom_new_model()
113 {
114   subscribe_all();
115   return gedcom_new_model();
116 }
117
118 Gom_ctxt make_gom_ctxt(int ctxt_type, OBJ_TYPE obj_type, void *ctxt_ptr)
119 {
120   Gom_ctxt ctxt   = (Gom_ctxt)malloc(sizeof(struct Gom_ctxt_struct));
121   if (! ctxt)
122     MEMORY_ERROR;
123   else {
124     ctxt->ctxt_type = ctxt_type;
125     ctxt->obj_type  = obj_type;
126     ctxt->ctxt_ptr  = ctxt_ptr;
127   }
128   return ctxt;
129 }
130
131 void NULL_DESTROY(void* anything UNUSED)
132 {
133 }
134
135 void destroy_gom_ctxt(Gom_ctxt ctxt)
136 {
137   SAFE_FREE(ctxt);
138 }
139
140 void gom_cast_error(const char* file, int line,
141                     OBJ_TYPE expected, OBJ_TYPE found)
142 {
143   fprintf(stderr,
144           "Wrong gom ctxt cast at %s, line %d: expected %d, found %d\n",
145           file, line, expected, found);
146   abort();
147 }
148
149 void gom_mem_error(const char *filename, int line)
150 {
151   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
152 }
153
154 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found)
155 {
156   gedcom_warning(_("Internal error: Unexpected context at %s, line %d: %d"),
157                  file, line, found);
158 }
159
160 void gom_no_context(const char* file, int line)
161 {
162   gedcom_warning(_("Internal error: No context at %s, line %d"),
163                  file, line);
164 }
165
166 void gom_default_callback (Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
167                            int level, char* tag, char* raw_value,
168                            int parsed_tag UNUSED)
169 {
170   gedcom_warning(_("Data loss in import: \"%d %s %s\""),
171                  level, tag, raw_value);
172 }
173
174 void def_rec_end(Gedcom_rec rec UNUSED, Gedcom_ctxt self)
175 {
176   Gom_ctxt ctxt = (Gom_ctxt)self;
177   destroy_gom_ctxt(ctxt);
178 }
179
180 void def_elt_end(Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
181                  Gedcom_ctxt self, Gedcom_val parsed_value UNUSED)
182 {
183   Gom_ctxt ctxt = (Gom_ctxt)self;
184   destroy_gom_ctxt(ctxt);
185 }
186
187 void set_xref_type(struct xref_value* xr, const char *str)
188 {
189   if (!strcasecmp(str, "FAM"))
190     xr->type = XREF_FAM;
191   else if (!strcasecmp(str, "INDI"))
192     xr->type = XREF_INDI;
193   else if (!strcasecmp(str, "NOTE"))
194     xr->type = XREF_NOTE;
195   else if (!strcasecmp(str, "OBJE"))
196     xr->type = XREF_OBJE;
197   else if (!strcasecmp(str, "REPO"))
198     xr->type = XREF_REPO;
199   else if (!strcasecmp(str, "SOUR"))
200     xr->type = XREF_SOUR;
201   else if (!strcasecmp(str, "SUBM"))
202     xr->type = XREF_SUBM;
203   else if (!strcasecmp(str, "SUBN"))
204     xr->type = XREF_SUBN;
205   else
206     xr->type = XREF_ANY;
207 }
208
209 char* concat_strings(NL_TYPE type, char *str1, const char *str2)
210 {
211   if (str1 != NULL && str2 != NULL) {
212     char *newp;
213     char *wp;
214     size_t len1 = strlen(str1);
215     size_t len2 = strlen(str2);
216     size_t len  = len1 + len2 + 1;
217     if (type == WITH_NL)
218       len++;
219     newp = (char*) realloc(str1, len);
220     if (newp == NULL)
221       return NULL;
222     wp   = newp + len1;
223     str1 = newp;
224     if (type == WITH_NL)
225       *wp++ = '\n';
226     wp = memcpy (wp, str2, len2);
227     wp += len2;
228     *wp++ = '\0';
229   }
230
231   return str1;
232 }
233
234 struct date_value* dup_date(struct date_value dv)
235 {
236   struct date_value* dv_ptr;
237   dv_ptr = (struct date_value*) malloc(sizeof(struct date_value));
238   if (! dv_ptr)
239     MEMORY_ERROR;
240   else {
241     memcpy(dv_ptr, &dv, sizeof(struct date_value));
242   }
243   return dv_ptr;
244 }
245
246 struct age_value* dup_age(struct age_value age)
247 {
248   struct age_value* age_ptr;
249   age_ptr = (struct age_value*) malloc(sizeof(struct age_value));
250   if (! age_ptr)
251     MEMORY_ERROR;
252   else {
253     memcpy(age_ptr, &age, sizeof(struct age_value));
254   }
255   return age_ptr;
256 }