23d5c867a41abce99758334432e6997fabc77d30
[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
27 #define INITIAL_BUF_SIZE 256
28 char *mess_buffer = NULL;
29 size_t bufsize;
30
31 Gedcom_msg_handler msg_handler = NULL;
32
33 void gedcom_set_message_handler(Gedcom_msg_handler func)
34 {
35   msg_handler = func;
36 }
37
38 void reset_mess_buffer()
39 {
40   if (mess_buffer != NULL)
41     mess_buffer[0] = '\0';
42 }
43
44 void init_mess_buffer()
45 {
46   if (mess_buffer == NULL) {
47     mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
48     if (mess_buffer) {
49       mess_buffer[0] = '\0';
50       bufsize = INITIAL_BUF_SIZE;
51     }
52     else
53       fprintf(stderr, _("Could not allocate memory at %s, %d\n"),
54               __FILE__, __LINE__);
55   }
56 }
57
58 int safe_buf_vappend(char *s, va_list ap)
59 {
60   int res = 0;
61   int len;
62   init_mess_buffer();
63   if (mess_buffer) {
64     len = strlen(mess_buffer);
65     while (1) {
66       char *buf_ptr = mess_buffer + len;
67       int rest_size = bufsize - len;
68       
69       res = vsnprintf(buf_ptr, rest_size, s, ap);
70       
71       if (res > -1 && res < rest_size) {
72         break;
73       }
74       else  {
75         bufsize *= 2;
76         mess_buffer = realloc(mess_buffer, bufsize);
77       }
78     }
79   }
80   return res;
81 }
82
83 int safe_buf_append(char *s, ...)
84 {
85   int res;
86   va_list ap;
87   
88   va_start(ap, s);
89   res = safe_buf_vappend(s, ap);
90   va_end(ap);
91   
92   return res;
93 }
94
95 int gedcom_message(char* s, ...)
96 {
97   int res;
98   va_list ap;
99
100   va_start(ap, s);
101   reset_mess_buffer();
102   res = safe_buf_vappend(s, ap);
103   va_end(ap);
104   if (msg_handler)
105     (*msg_handler)(MESSAGE, mess_buffer);
106   return res;
107 }
108
109 int gedcom_warning(char* s, ...)
110 {
111   int res;
112   va_list ap;
113
114   reset_mess_buffer();
115   safe_buf_append(_("Warning on line %d: "), line_no);
116   va_start(ap, s);
117   res = safe_buf_vappend(s, ap);
118   va_end(ap);
119   if (msg_handler)
120     (*msg_handler)(WARNING, mess_buffer);
121   
122   return res;
123 }
124
125 int gedcom_error(char* s, ...)
126 {
127   int res;
128   va_list ap;
129
130   reset_mess_buffer();
131   safe_buf_append(_("Error on line %d: "), line_no);
132   va_start(ap, s);
133   res = safe_buf_vappend(s, ap);
134   va_end(ap);
135   if (msg_handler)
136     (*msg_handler)(ERROR, mess_buffer);
137   
138   return res;
139 }
140
141 void gedcom_mem_error(char *filename, int line)
142 {
143   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
144 }