renamed the package to libgedcom-dev
[gedcom-parse.git] / gedcom / interface.c
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.
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 "gedcom_internal.h"
25 #include "interface.h"
26
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;
32
33 /** This function allows to set the default callback.  You can only register
34     one default callback.
35     \param func The default callback.
36  */
37 void gedcom_set_default_callback(Gedcom_def_cb func)
38 {
39   if (default_cb) {
40     gedcom_error(_("Internal error: Duplicate registration for default callback"));
41   }
42   default_cb = func;
43 }
44
45 /** This function allows to subscribe to a record of a certain type, with a
46     start and an end
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
49     given record type.
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
54  */
55 void gedcom_subscribe_to_record(Gedcom_rec rec,
56                                 Gedcom_rec_start_cb cb_start,
57                                 Gedcom_rec_end_cb cb_end)
58 {
59   if (record_start_callback[rec] || record_end_callback[rec])
60     gedcom_error(_("Internal error: Duplicate registration for record type %d"), rec);
61   if (cb_start) {
62     record_start_callback[rec] = cb_start;
63     record_end_callback[rec]   = cb_end;
64   }
65 }
66
67 /** This function allows to subscribe to an element of a certain type, with a
68     start and an end
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
71     element type.
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
76  */
77 void gedcom_subscribe_to_element(Gedcom_elt elt,
78                                  Gedcom_elt_start_cb cb_start,
79                                  Gedcom_elt_end_cb cb_end)
80 {
81   if (element_start_callback[elt] || element_end_callback[elt])
82     gedcom_error(_("Internal error: Duplicate registration for element type %d"), elt);
83   if (cb_start) {
84     element_start_callback[elt] = cb_start;
85     element_end_callback[elt]   = cb_end;
86   }
87 }
88
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)
92 {
93   Gedcom_rec_start_cb cb = record_start_callback[rec];
94   if (cb != NULL)
95     return (*cb)(rec, level, xref, tag.string, raw_value, tag.value,
96                  parsed_value);
97   else
98     return NULL;
99 }
100
101 void end_record(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
102 {
103   Gedcom_rec_end_cb cb = record_end_callback[rec];
104   if (cb != NULL)
105     (*cb)(rec, self, parsed_value);
106 }
107
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)
111 {
112   Gedcom_elt_start_cb cb = element_start_callback[elt];
113   Gedcom_ctxt ctxt = parent;
114   if (cb != NULL)
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);
119   return ctxt;
120 }
121
122 void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
123                  Gedcom_val parsed_value)
124 {
125   Gedcom_elt_end_cb cb = element_end_callback[elt];
126   if (cb != NULL)
127     (*cb)(elt, parent, self, parsed_value);
128 }
129
130 const char* val_type_str[] = { N_("null value"),
131                                N_("character string"),
132                                N_("date"),
133                                N_("age"),
134                                N_("cross-reference") };
135
136 void gedcom_cast_error(const char* file, int line,
137                        Gedcom_val_type tried_type,
138                        Gedcom_val_type real_type)
139 {
140   int tried_bit=0, real_bit=0;
141   while (tried_type && tried_type % 2 == 0) {
142     tried_bit++;
143     tried_type >>= 1;
144   }
145   while (real_type && real_type % 2 == 0) {
146     real_bit++;
147     real_type >>= 1;
148   }
149   gedcom_warning
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]));
152 }
153
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.
159  */
160 void gedcom_set_error_handling(Gedcom_err_mech mechanism)
161 {
162   error_mechanism = mechanism;
163 }
164
165 /** This function allows to change the debug level.
166
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
171       
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.
174 */
175 void gedcom_set_debug_level(int level, FILE* f)
176 {
177   if (f != NULL)
178     trace_output = f;
179   else
180     trace_output = stderr;
181   if (level > 0) {
182     gedcom_high_level_debug = 1;
183   }
184   if (level > 1) {
185     gedcom_enable_internal_debug();
186   }
187 }
188