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 cleanup_mess_buffer()
50 void init_mess_buffer()
52 if (mess_buffer == NULL) {
53 mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
55 mess_buffer[0] = '\0';
56 bufsize = INITIAL_BUF_SIZE;
57 if (atexit(cleanup_mess_buffer) != 0)
58 gedcom_warning(_("Could not register buffer cleanup function"));
61 fprintf(stderr, _("Could not allocate memory at %s, %d"),
63 fprintf(stderr, "\n");
68 int safe_buf_vappend(const char *s, va_list ap)
74 len = strlen(mess_buffer);
76 char *buf_ptr = mess_buffer + len;
77 int rest_size = bufsize - len;
79 res = vsnprintf(buf_ptr, rest_size, s, ap);
81 if (res > -1 && res < rest_size) {
86 mess_buffer = realloc(mess_buffer, bufsize);
93 int safe_buf_append(const char *s, ...)
99 res = safe_buf_vappend(s, ap);
105 int gedcom_message(const char* s, ...)
112 res = safe_buf_vappend(s, ap);
115 (*msg_handler)(MESSAGE, mess_buffer);
119 int gedcom_warning(const char* s, ...)
126 safe_buf_append(_("Warning on line %d: "), line_no);
128 safe_buf_append(_("Warning: "));
130 res = safe_buf_vappend(s, ap);
133 (*msg_handler)(WARNING, mess_buffer);
138 int gedcom_error(const char* s, ...)
145 safe_buf_append(_("Error on line %d: "), line_no);
147 safe_buf_append(_("Error: "));
149 res = safe_buf_vappend(s, ap);
152 (*msg_handler)(ERROR, mess_buffer);
157 void gedcom_mem_error(const char *filename, int line)
159 gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);