Moved get/set string to gom_modify.c.
[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 typedef enum {
40   T_NULL,
41   
42   T_header, T_submission, T_submitter, T_family, T_individual,
43   T_multimedia, T_note, T_repository, T_source, T_user_rec,
44   
45   T_address, T_event, T_place, T_source_citation, T_text,
46   T_note_sub, T_multimedia_link, T_lds_event, T_user_ref_number,
47   T_change_date, T_personal_name, T_family_link, T_pedigree,
48   T_association, T_source_event, T_source_description
49 } OBJ_TYPE;
50
51 /* Assumptions for context:
52     - In case of error, NULL is passed as context
53     - If not NULL, the ctxt_ptr of the context is not NULL also
54     - UNEXPECTED_CONTEXT is not treated as an error, but as a warning
55 */
56
57 struct Gom_ctxt_struct {
58   int ctxt_type;
59   OBJ_TYPE obj_type;
60   void* ctxt_ptr;
61 };
62
63 typedef struct Gom_ctxt_struct *Gom_ctxt;
64
65 Gom_ctxt make_gom_ctxt(int ctxt_type, OBJ_TYPE obj_type, void *ctxt_ptr);
66 void destroy_gom_ctxt(Gom_ctxt ctxt);
67 void gom_cast_error(const char* file, int line,
68                     OBJ_TYPE expected, OBJ_TYPE found);
69 void gom_no_context(const char* file, int line);
70 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found);
71
72 #define MAKE_GOM_CTXT(CTXT_TYPE, STRUCTTYPE, CTXT_PTR)                        \
73   make_gom_ctxt(CTXT_TYPE, T_ ## STRUCTTYPE, CTXT_PTR)
74
75 #define SAFE_CTXT_CAST(STRUCTTYPE, VAL)                                       \
76   (((VAL)->obj_type == T_ ## STRUCTTYPE) ?                                    \
77    (VAL)->ctxt_ptr :                                                          \
78    (gom_cast_error(__FILE__, __LINE__, T_ ## STRUCTTYPE, (VAL)->obj_type),    \
79     (VAL)->ctxt_ptr))
80
81 #define SAFE_FREE(PTR)                                                        \
82   if (PTR) {                                                                  \
83     free(PTR);                                                                \
84     PTR = NULL;                                                               \
85   }
86
87 #define UNEXPECTED_CONTEXT(CTXT_TYPE)                                         \
88   gom_unexpected_context(__FILE__, __LINE__, CTXT_TYPE)
89
90 #define NO_CONTEXT                                                            \
91   gom_no_context(__FILE__, __LINE__)
92
93 void gom_mem_error(const char *filename, int line);
94
95 #define MEMORY_ERROR gom_mem_error(__FILE__, __LINE__)
96
97 void def_rec_end(Gedcom_rec rec, Gedcom_ctxt self);
98 void def_elt_end(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
99                  Gedcom_val parsed_value);
100 void set_xref_type(struct xref_value *xr, const char* str);
101
102 typedef enum {
103   WITHOUT_NL,
104   WITH_NL
105 } NL_TYPE;
106
107 char* concat_strings(NL_TYPE type, char *str1, const char *str2);
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, int level, Gedcom_val xref, char *tag,   \
156                      char *raw_value, int parsed_tag, Gedcom_val parsed_value
157
158 #define _ELT_PARAMS_ Gedcom_elt elt, Gedcom_ctxt parent, int level, char *tag,\
159                      char *raw_value, int parsed_tag, Gedcom_val parsed_value
160
161 #define REC_CB(STRUCTTYPE,CB_NAME,FUNC)                                       \
162   Gedcom_ctxt CB_NAME(_REC_PARAMS_)                                           \
163   {                                                                           \
164     struct xref_value* xr = GEDCOM_XREF_PTR(xref);                            \
165     if (! xr->object)                                                         \
166       xr->object = (Gedcom_ctxt) FUNC(xr->string);                            \
167     if (xr->object)                                                           \
168       return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, STRUCTTYPE, xr->object);        \
169     else                                                                      \
170       return NULL;                                                            \
171   }
172
173 #define GET_REC_BY_XREF(STRUCTTYPE,XREF_TYPE,FUNC_NAME)                       \
174   struct STRUCTTYPE *FUNC_NAME(const char *xrefstr)                           \
175   {                                                                           \
176     struct xref_value* xr = gedcom_get_by_xref(xrefstr);                      \
177     if (xr && (xr->type == XREF_TYPE) && xr->object)                          \
178       return (struct STRUCTTYPE*)(xr->object);                                \
179     else                                                                      \
180       return NULL;                                                            \
181   }
182
183 #define STRING_CB(STRUCTTYPE,CB_NAME,FIELD)                                   \
184   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
185   {                                                                           \
186     Gom_ctxt result = NULL;                                                   \
187     if (! parent)                                                             \
188       NO_CONTEXT;                                                             \
189     else {                                                                    \
190       struct STRUCTTYPE *obj                                                  \
191         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
192       if (obj) {                                                              \
193         char *str = GEDCOM_STRING(parsed_value);                              \
194         obj->FIELD = strdup(str);                                             \
195         if (! obj->FIELD)                                                     \
196           MEMORY_ERROR;                                                       \
197         else                                                                  \
198           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
199       }                                                                       \
200     }                                                                         \
201     return (Gedcom_ctxt)result;                                               \
202   }
203
204 #define DATE_CB(STRUCTTYPE,CB_NAME,FIELD)                                     \
205   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
206   {                                                                           \
207     Gom_ctxt result = NULL;                                                   \
208     if (! parent)                                                             \
209       NO_CONTEXT;                                                             \
210     else {                                                                    \
211       struct STRUCTTYPE *obj                                                  \
212         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
213       if (obj) {                                                              \
214         struct date_value dv = GEDCOM_DATE(parsed_value);                     \
215         obj->FIELD = dup_date(dv);                                            \
216         if (! obj->FIELD)                                                     \
217           MEMORY_ERROR;                                                       \
218         else                                                                  \
219           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
220       }                                                                       \
221     }                                                                         \
222     return (Gedcom_ctxt)result;                                               \
223   }
224
225 #define AGE_CB(STRUCTTYPE,CB_NAME,FIELD)                                      \
226   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
227   {                                                                           \
228     Gom_ctxt result = NULL;                                                   \
229     if (! parent)                                                             \
230       NO_CONTEXT;                                                             \
231     else {                                                                    \
232       struct STRUCTTYPE *obj                                                  \
233         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
234       if (obj) {                                                              \
235         struct age_value age = GEDCOM_AGE(parsed_value);                      \
236         obj->FIELD = dup_age(age);                                            \
237         if (! obj->FIELD)                                                     \
238           MEMORY_ERROR;                                                       \
239         else                                                                  \
240           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
241       }                                                                       \
242     }                                                                         \
243     return (Gedcom_ctxt)result;                                               \
244   }
245
246 #define XREF_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC)                                \
247   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
248   {                                                                           \
249     Gom_ctxt result = NULL;                                                   \
250     if (! parent)                                                             \
251       NO_CONTEXT;                                                             \
252     else {                                                                    \
253       struct STRUCTTYPE *obj                                                  \
254         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
255       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);                  \
256       if (! xr->object)                                                       \
257         xr->object = (Gedcom_ctxt) FUNC(xr->string);                          \
258       if (obj) {                                                              \
259         obj->FIELD = xr;                                                      \
260         result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                         \
261       }                                                                       \
262     }                                                                         \
263     return (Gedcom_ctxt)result;                                               \
264   }
265
266 #define XREF_LIST_CB(STRUCTTYPE,CB_NAME,FIELD,FUNC)                           \
267   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
268   {                                                                           \
269     Gom_ctxt result = NULL;                                                   \
270     if (! parent)                                                             \
271       NO_CONTEXT;                                                             \
272     else {                                                                    \
273       struct STRUCTTYPE *obj                                                  \
274         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
275       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);                  \
276       struct xref_list *xrl;                                                  \
277       if (! xr->object)                                                       \
278         xr->object = (Gedcom_ctxt) FUNC(xr->string);                          \
279       if (obj) {                                                              \
280         MAKE_CHAIN_ELT(xref_list, obj->FIELD, xrl);                           \
281         if (xrl) {                                                            \
282           xrl->xref = xr;                                                     \
283           result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                       \
284         }                                                                     \
285       }                                                                       \
286     }                                                                         \
287     return (Gedcom_ctxt)result;                                               \
288   }
289
290 #define NULL_CB(STRUCTTYPE,CB_NAME)                                           \
291   Gedcom_ctxt CB_NAME(_ELT_PARAMS_)                                           \
292   {                                                                           \
293     Gom_ctxt result = NULL;                                                   \
294     if (! parent)                                                             \
295       NO_CONTEXT;                                                             \
296     else {                                                                    \
297       struct STRUCTTYPE *obj                                                  \
298         = SAFE_CTXT_CAST(STRUCTTYPE, (Gom_ctxt)parent);                       \
299       if (obj)                                                                \
300         result = MAKE_GOM_CTXT(elt, STRUCTTYPE, obj);                         \
301     }                                                                         \
302     return (Gedcom_ctxt)result;                                               \
303   }
304
305 #endif /* __GOM_INTERNAL_H */