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