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 #define MAKE_GOM_CTXT(CTXT_TYPE, STRUCTTYPE, CTXT_PTR) \
79 make_gom_ctxt(CTXT_TYPE, T_ ## STRUCTTYPE, CTXT_PTR)
81 #define SAFE_CTXT_CAST(STRUCTTYPE, VAL) \
82 (((VAL)->obj_type == T_ ## STRUCTTYPE) ? \
84 (gom_cast_error(__FILE__, __LINE__, T_ ## STRUCTTYPE, (VAL)->obj_type), \
87 #define SAFE_FREE(PTR) \
93 #define UNEXPECTED_CONTEXT(CTXT_TYPE) \
94 gom_unexpected_context(__FILE__, __LINE__, CTXT_TYPE)
97 gom_no_context(__FILE__, __LINE__)
99 void gom_mem_error(const char *filename, int line);
101 #define MEMORY_ERROR gom_mem_error(__FILE__, __LINE__)
103 void def_rec_end(Gedcom_rec rec, Gedcom_ctxt self);
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);
113 char* concat_strings(NL_TYPE type, char *str1, const char *str2);
114 struct date_value* dup_date(struct date_value dv);
115 struct age_value* dup_age(struct age_value age);
117 /* Doubly-linked list, but last rec->next is NULL (doesn't go to first rec) */
118 #define LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
120 struct STRUCTTYPE *_local_obj = VAL; \
123 VAL->previous = _local_obj; \
128 FIRSTVAL->previous->next = VAL; \
129 VAL->previous = FIRSTVAL->previous; \
130 FIRSTVAL->previous = VAL; \
134 #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
136 VAL = (struct STRUCTTYPE*) malloc(sizeof(struct STRUCTTYPE)); \
140 memset (VAL, 0, sizeof(struct STRUCTTYPE)); \
141 LINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \
145 void NULL_DESTROY(void* anything);
147 #define DESTROY_CHAIN_ELTS(STRUCTTYPE, FIRSTVAL, DESTROYFUNC) \
150 struct STRUCTTYPE *runner, *next; \
153 next = runner->next; \
154 DESTROYFUNC(runner); \
161 #define _REC_PARAMS_ Gedcom_rec rec UNUSED, int level UNUSED, \
162 Gedcom_val xref UNUSED, char *tag UNUSED, \
163 char *raw_value UNUSED, int parsed_tag UNUSED, \
164 Gedcom_val parsed_value UNUSED
166 #define _ELT_PARAMS_ Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED, \
167 int level UNUSED, char *tag UNUSED, \
168 char *raw_value UNUSED, int parsed_tag UNUSED, \
169 Gedcom_val parsed_value UNUSED
171 #define REC_CB(STRUCTTYPE,CB_NAME,FUNC) \
172 Gedcom_ctxt CB_NAME(_REC_PARAMS_) \
174 struct xref_value* xr = GEDCOM_XREF_PTR(xref); \
176 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
178 return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, STRUCTTYPE, xr->object); \
183 #define GET_REC_BY_XREF(STRUCTTYPE,XREF_TYPE,FUNC_NAME) \
184 struct STRUCTTYPE *FUNC_NAME(const char *xrefstr) \
186 struct xref_value* xr = gedcom_get_by_xref(xrefstr); \
187 if (xr && (xr->type == XREF_TYPE) && xr->object) \
188 return (struct STRUCTTYPE*)(xr->object); \
193 #define STRING_CB(STRUCTTYPE,CB_NAME,FIELD) \
194 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
196 Gom_ctxt result = NULL; \
200 struct STRUCTTYPE *obj \
201 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
203 char *str = GEDCOM_STRING(parsed_value); \
204 obj->FIELD = strdup(str); \
208 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
211 return (Gedcom_ctxt)result; \
214 #define DATE_CB(STRUCTTYPE,CB_NAME,FIELD) \
215 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
217 Gom_ctxt result = NULL; \
221 struct STRUCTTYPE *obj \
222 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
224 struct date_value dv = GEDCOM_DATE(parsed_value); \
225 obj->FIELD = dup_date(dv); \
229 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
232 return (Gedcom_ctxt)result; \
235 #define AGE_CB(STRUCTTYPE,CB_NAME,FIELD) \
236 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
238 Gom_ctxt result = NULL; \
242 struct STRUCTTYPE *obj \
243 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
245 struct age_value age = GEDCOM_AGE(parsed_value); \
246 obj->FIELD = dup_age(age); \
250 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
253 return (Gedcom_ctxt)result; \
256 #define XREF_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC) \
257 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
259 Gom_ctxt result = NULL; \
263 struct STRUCTTYPE *obj \
264 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
265 struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value); \
267 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
270 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
273 return (Gedcom_ctxt)result; \
276 #define XREF_LIST_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC) \
277 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
279 Gom_ctxt result = NULL; \
283 struct STRUCTTYPE *obj \
284 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
285 struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value); \
286 struct xref_list *xrl; \
288 xr->object = (Gedcom_ctxt) FUNC(xr->string); \
290 MAKE_CHAIN_ELT(xref_list, obj->FIELD, xrl); \
293 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
297 return (Gedcom_ctxt)result; \
300 #define NULL_CB(STRUCTTYPE,CB_NAME) \
301 Gedcom_ctxt CB_NAME(_ELT_PARAMS_) \
303 Gom_ctxt result = NULL; \
307 struct STRUCTTYPE *obj \
308 = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent); \
310 result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj); \
312 return (Gedcom_ctxt)result; \
315 #endif /* __GOM_INTERNAL_H */