Portability: have fallback solution if vsnprintf is not available.
[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 #if HAVE_VSNPRINTF
28 #define INITIAL_BUF_SIZE 256
29 #else
30 /* Risk on overflowing buffer, so make size big */
31 #define INITIAL_BUF_SIZE 65536
32 #endif
33
34 char *mess_buffer = NULL;
35 size_t bufsize;
36
37 Gedcom_msg_handler msg_handler = NULL;
38
39 void gedcom_set_message_handler(Gedcom_msg_handler func)
40 {
41   msg_handler = func;
42 }
43
44 void reset_mess_buffer()
45 {
46   if (mess_buffer != NULL)
47     mess_buffer[0] = '\0';
48 }
49
50 void cleanup_mess_buffer()
51 {
52   if (mess_buffer)
53     free(mess_buffer);
54 }
55
56 void init_mess_buffer()
57 {
58   if (mess_buffer == NULL) {
59     mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
60     if (mess_buffer) {
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"));
65     }
66     else {
67       fprintf(stderr, _("Could not allocate memory at %s, %d"),
68               __FILE__, __LINE__);
69       fprintf(stderr, "\n");
70     }
71   }
72 }
73
74 int safe_buf_vappend(const char *s, va_list ap)
75 {
76   int res = 0;
77   int len;
78   init_mess_buffer();
79   if (mess_buffer) {
80     len = strlen(mess_buffer);
81     while (1) {
82       char *buf_ptr = mess_buffer + len;
83       int rest_size = bufsize - len;
84       
85 #if HAVE_VSNPRINTF
86       res = vsnprintf(buf_ptr, rest_size, s, ap);
87       
88       if (res > -1 && res < rest_size) {
89         break;
90       }
91       else  {
92         bufsize *= 2;
93         mess_buffer = realloc(mess_buffer, bufsize);
94       }
95 #else /* not HAVE_VSNPRINTF */
96 #  if HAVE_VSPRINTF
97 #     warning "Using VSPRINTF. Buffer overflow could happen!"
98       vsprintf(buf_ptr, s, ap);
99       break;
100 #  else /* not HAVE_VPRINTF */
101 #     error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!"
102 #  endif
103 #endif
104     }
105   }
106   return res;
107 }
108
109 int safe_buf_append(const char *s, ...)
110 {
111   int res;
112   va_list ap;
113   
114   va_start(ap, s);
115   res = safe_buf_vappend(s, ap);
116   va_end(ap);
117   
118   return res;
119 }
120
121 int gedcom_message(const char* s, ...)
122 {
123   int res;
124   va_list ap;
125
126   va_start(ap, s);
127   reset_mess_buffer();
128   res = safe_buf_vappend(s, ap);
129   va_end(ap);
130   if (msg_handler)
131     (*msg_handler)(MESSAGE, mess_buffer);
132   return res;
133 }
134
135 int gedcom_warning(const char* s, ...)
136 {
137   int res;
138   va_list ap;
139
140   reset_mess_buffer();
141   if (line_no != 0) 
142     safe_buf_append(_("Warning on line %d: "), line_no);
143   else
144     safe_buf_append(_("Warning: "));
145   va_start(ap, s);
146   res = safe_buf_vappend(s, ap);
147   va_end(ap);
148   if (msg_handler)
149     (*msg_handler)(WARNING, mess_buffer);
150   
151   return res;
152 }
153
154 int gedcom_error(const char* s, ...)
155 {
156   int res;
157   va_list ap;
158
159   reset_mess_buffer();
160   if (line_no != 0)
161     safe_buf_append(_("Error on line %d: "), line_no);
162   else
163     safe_buf_append(_("Error: "));
164   va_start(ap, s);
165   res = safe_buf_vappend(s, ap);
166   va_end(ap);
167   if (msg_handler)
168     (*msg_handler)(ERROR, mess_buffer);
169   
170   return res;
171 }
172
173 void gedcom_mem_error(const char *filename, int line)
174 {
175   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
176 }