1 /* Implementation of the interface to applications.
2 Copyright (C) 2001, 2002 The Genes Development Team
3 This file is part of the Gedcom parser library.
4 Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
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 #include "gedcom_internal.h"
25 #include "interface.h"
27 static Gedcom_rec_start_cb record_start_callback [NR_OF_RECS] = { NULL };
28 static Gedcom_rec_end_cb record_end_callback [NR_OF_RECS] = { NULL };
29 static Gedcom_elt_start_cb element_start_callback[NR_OF_ELTS] = { NULL };
30 static Gedcom_elt_end_cb element_end_callback [NR_OF_ELTS] = { NULL };
31 static Gedcom_def_cb default_cb = NULL;
33 void gedcom_set_default_callback(Gedcom_def_cb func)
36 gedcom_error(_("Internal error: Duplicate registration for default callback"));
41 void gedcom_subscribe_to_record(Gedcom_rec rec,
42 Gedcom_rec_start_cb cb_start,
43 Gedcom_rec_end_cb cb_end)
45 if (record_start_callback[rec] || record_end_callback[rec])
46 gedcom_error(_("Internal error: Duplicate registration for record type %d"), rec);
48 record_start_callback[rec] = cb_start;
49 record_end_callback[rec] = cb_end;
53 void gedcom_subscribe_to_element(Gedcom_elt elt,
54 Gedcom_elt_start_cb cb_start,
55 Gedcom_elt_end_cb cb_end)
57 if (element_start_callback[elt] || element_end_callback[elt])
58 gedcom_error(_("Internal error: Duplicate registration for element type %d"), elt);
60 element_start_callback[elt] = cb_start;
61 element_end_callback[elt] = cb_end;
65 Gedcom_ctxt start_record(Gedcom_rec rec,
66 int level, Gedcom_val xref, struct tag_struct tag,
67 char *raw_value, Gedcom_val parsed_value)
69 Gedcom_rec_start_cb cb = record_start_callback[rec];
71 return (*cb)(rec, level, xref, tag.string, raw_value, tag.value,
77 void end_record(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
79 Gedcom_rec_end_cb cb = record_end_callback[rec];
81 (*cb)(rec, self, parsed_value);
84 Gedcom_ctxt start_element(Gedcom_elt elt, Gedcom_ctxt parent,
85 int level, struct tag_struct tag, char *raw_value,
86 Gedcom_val parsed_value)
88 Gedcom_elt_start_cb cb = element_start_callback[elt];
89 Gedcom_ctxt ctxt = parent;
91 ctxt = (*cb)(elt, parent, level, tag.string, raw_value,
92 tag.value, parsed_value);
93 else if (default_cb != NULL && parent != NULL)
94 (*default_cb)(elt, parent, level, tag.string, raw_value, tag.value);
98 void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
99 Gedcom_val parsed_value)
101 Gedcom_elt_end_cb cb = element_end_callback[elt];
103 (*cb)(elt, parent, self, parsed_value);
106 const char* val_type_str[] = { N_("null value"),
107 N_("character string"),
110 N_("cross-reference") };
112 void gedcom_cast_error(const char* file, int line,
113 Gedcom_val_type tried_type,
114 Gedcom_val_type real_type)
116 int tried_bit=0, real_bit=0;
117 while (tried_type && tried_type % 2 == 0) {
121 while (real_type && real_type % 2 == 0) {
126 (_("Wrong cast of value in file %s, at line %d: %s instead of %s"),
127 file, line, _(val_type_str[tried_bit]), _(val_type_str[real_bit]));
130 /** This function allows to customize what happens on an error. It doesn't
131 influence the generation of error or warning messages, only the behaviour
132 of the parser and its return code. See \ref Gedcom_err_mech for the
136 void gedcom_set_error_handling(Gedcom_err_mech mechanism)
138 error_mechanism = mechanism;