Moved common code to gom_internal.h
[gedcom-parse.git] / gom / gom_internal.h
1 /* General header for 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 #ifndef __GOM_INTERNAL_H
25 #define __GOM_INTERNAL_H
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <libintl.h>
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include "gom.h"
34 #include "gedcom.h"
35       
36 #define _(string) dgettext(PACKAGE, string)
37 #define N_(string) (string)
38
39 #ifdef __GNUC__
40 #define UNUSED __attribute__((unused))
41 #else
42 #define UNUSED
43 #endif
44
45 typedef enum {
46   T_NULL,
47   
48   T_header, T_submission, T_submitter, T_family, T_individual,
49   T_multimedia, T_note, T_repository, T_source, T_user_rec,
50   
51   T_address, T_event, T_place, T_source_citation, T_text,
52   T_note_sub, T_multimedia_link, T_lds_event, T_user_ref_number,
53   T_change_date, T_personal_name, T_family_link, T_pedigree,
54   T_association, T_source_event, T_source_description
55 } OBJ_TYPE;
56
57 /* Assumptions for context:
58     - In case of error, NULL is passed as context
59     - If not NULL, the ctxt_ptr of the context is not NULL also
60     - UNEXPECTED_CONTEXT is not treated as an error, but as a warning
61 */
62
63 struct Gom_ctxt_struct {
64   int ctxt_type;
65   OBJ_TYPE obj_type;
66   void* ctxt_ptr;
67 };
68
69 typedef struct Gom_ctxt_struct *Gom_ctxt;
70
71 Gom_ctxt make_gom_ctxt(int ctxt_type, OBJ_TYPE obj_type, void *ctxt_ptr);
72 void destroy_gom_ctxt(Gom_ctxt ctxt);
73 void gom_cast_error(const char* file, int line,
74                     OBJ_TYPE expected, OBJ_TYPE found);
75 void gom_no_context(const char* file, int line);
76 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found);
77
78 #define MAKE_GOM_CTXT(CTXT_TYPE, STRUCTTYPE, CTXT_PTR)                        \
79   make_gom_ctxt(CTXT_TYPE, T_ ## STRUCTTYPE, CTXT_PTR)
80
81 #define SAFE_CTXT_CAST(STRUCTTYPE, VAL)                                       \
82   (((VAL)->obj_type == T_ ## STRUCTTYPE) ?                                    \
83    (VAL)->ctxt_ptr :                                                          \
84    (gom_cast_error(__FILE__, __LINE__, T_ ## STRUCTTYPE, (VAL)->obj_type),    \
85     (VAL)->ctxt_ptr))
86
87 #define SAFE_FREE(PTR)                                                        \
88   if (PTR) {                                                                  \
89     free(PTR);                                                                \
90     PTR = NULL;                                                               \
91   }
92
93 #define UNEXPECTED_CONTEXT(CTXT_TYPE)                                         \
94   gom_unexpected_context(__FILE__, __LINE__, CTXT_TYPE)
95
96 #define NO_CONTEXT                                                            \
97   gom_no_context(__FILE__, __LINE__)
98
99 void gom_mem_error(const char *filename, int line);
100
101 #define MEMORY_ERROR gom_mem_error(__FILE__, __LINE__)
102
103 void def_rec_end(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value);
104 void def_elt_end(Gedcom_elt elt, Gedcom_ctxt parent,
105                  Gedcom_ctxt self, Gedcom_val parsed_value);
106 void set_xref_type(struct xref_value *xr, const char* str);
107
108 struct date_value* dup_date(struct date_value dv);
109 struct age_value*  dup_age(struct age_value age);
110
111 /* Doubly-linked list, but last rec->next is NULL (doesn't go to first rec) */
112 #define LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL)                             \
113   {                                                                           \
114     struct STRUCTTYPE *_local_obj = VAL;                                      \
115     if (! FIRSTVAL) {                                                         \
116       VAL->next = NULL;                                                       \
117       VAL->previous = _local_obj;                                             \
118       FIRSTVAL = VAL;                                                         \
119     }                                                                         \
120     else {                                                                    \
121       VAL->next = NULL;                                                       \
122       FIRSTVAL->previous->next = VAL;                                         \
123       VAL->previous = FIRSTVAL->previous;                                     \
124       FIRSTVAL->previous = VAL;                                               \
125     }                                                                         \
126   }
127
128 #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL)                             \
129   {                                                                           \
130     VAL = (struct STRUCTTYPE*) malloc(sizeof(struct STRUCTTYPE));             \
131     if (! VAL)                                                                \
132       MEMORY_ERROR;                                                           \
133     else {                                                                    \
134       memset (VAL, 0, sizeof(struct STRUCTTYPE));                             \
135       LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL)                               \
136     }                                                                         \
137   }
138
139 void NULL_DESTROY(void* anything);
140
141 #define DESTROY_CHAIN_ELTS(STRUCTTYPE, FIRSTVAL, DESTROYFUNC)                 \
142   {                                                                           \
143     if (FIRSTVAL) {                                                           \
144       struct STRUCTTYPE *runner, *next;                                       \
145       runner = FIRSTVAL;                                                      \
146       while (runner) {                                                        \
147         next = runner->next;                                                  \
148         DESTROYFUNC(runner);                                                  \
149         SAFE_FREE(runner);                                                    \
150         runner = next;                                                        \
151       }                                                                       \
152     }                                                                         \
153   }
154
155 #define _REC_PARAMS_ Gedcom_rec rec UNUSED, int level UNUSED,                 \
156                      Gedcom_val xref UNUSED, char *tag UNUSED,                \
157                      char *raw_value UNUSED, int parsed_tag UNUSED,           \
158                      Gedcom_val parsed_value UNUSED
159
160 #define _REC_END_PARAMS_ Gedcom_rec rec UNUSED, Gedcom_ctxt self UNUSED,      \
161                          Gedcom_val parsed_value UNUSED
162
163 #define _ELT_PARAMS_ Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,        \
164                      int level UNUSED, char *tag UNUSED,                      \
165                      char *raw_value UNUSED, int parsed_tag UNUSED,           \
166                      Gedcom_val parsed_value UNUSED
167
168 #define _ELT_END_PARAMS_ Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,    \
169                          Gedcom_ctxt self UNUSED,                             \
170                          Gedcom_val parsed_value UNUSED
171
172 #define REC_CB(STRUCTTYPE,CB_NAME,FUNC)                                       \
173   Gedcom_ctxt CB_NAME(_REC_PARAMS_)                                           \
174   {                                                                           \
175     struct xref_value* xr = GEDCOM_XREF_PTR(xref);                            \
176     if (! xr->object)                                                         \
177       xr->object = (Gedcom_ctxt) FUNC(xr->string);                            \
178     if (xr->object)                                                           \
179       return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, STRUCTTYPE, xr->object);        \
180     else                                                                      \
181       return NULL;                                                            \
182   }
183
184 #define GET_REC_BY_XREF(STRUCTTYPE,XREF_TYPE,FUNC_NAME)                       \
185   struct STRUCTTYPE *FUNC_NAME(const char *xrefstr)                           \
186   {                                                                           \
187     struct xref_value* xr = gedcom_get_by_xref(xrefstr);                      \
188     if (xr && (xr->type == XREF_TYPE) && xr->object)                          \
189       return (struct STRUCTTYPE*)(xr->object);                                \
190     else                                                                      \
191       return NULL;                                                            \
192   }
193
194 #define STRING_CB(STRUCTTYPE,CB_NAME,FIELD)                                   \
195   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
196   {                                                                           \
197     Gom_ctxt result = NULL;                                                   \
198     if (! parent)                                                             \
199       NO_CONTEXT;                                                             \
200     else {                                                                    \
201       struct STRUCTTYPE *obj                                                  \
202         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
203       if (obj) {                                                              \
204         char *str = GEDCOM_STRING(parsed_value);                              \
205         obj->FIELD = strdup(str);                                             \
206         if (! obj->FIELD)                                                     \
207           MEMORY_ERROR;                                                       \
208         else                                                                  \
209           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
210       }                                                                       \
211     }                                                                         \
212     return (Gedcom_ctxt)result;                                               \
213   }
214
215 #define STRING_END_CB(STRUCTTYPE,CB_NAME,FIELD)                               \
216   void CB_NAME(_ELT_END_PARAMS_)                                              \
217   {                                                                           \
218     Gom_ctxt ctxt = (Gom_ctxt)self;                                           \
219     if (! ctxt)                                                               \
220       NO_CONTEXT;                                                             \
221     else {                                                                    \
222       struct STRUCTTYPE *obj = SAFE_CTXT_CAST(STRUCTTYPE, ctxt);              \
223       if (obj) {                                                              \
224         char *str = GEDCOM_STRING(parsed_value);                              \
225         char *newvalue = strdup(str);                                         \
226         if (! newvalue)                                                       \
227           MEMORY_ERROR;                                                       \
228         else                                                                  \
229           obj->FIELD = newvalue;                                              \
230       }                                                                       \
231       destroy_gom_ctxt(ctxt);                                                 \
232     }                                                                         \
233   }
234
235 #define STRING_END_REC_CB(STRUCTTYPE,CB_NAME,FIELD)                           \
236   void CB_NAME(_REC_END_PARAMS_)                                              \
237   {                                                                           \
238     Gom_ctxt ctxt = (Gom_ctxt)self;                                           \
239     if (! ctxt)                                                               \
240       NO_CONTEXT;                                                             \
241     else {                                                                    \
242       struct STRUCTTYPE *obj = SAFE_CTXT_CAST(STRUCTTYPE, ctxt);              \
243       if (obj) {                                                              \
244         char *str = GEDCOM_STRING(parsed_value);                              \
245         char *newvalue = strdup(str);                                         \
246         if (! newvalue)                                                       \
247           MEMORY_ERROR;                                                       \
248         else                                                                  \
249           obj->FIELD = newvalue;                                              \
250       }                                                                       \
251       destroy_gom_ctxt(ctxt);                                                 \
252     }                                                                         \
253   }
254
255 #define DATE_CB(STRUCTTYPE,CB_NAME,FIELD)                                     \
256   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
257   {                                                                           \
258     Gom_ctxt result = NULL;                                                   \
259     if (! parent)                                                             \
260       NO_CONTEXT;                                                             \
261     else {                                                                    \
262       struct STRUCTTYPE *obj                                                  \
263         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
264       if (obj) {                                                              \
265         struct date_value dv = GEDCOM_DATE(parsed_value);                     \
266         obj->FIELD = dup_date(dv);                                            \
267         if (! obj->FIELD)                                                     \
268           MEMORY_ERROR;                                                       \
269         else                                                                  \
270           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
271       }                                                                       \
272     }                                                                         \
273     return (Gedcom_ctxt)result;                                               \
274   }
275
276 #define AGE_CB(STRUCTTYPE,CB_NAME,FIELD)                                      \
277   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
278   {                                                                           \
279     Gom_ctxt result = NULL;                                                   \
280     if (! parent)                                                             \
281       NO_CONTEXT;                                                             \
282     else {                                                                    \
283       struct STRUCTTYPE *obj                                                  \
284         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
285       if (obj) {                                                              \
286         struct age_value age = GEDCOM_AGE(parsed_value);                      \
287         obj->FIELD = dup_age(age);                                            \
288         if (! obj->FIELD)                                                     \
289           MEMORY_ERROR;                                                       \
290         else                                                                  \
291           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
292       }                                                                       \
293     }                                                                         \
294     return (Gedcom_ctxt)result;                                               \
295   }
296
297 #define XREF_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC)                                \
298   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
299   {                                                                           \
300     Gom_ctxt result = NULL;                                                   \
301     if (! parent)                                                             \
302       NO_CONTEXT;                                                             \
303     else {                                                                    \
304       struct STRUCTTYPE *obj                                                  \
305         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
306       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);                  \
307       if (! xr->object)                                                       \
308         xr->object = (Gedcom_ctxt) FUNC(xr->string);                          \
309       if (obj) {                                                              \
310         obj->FIELD = xr;                                                      \
311         result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                         \
312       }                                                                       \
313     }                                                                         \
314     return (Gedcom_ctxt)result;                                               \
315   }
316
317 #define XREF_LIST_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC)                           \
318   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
319   {                                                                           \
320     Gom_ctxt result = NULL;                                                   \
321     if (! parent)                                                             \
322       NO_CONTEXT;                                                             \
323     else {                                                                    \
324       struct STRUCTTYPE *obj                                                  \
325         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
326       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);                  \
327       struct xref_list *xrl;                                                  \
328       if (! xr->object)                                                       \
329         xr->object = (Gedcom_ctxt) FUNC(xr->string);                          \
330       if (obj) {                                                              \
331         MAKE_CHAIN_ELT(xref_list, obj->FIELD, xrl);                           \
332         if (xrl) {                                                            \
333           xrl->xref = xr;                                                     \
334           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
335         }                                                                     \
336       }                                                                       \
337     }                                                                         \
338     return (Gedcom_ctxt)result;                                               \
339   }
340
341 #define NULL_CB(STRUCTTYPE,CB_NAME)                                           \
342   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
343   {                                                                           \
344     Gom_ctxt result = NULL;                                                   \
345     if (! parent)                                                             \
346       NO_CONTEXT;                                                             \
347     else {                                                                    \
348       struct STRUCTTYPE *obj                                                  \
349         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
350       if (obj)                                                                \
351         result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                         \
352     }                                                                         \
353     return (Gedcom_ctxt)result;                                               \
354   }
355
356 #endif /* __GOM_INTERNAL_H */