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