Mercurial > code
diff buf.c @ 166:7f214f26a4c0
Added a macro for pack.c
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 24 Sep 2012 23:04:25 +0200 |
parents | 44e7b975618a |
children |
line wrap: on
line diff
--- a/buf.c Mon Sep 24 23:03:50 2012 +0200 +++ b/buf.c Mon Sep 24 23:04:25 2012 +0200 @@ -27,34 +27,29 @@ #define BUF_FIXED(buf) ((buf)->flags & BUF_FIXED) #define BUF_AUTO(buf) (buf->flags == 0) -static void copy(struct buf *, const void *, size_t); -static int grow(struct buf *, size_t); +static void copy(struct buf *, const void *, size_t); +static int grow(struct buf *, size_t); int -buf_init(struct buf *buf) +buf_init(struct buf *buf, const char *txt) { /* Set defaults if needed */ - buf->bsize = (buf->bsize <= 0) ? BUF_DEFAULT_BSIZE : buf->bsize; - buf->alsize = buf->bsize + 1; + buf->chksize = (buf->chksize <= 0) ? 128 : buf->chksize; + buf->alsize = buf->chksize + 1; buf->malloc = (buf->malloc == NULL) ? &malloc : buf->malloc; buf->realloc = (buf->realloc == NULL) ? &realloc : buf->realloc; if ((buf->text = buf->malloc(buf->alsize)) == NULL) return -1; + if (txt != NULL) + buf_cat(buf, txt); + memset(buf->text, 0, buf->alsize); return 0; } -/* - * Valid options that can be set for a buffer : - * l -> optional buf block size of type int - * m -> malloc function that must matches void * (*malloc)(size_t) - * r -> realloc function that must matches void * (*realloc)(void *, size_t) - * f -> buffer flags that are OR'ed - */ - void buf_set(struct buf *buf, const char *fmt, ...) { @@ -65,7 +60,7 @@ for (p = fmt; *p != '\0'; ++p) switch (*p) { case 'l': - buf->bsize = va_arg(ap, int); + buf->chksize = va_arg(ap, int); break; case 'm': buf->malloc = va_arg(ap, void *(*)(size_t)); @@ -85,7 +80,6 @@ /* * This function appends not more than max characters from str. */ - int buf_ncat(struct buf *buf, const char *str, size_t max) { @@ -116,7 +110,6 @@ /* * Append the string str to the end of the string buffer. */ - int buf_cat(struct buf *buf, const char *str) { @@ -126,7 +119,6 @@ /* * Append the caracter c to the end of buffer */ - int buf_putc(struct buf *buf, int c) { @@ -140,7 +132,6 @@ * returns -1 on fixed safe buffer, otherwise 0 is returned if there * is no allocation failure. */ - int buf_vprintf(struct buf *buf, const char *fmt, va_list ap) { @@ -161,7 +152,7 @@ * vsnprintf returns -1 on windows, we need to grow * the buffer by block size. */ - if (grow(buf, buf->alsize + buf->bsize) < 0) { + if (grow(buf, buf->alsize + buf->chksize) < 0) { done = 1; rv = -1; } @@ -191,7 +182,6 @@ /* * Realloc the string to it's size and remove useless bytes. */ - int buf_trim(struct buf *buf) { @@ -209,7 +199,6 @@ * Remove `n' characters from the buffer, a positive value will cut the string * from beginning, negative value will cut from end. */ - void buf_cut(struct buf *buf, int start) { @@ -227,7 +216,6 @@ /* * Clear the string buffer. */ - void buf_clear(struct buf *buf) { @@ -251,7 +239,6 @@ /* * Append to the end of buffer the void ptr of count size. */ - static void copy(struct buf *buf, const void *ptr, size_t count) { @@ -265,17 +252,16 @@ * Grow the text buffer until the available size fit the needed * size. This function may return -1 on allocation failure. */ - static int grow(struct buf *buf, size_t needed) { while (BUF_AVAIL(buf) <= needed) { - buf->text = buf->realloc(buf->text, buf->alsize + buf->bsize); + buf->text = buf->realloc(buf->text, buf->alsize + buf->chksize); if (buf->text == NULL) return -1; - buf->alsize += buf->bsize; + buf->alsize += buf->chksize; } return 0;