X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gedcom%2Finterface.c;h=7d155576744fda077ea177ae97d7fd876833158b;hb=refs%2Fheads%2Flibiconv-gedcom-1.14;hp=265c28f9dbf5886dd012ef8cf6423b2bbd4ed4e6;hpb=40ff55f97de314e09359361d83f7eacd47fa3bce;p=gedcom-parse.git diff --git a/gedcom/interface.c b/gedcom/interface.c index 265c28f..7d15557 100644 --- a/gedcom/interface.c +++ b/gedcom/interface.c @@ -1,5 +1,5 @@ /* Implementation of the interface to applications. - Copyright (C) 2001 The Genes Development Team + Copyright (C) 2001, 2002 The Genes Development Team This file is part of the Gedcom parser library. Contributed by Peter Verthez , 2001. @@ -24,60 +24,98 @@ #include "gedcom_internal.h" #include "interface.h" -static Gedcom_rec_start_cb record_start_callback [LAST_REC] = { NULL }; -static Gedcom_rec_end_cb record_end_callback [LAST_REC] = { NULL }; -static Gedcom_elt_start_cb element_start_callback[LAST_ELT] = { NULL }; -static Gedcom_elt_end_cb element_end_callback [LAST_ELT] = { NULL }; -static Gedcom_def_cb default_cb = NULL; +static Gedcom_rec_start_cb record_start_callback [NR_OF_RECS] = { NULL }; +static Gedcom_rec_end_cb record_end_callback [NR_OF_RECS] = { NULL }; +static Gedcom_elt_start_cb element_start_callback[NR_OF_ELTS] = { NULL }; +static Gedcom_elt_end_cb element_end_callback [NR_OF_ELTS] = { NULL }; +static Gedcom_def_cb default_cb = NULL; +/** This function allows to set the default callback. You can only register + one default callback. + \param func The default callback. + */ void gedcom_set_default_callback(Gedcom_def_cb func) { + if (default_cb) { + gedcom_error(_("Internal error: Duplicate registration for default callback")); + } default_cb = func; } +/** This function allows to subscribe to a record of a certain type, with a + start and an end + callback. The end callback is optional: you can pass \c NULL if you are + not interested in the end callback. You can only register once for a + given record type. + \param rec The record to subscribe to (see the + interface details) + \param cb_start The start callback + \param cb_end The end callback + */ void gedcom_subscribe_to_record(Gedcom_rec rec, Gedcom_rec_start_cb cb_start, Gedcom_rec_end_cb cb_end) { - record_start_callback[rec] = cb_start; - record_end_callback[rec] = cb_end; + if (record_start_callback[rec] || record_end_callback[rec]) + gedcom_error(_("Internal error: Duplicate registration for record type %d"), rec); + if (cb_start) { + record_start_callback[rec] = cb_start; + record_end_callback[rec] = cb_end; + } } +/** This function allows to subscribe to an element of a certain type, with a + start and an end + callback. The end callback is optional: you can pass \c NULL if you are + not interested in the end callback. You can only register once for a given + element type. + \param elt The element to subscribe to (see the + interface details) + \param cb_start The start callback + \param cb_end The end callback + */ void gedcom_subscribe_to_element(Gedcom_elt elt, Gedcom_elt_start_cb cb_start, Gedcom_elt_end_cb cb_end) { - element_start_callback[elt] = cb_start; - element_end_callback[elt] = cb_end; + if (element_start_callback[elt] || element_end_callback[elt]) + gedcom_error(_("Internal error: Duplicate registration for element type %d"), elt); + if (cb_start) { + element_start_callback[elt] = cb_start; + element_end_callback[elt] = cb_end; + } } Gedcom_ctxt start_record(Gedcom_rec rec, - int level, char *xref, char *tag) + int level, Gedcom_val xref, struct tag_struct tag, + char *raw_value, Gedcom_val parsed_value) { Gedcom_rec_start_cb cb = record_start_callback[rec]; if (cb != NULL) - return (*cb)(level, xref, tag); + return (*cb)(rec, level, xref, tag.string, raw_value, tag.value, + parsed_value); else return NULL; } -void end_record(Gedcom_rec rec, Gedcom_ctxt self) +void end_record(Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value) { Gedcom_rec_end_cb cb = record_end_callback[rec]; if (cb != NULL) - (*cb)(self); + (*cb)(rec, self, parsed_value); } Gedcom_ctxt start_element(Gedcom_elt elt, Gedcom_ctxt parent, - int level, char *tag, char *raw_value, + int level, struct tag_struct tag, char *raw_value, Gedcom_val parsed_value) { Gedcom_elt_start_cb cb = element_start_callback[elt]; Gedcom_ctxt ctxt = parent; if (cb != NULL) - ctxt = (*cb)(parent, level, tag, raw_value, parsed_value); - else if (default_cb != NULL) - (*default_cb)(parent, level, tag, raw_value); + ctxt = (*cb)(elt, parent, level, tag.string, raw_value, + tag.value, parsed_value); + else if (default_cb != NULL && parent != NULL) + (*default_cb)(elt, parent, level, tag.string, raw_value, tag.value); return ctxt; } @@ -86,6 +124,65 @@ void end_element(Gedcom_elt elt, Gedcom_ctxt parent, Gedcom_ctxt self, { Gedcom_elt_end_cb cb = element_end_callback[elt]; if (cb != NULL) - (*cb)(parent, self, parsed_value); + (*cb)(elt, parent, self, parsed_value); +} + +const char* val_type_str[] = { N_("null value"), + N_("character string"), + N_("date"), + N_("age"), + N_("cross-reference") }; + +void gedcom_cast_error(const char* file, int line, + Gedcom_val_type tried_type, + Gedcom_val_type real_type) +{ + int tried_bit=0, real_bit=0; + while (tried_type && tried_type % 2 == 0) { + tried_bit++; + tried_type >>= 1; + } + while (real_type && real_type % 2 == 0) { + real_bit++; + real_type >>= 1; + } + gedcom_warning + (_("Wrong cast of value in file %s, at line %d: %s instead of %s"), + file, line, _(val_type_str[tried_bit]), _(val_type_str[real_bit])); +} + +/** This function allows to customize what happens on an error. It doesn't + influence the generation of error or warning messages, only the behaviour + of the parser and its return code. + \param mechanism The mechanism to be used; see \ref Gedcom_err_mech + for the possible mechanisms. + */ +void gedcom_set_error_handling(Gedcom_err_mech mechanism) +{ + error_mechanism = mechanism; +} + +/** This function allows to change the debug level. + + \param level The debug level, one of the following values: + - 0: no debugging information (this is the default) + - 1: only debugging information from libgedcom itself + - 2: debugging information from libgedcom and yacc + + \param f A file handle (which must be open) to write debugging information + to; if \c NULL is passed, \c stderr will be used. +*/ +void gedcom_set_debug_level(int level, FILE* f) +{ + if (f != NULL) + trace_output = f; + else + trace_output = stderr; + if (level > 0) { + gedcom_high_level_debug = 1; + } + if (level > 1) { + gedcom_enable_internal_debug(); + } }