Copied from old documentation. Removed all Gedcom_val details.
[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 void gedcom_set_default_callback(Gedcom_def_cb func)
34 {
35   if (default_cb) {
36     gedcom_error(_("Internal error: Duplicate registration for default callback"));
37   }
38   default_cb = func;
39 }
40
41 void gedcom_subscribe_to_record(Gedcom_rec rec,
42                                 Gedcom_rec_start_cb cb_start,
43                                 Gedcom_rec_end_cb cb_end)
44 {
45   if (record_start_callback[rec] || record_end_callback[rec])
46     gedcom_error(_("Internal error: Duplicate registration for record type %d"), rec);
47   if (cb_start) {
48     record_start_callback[rec] = cb_start;
49     record_end_callback[rec]   = cb_end;
50   }
51 }
52
53 void gedcom_subscribe_to_element(Gedcom_elt elt,
54                                  Gedcom_elt_start_cb cb_start,
55                                  Gedcom_elt_end_cb cb_end)
56 {
57   if (element_start_callback[elt] || element_end_callback[elt])
58     gedcom_error(_("Internal error: Duplicate registration for element type %d"), elt);
59   if (cb_start) {
60     element_start_callback[elt] = cb_start;
61     element_end_callback[elt]   = cb_end;
62   }
63 }
64
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)
68 {
69   Gedcom_rec_start_cb cb = record_start_callback[rec];
70   if (cb != NULL)
71     return (*cb)(rec, level, xref, tag.string, raw_value, tag.value,
72                  parsed_value);
73   else
74     return NULL;
75 }
76
77 void end_record(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
78 {
79   Gedcom_rec_end_cb cb = record_end_callback[rec];
80   if (cb != NULL)
81     (*cb)(rec, self, parsed_value);
82 }
83
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)
87 {
88   Gedcom_elt_start_cb cb = element_start_callback[elt];
89   Gedcom_ctxt ctxt = parent;
90   if (cb != NULL)
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);
95   return ctxt;
96 }
97
98 void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
99                  Gedcom_val parsed_value)
100 {
101   Gedcom_elt_end_cb cb = element_end_callback[elt];
102   if (cb != NULL)
103     (*cb)(elt, parent, self, parsed_value);
104 }
105
106 const char* val_type_str[] = { N_("null value"),
107                                N_("character string"),
108                                N_("date"),
109                                N_("age"),
110                                N_("cross-reference") };
111
112 void gedcom_cast_error(const char* file, int line,
113                        Gedcom_val_type tried_type,
114                        Gedcom_val_type real_type)
115 {
116   int tried_bit=0, real_bit=0;
117   while (tried_type && tried_type % 2 == 0) {
118     tried_bit++;
119     tried_type >>= 1;
120   }
121   while (real_type && real_type % 2 == 0) {
122     real_bit++;
123     real_type >>= 1;
124   }
125   gedcom_warning
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]));
128 }
129
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
133     possible mechanisms.
134  */
135
136 void gedcom_set_error_handling(Gedcom_err_mech mechanism)
137 {
138   error_mechanism = mechanism;
139 }