-
Notifications
You must be signed in to change notification settings - Fork 3
/
reporting.c
159 lines (136 loc) · 4.35 KB
/
reporting.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* $Id$ */
/*
*
* Copyright (C) 2001-2004 EPFL (Swiss Federal Institute of Technology,
* Lausanne) This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* In addition, as a special exception, EPFL gives permission to link
* the code of this program with the Qt non-commercial edition library
* (or with modified versions of Qt non-commercial edition that use the
* same license as Qt non-commercial edition), and distribute linked
* combinations including the two. You must obey the GNU General
* Public License in all respects for all of the code used other than
* Qt non-commercial edition. If you modify this file, you may extend
* this exception to your version of the file, but you are not
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Authors : Nicolas Aspert, Diego Santa-Cruz and Davy Jacquet
*
* Web site : http://mesh.epfl.ch
*
* Reference :
* "MESH : Measuring Errors between Surfaces using the Hausdorff distance"
* in Proceedings of IEEE Intl. Conf. on Multimedia and Expo (ICME) 2002,
* vol. I, pp. 705-708, available on http://mesh.epfl.ch
*
*/
#include <reporting.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <xalloc.h>
/* Minimum amount of free space in buffer */
#define OUTBUF_MIN_FREE (2*OUTBUF_MAX_SZ)
/* --------------------------------------------------------------------------*
* External functions *
* --------------------------------------------------------------------------*/
/* see reporting.h */
struct outbuf *outbuf_new(outbuf_flush_cb_t *flush_cb, void *cb_out)
{
struct outbuf *ob;
ob = xa_malloc(sizeof(*ob));
ob->strbuf = xa_malloc(OUTBUF_MIN_FREE);
ob->strbuf[0] = '\0';
ob->pos = ob->strbuf;
ob->end = ob->strbuf + OUTBUF_MIN_FREE;
ob->flush = flush_cb;
ob->cb_out = cb_out;
return ob;
}
/* see reporting.h */
void outbuf_delete(struct outbuf *ob)
{
if (ob != NULL) {
free(ob->strbuf);
free(ob);
}
}
/* see reporting.h */
void outbuf_flush(struct outbuf *ob)
{
assert(*(ob->pos) == '\0');
if (ob->flush != NULL) {
ob->flush(ob->cb_out,ob->strbuf);
ob->pos = ob->strbuf;
if (ob->end > ob->pos) *(ob->pos) = '\0';
}
}
/* see reporting.h */
void outbuf_printf(struct outbuf *ob, const char *format, ...)
{
va_list ap;
int len;
int free_sz;
size_t new_sz,off;
assert(ob->strbuf != NULL && ob->pos != NULL && ob->end != NULL);
/* Ensure a minimum amount of free bytes */
free_sz = ob->end - ob->pos;
if (free_sz < OUTBUF_MAX_SZ) {
off = ob->pos-ob->strbuf;
new_sz = ob->end-ob->strbuf+OUTBUF_MIN_FREE;
ob->strbuf = xa_realloc(ob->strbuf,new_sz);
ob->pos = ob->strbuf+off;
ob->end = ob->strbuf+new_sz;
free_sz = ob->end - ob->pos;
}
/* Print to string buffer */
va_start(ap,format);
len = vsprintf(ob->pos,format,ap);
va_end(ap);
/* Check that buffer was large enough. Ensuring that we have enough free
* space requires C99 or some ugly black magic. This should be enough for us
* anyhow. */
if (len >= free_sz) {
fprintf(stderr,"PANIC: String buffer size exceeded. "
"Memory possibly corrupted.");
}
ob->pos += len;
}
/* see reporting.h */
void prog_report(struct prog_reporter *pr, int p)
{
pr->prog(pr->cb_out,p);
}
/* see reporting.h */
void stdio_puts(void *out, const char *str)
{
fputs(str,(FILE*)out);
fflush((FILE*)out);
}
/* see reporting.h */
void stdio_prog(void *out, int p)
{
FILE *fout;
fout = (FILE*)out;
if(p < 0) {
fprintf(fout,"\r \r"); /* Remove progress message */
} else {
fprintf(fout,"\rProgress %3d %%",p);
}
fflush(fout);
}