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"
27 #define INITIAL_BUF_SIZE 256
28 char *mess_buffer = NULL;
31 Gedcom_msg_handler msg_handler = NULL;
33 void gedcom_set_message_handler(Gedcom_msg_handler func)
38 void reset_mess_buffer()
40 if (mess_buffer != NULL)
41 mess_buffer[0] = '\0';
44 void init_mess_buffer()
46 if (mess_buffer == NULL) {
47 mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
49 mess_buffer[0] = '\0';
50 bufsize = INITIAL_BUF_SIZE;
53 fprintf(stderr, _("Could not allocate memory at %s, %d\n"),
58 int safe_buf_vappend(char *s, va_list ap)
64 len = strlen(mess_buffer);
66 char *buf_ptr = mess_buffer + len;
67 int rest_size = bufsize - len;
69 res = vsnprintf(buf_ptr, rest_size, s, ap);
71 if (res > -1 && res < rest_size) {
76 mess_buffer = realloc(mess_buffer, bufsize);
83 int safe_buf_append(char *s, ...)
89 res = safe_buf_vappend(s, ap);
95 int gedcom_message(char* s, ...)
102 res = safe_buf_vappend(s, ap);
105 (*msg_handler)(MESSAGE, mess_buffer);
109 int gedcom_warning(char* s, ...)
115 safe_buf_append(_("Warning on line %d: "), line_no);
117 res = safe_buf_vappend(s, ap);
120 (*msg_handler)(WARNING, mess_buffer);
125 int gedcom_error(char* s, ...)
131 safe_buf_append(_("Error on line %d: "), line_no);
133 res = safe_buf_vappend(s, ap);
136 (*msg_handler)(ERROR, mess_buffer);
141 void gedcom_mem_error(char *filename, int line)
143 gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);