Split off context stuff from gom.c
[gedcom-parse.git] / gom / gom_internal.c
1 /* Internals for building 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 #include "gom.h"
25 #include "gom_internal.h"
26
27 const char* ctxt_names[] =
28 {
29   "NULL",
30   
31   "header", "submission", "submitter", "family", "individual",
32   "multimedia", "note", "repository", "source", "user_rec",
33   
34   "address", "event", "place", "source_citation", "text",
35   "note_sub", "multimedia_link", "lds_event", "user_ref_number",
36   "change_date", "personal_name", "family_link", "pedigree",
37   "association", "source_event", "source_description"
38 };
39
40 Gom_ctxt make_gom_ctxt(int ctxt_type, OBJ_TYPE obj_type, void *ctxt_ptr)
41 {
42   Gom_ctxt ctxt   = (Gom_ctxt)malloc(sizeof(struct Gom_ctxt_struct));
43   if (! ctxt)
44     MEMORY_ERROR;
45   else {
46     ctxt->ctxt_type = ctxt_type;
47     ctxt->obj_type  = obj_type;
48     ctxt->ctxt_ptr  = ctxt_ptr;
49   }
50   return ctxt;
51 }
52
53 void NULL_DESTROY(void* anything UNUSED)
54 {
55 }
56
57 void destroy_gom_ctxt(Gom_ctxt ctxt)
58 {
59   SAFE_FREE(ctxt);
60 }
61
62 void gom_cast_error(const char* file, int line,
63                     OBJ_TYPE expected, OBJ_TYPE found)
64 {
65   const char* expected_name = "<out-of-bounds>";
66   const char* found_name    = "<out-of-bounds>";
67   if (expected < T_LAST)
68     expected_name = ctxt_names[expected];
69   if (found < T_LAST)
70     found_name = ctxt_names[found];
71   fprintf(stderr,
72           "Wrong gom ctxt cast at %s, line %d: expected %s, found %s\n",
73           file, line, expected_name, found_name);
74   abort();
75 }
76
77 void gom_mem_error(const char *filename, int line)
78 {
79   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
80 }
81
82 void gom_xref_already_in_use(const char *xrefstr)
83 {
84   gedcom_error(_("Cross-reference key '%s' is already in use"), xrefstr);
85 }
86
87 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found)
88 {
89   const char* found_name    = "<out-of-bounds>";
90   if (found < T_LAST)
91     found_name = ctxt_names[found];
92   gedcom_warning(_("Internal error: Unexpected context at %s, line %d: %s"),
93                  file, line, found_name);
94 }
95
96 void gom_no_context(const char* file, int line)
97 {
98   gedcom_warning(_("Internal error: No context at %s, line %d"),
99                  file, line);
100 }
101
102 void gom_move_error(const char* type)
103 {
104   gedcom_warning(_("Could not move struct of type %s"), type);
105 }
106
107 void gom_find_error(const char* type)
108 {
109   gedcom_warning(_("Could not find struct of type %s in chain"), type);
110 }
111
112 void def_rec_end(Gedcom_rec rec UNUSED, Gedcom_ctxt self,
113                  Gedcom_val parsed_value UNUSED)
114 {
115   Gom_ctxt ctxt = (Gom_ctxt)self;
116   destroy_gom_ctxt(ctxt);
117 }
118
119 /* TODO: do this in a way so that elements out of context can be handled */
120 void def_elt_end(Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
121                  Gedcom_ctxt self, Gedcom_val parsed_value UNUSED)
122 {
123   Gom_ctxt ctxt = (Gom_ctxt)self;
124   destroy_gom_ctxt(ctxt);
125 }