7 #define BUFFSIZE 2 * 1024
9 struct doc
*doc_alloc(int fd
)
11 struct doc
*doc
= xmalloc(sizeof(struct doc
));
15 doc
->buf
= xmalloc(BUFFSIZE
);
19 static void flush_buf(int fd
, char *buf
, int len
)
23 while (len
> n
&& (c
= write(fd
, buf
+ n
, len
- n
)) >= 0)
27 static void doc_flush(struct doc
*doc
)
29 flush_buf(doc
->fd
, doc
->buf
, doc
->len
);
33 void doc_memcat(struct doc
*doc
, char *s
, int n
)
35 if (n
> doc
->size
/ 2) {
37 flush_buf(doc
->fd
, s
, n
);
40 if (n
+ doc
->len
> doc
->size
)
42 memcpy(doc
->buf
+ doc
->len
, s
, n
);
46 void doc_write(struct doc
*doc
, char *s
)
48 doc_memcat(doc
, s
, strlen(s
));
51 void doc_free(struct doc
*doc
)