Added gettext calls.
[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   safe_buf_append("\n");
98   if (msg_handler)
99     (*msg_handler)(MESSAGE, mess_buffer);
100   return res;
101 }
102
103 int gedcom_warning(char* s, ...)
104 {
105   int res;
106   va_list ap;
107
108   reset_mess_buffer();
109   safe_buf_append(_("Warning on line %d: "), line_no);
110   va_start(ap, s);
111   res = safe_buf_vappend(s, ap);
112   va_end(ap);
113   safe_buf_append("\n");
114   if (msg_handler)
115     (*msg_handler)(WARNING, mess_buffer);
116   
117   return res;
118 }
119
120 int gedcom_error(char* s, ...)
121 {
122   int res;
123   va_list ap;
124
125   reset_mess_buffer();
126   safe_buf_append(_("Error on line %d: "), line_no);
127   va_start(ap, s);
128   res = safe_buf_vappend(s, ap);
129   va_end(ap);
130   safe_buf_append("\n");
131   if (msg_handler)
132     (*msg_handler)(ERROR, mess_buffer);
133   
134   return res;
135 }