68a7c852e3f6dc3980f91920d8e4ff9ca084d659
[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     mess_buffer[0] = '\0';
49     bufsize = INITIAL_BUF_SIZE;
50   }
51 }
52
53 int safe_buf_vappend(char *s, va_list ap)
54 {
55   int res;
56   int len;
57   init_mess_buffer();
58   len = strlen(mess_buffer);
59   while (1) {
60     char *buf_ptr = mess_buffer + len;
61     int rest_size = bufsize - len;
62     
63     res = vsnprintf(buf_ptr, rest_size, s, ap);
64     
65     if (res > -1 && res < rest_size) {
66       break;
67     }
68     else  {
69       bufsize *= 2;
70       mess_buffer = realloc(mess_buffer, bufsize);
71     }
72   }
73   return res;  
74 }
75
76 int safe_buf_append(char *s, ...)
77 {
78   int res;
79   va_list ap;
80   
81   va_start(ap, s);
82   res = safe_buf_vappend(s, ap);
83   va_end(ap);
84   
85   return res;
86 }
87
88 int gedcom_message(char* s, ...)
89 {
90   int res;
91   va_list ap;
92
93   va_start(ap, s);
94   reset_mess_buffer();
95   res = safe_buf_vappend(s, ap);
96   va_end(ap);
97   if (msg_handler)
98     (*msg_handler)(MESSAGE, mess_buffer);
99   return res;
100 }
101
102 int gedcom_warning(char* s, ...)
103 {
104   int res;
105   va_list ap;
106
107   reset_mess_buffer();
108   safe_buf_append(_("Warning on line %d: "), line_no);
109   va_start(ap, s);
110   res = safe_buf_vappend(s, ap);
111   va_end(ap);
112   if (msg_handler)
113     (*msg_handler)(WARNING, mess_buffer);
114   
115   return res;
116 }
117
118 int gedcom_error(char* s, ...)
119 {
120   int res;
121   va_list ap;
122
123   reset_mess_buffer();
124   safe_buf_append(_("Error on line %d: "), line_no);
125   va_start(ap, s);
126   res = safe_buf_vappend(s, ap);
127   va_end(ap);
128   if (msg_handler)
129     (*msg_handler)(ERROR, mess_buffer);
130   
131   return res;
132 }