6 #define BUFFSIZE 2 * 1024
8 struct doc
*doc_alloc(int fd
)
10 struct doc
*doc
= xmalloc(sizeof(struct doc
));
14 doc
->buf
= xmalloc(BUFFSIZE
);
18 static void flush_buf(int fd
, char *buf
, int len
)
22 while (len
> n
&& (c
= write(fd
, buf
+ n
, len
- n
)) >= 0)
26 static void doc_flush(struct doc
*doc
)
28 flush_buf(doc
->fd
, doc
->buf
, doc
->len
);
32 void doc_memcat(struct doc
*doc
, char *s
, int n
)
34 if (n
> doc
->size
/ 2) {
36 flush_buf(doc
->fd
, s
, n
);
39 if (n
+ doc
->len
> doc
->size
)
41 memcpy(doc
->buf
+ doc
->len
, s
, n
);
45 void doc_write(struct doc
*doc
, char *s
)
47 doc_memcat(doc
, s
, strlen(s
));
50 void doc_free(struct doc
*doc
)