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.
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.
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.
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
24 #include "gedcom_internal.h"
28 #define INITIAL_BUF_SIZE 256
30 /* Risk on overflowing buffer, so make size big */
31 #define INITIAL_BUF_SIZE 65536
34 char *mess_buffer = NULL;
37 Gedcom_msg_handler msg_handler = NULL;
39 void gedcom_set_message_handler(Gedcom_msg_handler func)
44 void reset_mess_buffer()
46 if (mess_buffer != NULL)
47 mess_buffer[0] = '\0';
50 void cleanup_mess_buffer()
56 void init_mess_buffer()
58 if (mess_buffer == NULL) {
59 mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
61 mess_buffer[0] = '\0';
62 bufsize = INITIAL_BUF_SIZE;
63 if (atexit(cleanup_mess_buffer) != 0)
64 gedcom_warning(_("Could not register buffer cleanup function"));
67 fprintf(stderr, _("Could not allocate memory at %s, %d"),
69 fprintf(stderr, "\n");
74 int safe_buf_vappend(const char *s, va_list ap)
80 len = strlen(mess_buffer);
82 char *buf_ptr = mess_buffer + len;
83 int rest_size = bufsize - len;
86 res = vsnprintf(buf_ptr, rest_size, s, ap);
88 if (res > -1 && res < rest_size) {
93 mess_buffer = realloc(mess_buffer, bufsize);
95 #else /* not HAVE_VSNPRINTF */
97 # warning "Using VSPRINTF. Buffer overflow could happen!"
98 vsprintf(buf_ptr, s, ap);
100 # else /* not HAVE_VPRINTF */
101 # error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!"
109 int safe_buf_append(const char *s, ...)
115 res = safe_buf_vappend(s, ap);
121 int gedcom_message(const char* s, ...)
128 res = safe_buf_vappend(s, ap);
131 (*msg_handler)(MESSAGE, mess_buffer);
135 int gedcom_warning(const char* s, ...)
142 safe_buf_append(_("Warning on line %d: "), line_no);
144 safe_buf_append(_("Warning: "));
146 res = safe_buf_vappend(s, ap);
149 (*msg_handler)(WARNING, mess_buffer);
154 int gedcom_error(const char* s, ...)
161 safe_buf_append(_("Error on line %d: "), line_no);
163 safe_buf_append(_("Error: "));
165 res = safe_buf_vappend(s, ap);
168 (*msg_handler)(ERROR, mess_buffer);
173 void gedcom_mem_error(const char *filename, int line)
175 gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);