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 /** This function allows to set the default callback. You can only register
35 \param func The default callback.
37 void gedcom_set_default_callback(Gedcom_def_cb func)
40 gedcom_error(_("Internal error: Duplicate registration for default callback"));
45 /** This function allows to subscribe to a record of a certain type, with a
47 callback. The end callback is optional: you can pass \c NULL if you are
48 not interested in the end callback. You can only register once for a
50 \param rec The record to subscribe to (see the
51 <a href="interface.html#Record_identifiers">interface details</a>)
52 \param cb_start The start callback
53 \param cb_end The end callback
55 void gedcom_subscribe_to_record(Gedcom_rec rec,
56 Gedcom_rec_start_cb cb_start,
57 Gedcom_rec_end_cb cb_end)
59 if (record_start_callback[rec] || record_end_callback[rec])
60 gedcom_error(_("Internal error: Duplicate registration for record type %d"), rec);
62 record_start_callback[rec] = cb_start;
63 record_end_callback[rec] = cb_end;
67 /** This function allows to subscribe to an element of a certain type, with a
69 callback. The end callback is optional: you can pass \c NULL if you are
70 not interested in the end callback. You can only register once for a given
72 \param elt The element to subscribe to (see the
73 <a href="interface.html#Element_identifiers">interface details</a>)
74 \param cb_start The start callback
75 \param cb_end The end callback
77 void gedcom_subscribe_to_element(Gedcom_elt elt,
78 Gedcom_elt_start_cb cb_start,
79 Gedcom_elt_end_cb cb_end)
81 if (element_start_callback[elt] || element_end_callback[elt])
82 gedcom_error(_("Internal error: Duplicate registration for element type %d"), elt);
84 element_start_callback[elt] = cb_start;
85 element_end_callback[elt] = cb_end;
89 Gedcom_ctxt start_record(Gedcom_rec rec,
90 int level, Gedcom_val xref, struct tag_struct tag,
91 char *raw_value, Gedcom_val parsed_value)
93 Gedcom_rec_start_cb cb = record_start_callback[rec];
95 return (*cb)(rec, level, xref, tag.string, raw_value, tag.value,
101 void end_record(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
103 Gedcom_rec_end_cb cb = record_end_callback[rec];
105 (*cb)(rec, self, parsed_value);
108 Gedcom_ctxt start_element(Gedcom_elt elt, Gedcom_ctxt parent,
109 int level, struct tag_struct tag, char *raw_value,
110 Gedcom_val parsed_value)
112 Gedcom_elt_start_cb cb = element_start_callback[elt];
113 Gedcom_ctxt ctxt = parent;
115 ctxt = (*cb)(elt, parent, level, tag.string, raw_value,
116 tag.value, parsed_value);
117 else if (default_cb != NULL && parent != NULL)
118 (*default_cb)(elt, parent, level, tag.string, raw_value, tag.value);
122 void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
123 Gedcom_val parsed_value)
125 Gedcom_elt_end_cb cb = element_end_callback[elt];
127 (*cb)(elt, parent, self, parsed_value);
130 const char* val_type_str[] = { N_("null value"),
131 N_("character string"),
134 N_("cross-reference") };
136 void gedcom_cast_error(const char* file, int line,
137 Gedcom_val_type tried_type,
138 Gedcom_val_type real_type)
140 int tried_bit=0, real_bit=0;
141 while (tried_type && tried_type % 2 == 0) {
145 while (real_type && real_type % 2 == 0) {
150 (_("Wrong cast of value in file %s, at line %d: %s instead of %s"),
151 file, line, _(val_type_str[tried_bit]), _(val_type_str[real_bit]));
154 /** This function allows to customize what happens on an error. It doesn't
155 influence the generation of error or warning messages, only the behaviour
156 of the parser and its return code.
157 \param mechanism The mechanism to be used; see \ref Gedcom_err_mech
158 for the possible mechanisms.
160 void gedcom_set_error_handling(Gedcom_err_mech mechanism)
162 error_mechanism = mechanism;
165 /** This function allows to change the debug level.
167 \param level The debug level, one of the following values:
168 - 0: no debugging information (this is the default)
169 - 1: only debugging information from libgedcom itself
170 - 2: debugging information from libgedcom and yacc
172 \param f A file handle (which must be open) to write debugging information
173 to; if \c NULL is passed, \c stderr will be used.
175 void gedcom_set_debug_level(int level, FILE* f)
180 trace_output = stderr;
182 gedcom_high_level_debug = 1;
185 gedcom_enable_internal_debug();