Take care of const correctness.
[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"),
54               __FILE__, __LINE__);
55       fprintf(stderr, "\n");
56     }
57   }
58 }
59
60 int safe_buf_vappend(const char *s, va_list ap)
61 {
62   int res = 0;
63   int len;
64   init_mess_buffer();
65   if (mess_buffer) {
66     len = strlen(mess_buffer);
67     while (1) {
68       char *buf_ptr = mess_buffer + len;
69       int rest_size = bufsize - len;
70       
71       res = vsnprintf(buf_ptr, rest_size, s, ap);
72       
73       if (res > -1 && res < rest_size) {
74         break;
75       }
76       else  {
77         bufsize *= 2;
78         mess_buffer = realloc(mess_buffer, bufsize);
79       }
80     }
81   }
82   return res;
83 }
84
85 int safe_buf_append(const char *s, ...)
86 {
87   int res;
88   va_list ap;
89   
90   va_start(ap, s);
91   res = safe_buf_vappend(s, ap);
92   va_end(ap);
93   
94   return res;
95 }
96
97 int gedcom_message(const char* s, ...)
98 {
99   int res;
100   va_list ap;
101
102   va_start(ap, s);
103   reset_mess_buffer();
104   res = safe_buf_vappend(s, ap);
105   va_end(ap);
106   if (msg_handler)
107     (*msg_handler)(MESSAGE, mess_buffer);
108   return res;
109 }
110
111 int gedcom_warning(const char* s, ...)
112 {
113   int res;
114   va_list ap;
115
116   reset_mess_buffer();
117   safe_buf_append(_("Warning on line %d: "), line_no);
118   va_start(ap, s);
119   res = safe_buf_vappend(s, ap);
120   va_end(ap);
121   if (msg_handler)
122     (*msg_handler)(WARNING, mess_buffer);
123   
124   return res;
125 }
126
127 int gedcom_error(const char* s, ...)
128 {
129   int res;
130   va_list ap;
131
132   reset_mess_buffer();
133   safe_buf_append(_("Error on line %d: "), line_no);
134   va_start(ap, s);
135   res = safe_buf_vappend(s, ap);
136   va_end(ap);
137   if (msg_handler)
138     (*msg_handler)(ERROR, mess_buffer);
139   
140   return res;
141 }
142
143 void gedcom_mem_error(char *filename, int line)
144 {
145   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
146 }