renamed the package to libgedcom-dev
[gedcom-parse.git] / gedcom / message.c
1 /* Implementation of the messaging API 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 "gedcom.h"
26 #include "buffer.h"
27
28 void cleanup_mess_buffer();
29
30 struct safe_buffer mess_buffer = { NULL, 0, NULL, 0, cleanup_mess_buffer };
31 Gedcom_msg_handler msg_handler = NULL;
32
33 /** This function registers a callback that is called if there are errors,
34     warnings or just messages coming from the parser.
35
36     \param func The callback to be called on errors, warnings or messages; see
37     \ref Gedcom_msg_handler for the signature of the callback.
38 */
39 void gedcom_set_message_handler(Gedcom_msg_handler func)
40 {
41   msg_handler = func;
42 }
43
44 void cleanup_mess_buffer()
45 {
46   cleanup_buffer(&mess_buffer);
47 }
48
49 int gedcom_message(const char* s, ...)
50 {
51   int res;
52   va_list ap;
53
54   va_start(ap, s);
55   reset_buffer(&mess_buffer);
56   res = safe_buf_vappend(&mess_buffer, s, ap);
57   va_end(ap);
58   if (msg_handler)
59     (*msg_handler)(MESSAGE, get_buf_string(&mess_buffer));
60   return res;
61 }
62
63 int gedcom_warning(const char* s, ...)
64 {
65   int res;
66   va_list ap;
67
68   reset_buffer(&mess_buffer);
69   if (line_no != 0) 
70     safe_buf_append(&mess_buffer, _("Warning on line %d: "), line_no);
71   else
72     safe_buf_append(&mess_buffer, _("Warning: "));
73   va_start(ap, s);
74   res = safe_buf_vappend(&mess_buffer, s, ap);
75   va_end(ap);
76   if (msg_handler)
77     (*msg_handler)(WARNING, get_buf_string(&mess_buffer));
78   
79   return res;
80 }
81
82 int gedcom_error(const char* s, ...)
83 {
84   int res;
85   va_list ap;
86
87   reset_buffer(&mess_buffer);
88   if (line_no != 0)
89     safe_buf_append(&mess_buffer, _("Error on line %d: "), line_no);
90   else
91     safe_buf_append(&mess_buffer, _("Error: "));
92   va_start(ap, s);
93   res = safe_buf_vappend(&mess_buffer, s, ap);
94   va_end(ap);
95   if (msg_handler)
96     (*msg_handler)(ERROR, get_buf_string(&mess_buffer));
97   
98   return res;
99 }
100
101 void gedcom_mem_error(const char *filename, int line)
102 {
103   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
104 }