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.
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.
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.
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
24 #ifndef __GOM_INTERNAL_H
25 #define __GOM_INTERNAL_H
36 #define _(string) dgettext(PACKAGE, string)
37 #define N_(string) (string)
40 #define UNUSED __attribute__((unused))
48 T_header, T_submission, T_submitter, T_family, T_individual,
49 T_multimedia, T_note, T_repository, T_source, T_user_rec,
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
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
63 struct Gom_ctxt_struct {
69 typedef struct Gom_ctxt_struct *Gom_ctxt;
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);
78 int gom_write_xref_list(Gedcom_write_hndl hndl,
79 Gedcom_elt elt, int tag, int parent_rec_or_elt,
80 struct xref_list* val);
82 #define MAKE_GOM_CTXT(CTXT_TYPE, STRUCTTYPE, CTXT_PTR) \
83 make_gom_ctxt(CTXT_TYPE, T_ ## STRUCTTYPE, CTXT_PTR)
85 #define SAFE_CTXT_CAST(STRUCTTYPE, VAL) \
86 (((VAL)->obj_type == T_ ## STRUCTTYPE) ? \
88 (gom_cast_error(__FILE__, __LINE__, T_ ## STRUCTTYPE, (VAL)->obj_type), \
91 #define SAFE_FREE(PTR) \
97 #define UNEXPECTED_CONTEXT(CTXT_TYPE) \
98 gom_unexpected_context(__FILE__, __LINE__, CTXT_TYPE)
101 gom_no_context(__FILE__, __LINE__)
103 void gom_mem_error(const char *filename, int line);
105 #define MEMORY_ERROR gom_mem_error(__FILE__, __LINE__)
107 void def_rec_end(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value);
108 void def_elt_end(Gedcom_elt elt, Gedcom_ctxt parent,
109 Gedcom_ctxt self, Gedcom_val parsed_value);
110 void set_xref_type(struct xref_value *xr, const char* str);
112 /* Doubly-linked list, but last rec->next is NULL (doesn't go to first rec) */
113 #define LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
115 struct STRUCTTYPE *_local_obj = VAL; \
118 VAL->previous = _local_obj; \
123 FIRSTVAL->previous->next = VAL; \
124 VAL->previous = FIRSTVAL->previous; \
125 FIRSTVAL->previous = VAL; \
129 #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
131 VAL = (struct STRUCTTYPE*) malloc(sizeof(struct STRUCTTYPE)); \
135 memset (VAL, 0, sizeof(struct STRUCTTYPE)); \
136 LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
140 void NULL_DESTROY(void* anything);
142 #define DESTROY_CHAIN_ELTS(STRUCTTYPE, FIRSTVAL, DESTROYFUNC) \
145 struct STRUCTTYPE *runner, *next; \
148 next = runner->next; \
149 DESTROYFUNC(runner); \
156 #define _REC_PARAMS_ Gedcom_rec rec UNUSED, int level UNUSED, \
157 Gedcom_val xref UNUSED, char *tag UNUSED, \
158 char *raw_value UNUSED, int parsed_tag UNUSED, \
159 Gedcom_val parsed_value UNUSED
161 #define _REC_END_PARAMS_ Gedcom_rec rec UNUSED, Gedcom_ctxt self UNUSED, \
162 Gedcom_val parsed_value UNUSED
164 #define _ELT_PARAMS_ Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED, \
165 int level UNUSED, char *tag UNUSED, \
166 char *raw_value UNUSED, int parsed_tag UNUSED, \
167 Gedcom_val parsed_value UNUSED
169 #define _ELT_END_PARAMS_ Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED, \
170 Gedcom_ctxt self UNUSED, \
171 Gedcom_val parsed_value UNUSED
173 #define REC_CB(STRUCTTYPE,CB_NAME,FUNC) \
174 Gedcom_ctxt CB_NAME(_REC_PARAMS_) \
176 struct xref_value* xr = GEDCOM_XREF_PTR(xref); \
178 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
180 return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, STRUCTTYPE, xr->object); \
185 #define GET_REC_BY_XREF(STRUCTTYPE,XREF_TYPE,FUNC_NAME) \
186 struct STRUCTTYPE *FUNC_NAME(const char *xrefstr) \
188 struct xref_value* xr = gedcom_get_by_xref(xrefstr); \
189 if (xr && (xr->type == XREF_TYPE) && xr->object) \
190 return (struct STRUCTTYPE*)(xr->object); \
195 #define STRING_CB(STRUCTTYPE,CB_NAME,FIELD) \
196 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
198 Gom_ctxt result = NULL; \
202 struct STRUCTTYPE *obj \
203 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
205 char *str = GEDCOM_STRING(parsed_value); \
206 obj->FIELD = strdup(str); \
210 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
213 return (Gedcom_ctxt)result; \
216 #define STRING_END_CB(STRUCTTYPE,CB_NAME,FIELD) \
217 void CB_NAME(_ELT_END_PARAMS_) \
219 Gom_ctxt ctxt = (Gom_ctxt)self; \
223 struct STRUCTTYPE *obj = SAFE_CTXT_CAST(STRUCTTYPE, ctxt); \
225 char *str = GEDCOM_STRING(parsed_value); \
226 char *newvalue = strdup(str); \
230 obj->FIELD = newvalue; \
232 destroy_gom_ctxt(ctxt); \
236 #define STRING_END_REC_CB(STRUCTTYPE,CB_NAME,FIELD) \
237 void CB_NAME(_REC_END_PARAMS_) \
239 Gom_ctxt ctxt = (Gom_ctxt)self; \
243 struct STRUCTTYPE *obj = SAFE_CTXT_CAST(STRUCTTYPE, ctxt); \
245 char *str = GEDCOM_STRING(parsed_value); \
246 char *newvalue = strdup(str); \
250 obj->FIELD = newvalue; \
252 destroy_gom_ctxt(ctxt); \
256 #define DATE_CB(STRUCTTYPE,CB_NAME,FIELD) \
257 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
259 Gom_ctxt result = NULL; \
263 struct STRUCTTYPE *obj \
264 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
266 struct date_value dv = GEDCOM_DATE(parsed_value); \
267 obj->FIELD = gedcom_new_date_value(&dv); \
271 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
274 return (Gedcom_ctxt)result; \
277 #define AGE_CB(STRUCTTYPE,CB_NAME,FIELD) \
278 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
280 Gom_ctxt result = NULL; \
284 struct STRUCTTYPE *obj \
285 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
287 struct age_value age = GEDCOM_AGE(parsed_value); \
288 obj->FIELD = gedcom_new_age_value(&age); \
292 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
295 return (Gedcom_ctxt)result; \
298 #define XREF_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC) \
299 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
301 Gom_ctxt result = NULL; \
305 struct STRUCTTYPE *obj \
306 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
307 struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value); \
309 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
312 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
315 return (Gedcom_ctxt)result; \
318 #define XREF_LIST_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC) \
319 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
321 Gom_ctxt result = NULL; \
325 struct STRUCTTYPE *obj \
326 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
327 struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value); \
328 struct xref_list *xrl; \
330 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
332 MAKE_CHAIN_ELT(xref_list, obj->FIELD, xrl); \
335 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
339 return (Gedcom_ctxt)result; \
342 #define NULL_CB(STRUCTTYPE,CB_NAME) \
343 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
345 Gom_ctxt result = NULL; \
349 struct STRUCTTYPE *obj \
350 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
352 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
354 return (Gedcom_ctxt)result; \
357 #endif /* __GOM_INTERNAL_H */