Added functions for retrieving, adding, modifying and deleting cross-references.
[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 cleanup_mess_buffer()
45 {
46   if (mess_buffer)
47     free(mess_buffer);
48 }
49
50 void init_mess_buffer()
51 {
52   if (mess_buffer == NULL) {
53     mess_buffer = (char *)malloc(INITIAL_BUF_SIZE);
54     if (mess_buffer) {
55       mess_buffer[0] = '\0';
56       bufsize = INITIAL_BUF_SIZE;
57       if (atexit(cleanup_mess_buffer) != 0)
58         gedcom_warning(_("Could not register buffer cleanup function"));
59     }
60     else {
61       fprintf(stderr, _("Could not allocate memory at %s, %d"),
62               __FILE__, __LINE__);
63       fprintf(stderr, "\n");
64     }
65   }
66 }
67
68 int safe_buf_vappend(const char *s, va_list ap)
69 {
70   int res = 0;
71   int len;
72   init_mess_buffer();
73   if (mess_buffer) {
74     len = strlen(mess_buffer);
75     while (1) {
76       char *buf_ptr = mess_buffer + len;
77       int rest_size = bufsize - len;
78       
79       res = vsnprintf(buf_ptr, rest_size, s, ap);
80       
81       if (res > -1 && res < rest_size) {
82         break;
83       }
84       else  {
85         bufsize *= 2;
86         mess_buffer = realloc(mess_buffer, bufsize);
87       }
88     }
89   }
90   return res;
91 }
92
93 int safe_buf_append(const char *s, ...)
94 {
95   int res;
96   va_list ap;
97   
98   va_start(ap, s);
99   res = safe_buf_vappend(s, ap);
100   va_end(ap);
101   
102   return res;
103 }
104
105 int gedcom_message(const char* s, ...)
106 {
107   int res;
108   va_list ap;
109
110   va_start(ap, s);
111   reset_mess_buffer();
112   res = safe_buf_vappend(s, ap);
113   va_end(ap);
114   if (msg_handler)
115     (*msg_handler)(MESSAGE, mess_buffer);
116   return res;
117 }
118
119 int gedcom_warning(const char* s, ...)
120 {
121   int res;
122   va_list ap;
123
124   reset_mess_buffer();
125   if (line_no != 0) 
126     safe_buf_append(_("Warning on line %d: "), line_no);
127   else
128     safe_buf_append(_("Warning: "));
129   va_start(ap, s);
130   res = safe_buf_vappend(s, ap);
131   va_end(ap);
132   if (msg_handler)
133     (*msg_handler)(WARNING, mess_buffer);
134   
135   return res;
136 }
137
138 int gedcom_error(const char* s, ...)
139 {
140   int res;
141   va_list ap;
142
143   reset_mess_buffer();
144   if (line_no != 0)
145     safe_buf_append(_("Error on line %d: "), line_no);
146   else
147     safe_buf_append(_("Error: "));
148   va_start(ap, s);
149   res = safe_buf_vappend(s, ap);
150   va_end(ap);
151   if (msg_handler)
152     (*msg_handler)(ERROR, mess_buffer);
153   
154   return res;
155 }
156
157 void gedcom_mem_error(const char *filename, int line)
158 {
159   gedcom_error(_("Could not allocate memory at %s, %d"), filename, line);
160 }