Renamed logf function to yaz_log. Removed VC++ project files.
[yaz-moved-to-github.git] / util / wrbuf.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: wrbuf.c,v $
7  * Revision 1.5  1999-08-27 09:40:32  adam
8  * Renamed logf function to yaz_log. Removed VC++ project files.
9  *
10  * Revision 1.4  1998/02/11 11:53:36  adam
11  * Changed code so that it compiles as C++.
12  *
13  * Revision 1.3  1997/05/01 15:08:15  adam
14  * Added log_mask_str_x routine.
15  *
16  * Revision 1.2  1995/11/01 13:55:06  quinn
17  * Minor adjustments
18  *
19  * Revision 1.1  1995/10/06  08:51:25  quinn
20  * Added Write-buffer.
21  *
22  *
23  */
24
25 /*
26  * Growing buffer for writing various stuff.
27  */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <wrbuf.h>
33
34 WRBUF wrbuf_alloc(void)
35 {
36     WRBUF n;
37
38     if (!(n = (WRBUF)xmalloc(sizeof(*n))))
39         abort();
40     n->buf = 0;
41     n->size = 0;
42     n->pos = 0;
43     return n;
44 }
45
46 void wrbuf_free(WRBUF b, int free_buf)
47 {
48     if (free_buf && b->buf)
49         xfree(b->buf);
50     xfree(b);
51 }
52
53 void wrbuf_rewind(WRBUF b)
54 {
55     b->pos = 0;
56 }
57
58 int wrbuf_grow(WRBUF b, int minsize)
59 {
60     int togrow;
61
62     if (!b->size)
63         togrow = 1024;
64     else
65         togrow = b->size;
66     if (togrow < minsize)
67         togrow = minsize;
68     if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow)))
69         abort();
70     else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow)))
71         abort();
72     return 0;
73 }
74
75 int wrbuf_write(WRBUF b, const char *buf, int size)
76 {
77     if (b->pos + size >= b->size)
78         wrbuf_grow(b, size);
79     memcpy(b->buf + b->pos, buf, size);
80     b->pos += size;
81     return 0;
82 }