Use Gedcom_val for the xref in the record start callback too.
[gedcom-parse.git] / gedcom / interface.c
1 /* Implementation of the interface to applications.
2    Copyright (C) 2001 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   default_cb = func;
36 }
37
38 void gedcom_subscribe_to_record(Gedcom_rec rec,
39                                 Gedcom_rec_start_cb cb_start,
40                                 Gedcom_rec_end_cb cb_end)
41 {
42   if (cb_start) {
43     record_start_callback[rec] = cb_start;
44     record_end_callback[rec]   = cb_end;
45   }
46 }
47
48 void gedcom_subscribe_to_element(Gedcom_elt elt,
49                                  Gedcom_elt_start_cb cb_start,
50                                  Gedcom_elt_end_cb cb_end)
51 {
52   if (cb_start) {
53     element_start_callback[elt] = cb_start;
54     element_end_callback[elt]   = cb_end;
55   }
56 }
57
58 Gedcom_ctxt start_record(Gedcom_rec rec,
59                          int level, Gedcom_val xref, char *tag)
60 {
61   Gedcom_rec_start_cb cb = record_start_callback[rec];
62   if (cb != NULL)
63     return (*cb)(level, xref, tag);
64   else
65     return NULL;
66 }
67
68 void end_record(Gedcom_rec rec, Gedcom_ctxt self)
69 {
70   Gedcom_rec_end_cb cb = record_end_callback[rec];
71   if (cb != NULL)
72     (*cb)(self);
73 }
74
75 Gedcom_ctxt start_element(Gedcom_elt elt, Gedcom_ctxt parent, 
76                           int level, char *tag, char *raw_value,
77                           Gedcom_val parsed_value)
78 {
79   Gedcom_elt_start_cb cb = element_start_callback[elt];
80   Gedcom_ctxt ctxt = parent;
81   if (cb != NULL)
82     ctxt = (*cb)(parent, level, tag, raw_value, parsed_value);
83   else if (default_cb != NULL)
84     (*default_cb)(parent, level, tag, raw_value);
85   return ctxt;
86 }
87
88 void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self,
89                  Gedcom_val parsed_value)
90 {
91   Gedcom_elt_end_cb cb = element_end_callback[elt];
92   if (cb != NULL)
93     (*cb)(parent, self, parsed_value);
94 }
95
96 char* val_type_str[] = { N_("null value"),
97                          N_("character string"),
98                          N_("date") };
99
100 void gedcom_cast_error(char* file, int line,
101                        Gedcom_val_type tried_type,
102                        Gedcom_val_type real_type)
103 {
104   gedcom_warning
105     (_("Wrong cast of value in file %s, at line %d: %s instead of %s"),
106      file, line, _(val_type_str[tried_type]), _(val_type_str[real_type]));
107 }