Mercurial > code
changeset 166:7f214f26a4c0
Added a macro for pack.c
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 24 Sep 2012 23:04:25 +0200 |
parents | 970e491d93cb |
children | 1167cd06b475 |
files | buf.c buf.h pack.c |
diffstat | 3 files changed, 51 insertions(+), 62 deletions(-) [+] |
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;
--- a/buf.h Mon Sep 24 23:03:50 2012 +0200 +++ b/buf.h Mon Sep 24 23:04:25 2012 +0200 @@ -21,14 +21,6 @@ #include <stdarg.h> -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef BUF_DEFAULT_BSIZE -#define BUF_DEFAULT_BSIZE 128 -#endif - #ifdef __GNUC__ # define _buf_at_printf(i1, i2) __attribute__ ((format (printf, i1, i2))) #else @@ -36,25 +28,28 @@ #endif enum buf_flags { - BUF_AUTO = 0, /* string grows automatically */ - BUF_FIXED = (1 << 0), /* fixed size string */ - BUF_UNSAFE = (1 << 1), /* string may be truncated */ + BUF_UNSAFE = (1 << 0), /* string may be truncated */ + BUF_FIXED = (1 << 1), /* string has fixed length */ }; struct buf { - enum buf_flags flags; /* (ro) string flags */ - char *text; /* (ro) string text */ - size_t length; /* (ro) string length */ - size_t alsize; /* (ro) allocated size */ - int bsize; /* (ro) block size (used when growing) */ + enum buf_flags flags; /* string flags */ + char *text; /* string text */ + size_t length; /* string length */ + size_t alsize; /* allocated size */ + int chksize; /* chunk size */ + int maxsize; /* max fixed length size */ - /* Own allocation functions */ - void * (*malloc)(size_t); - void * (*realloc)(void *, size_t); + void * (*malloc)(size_t); /* alternate malloc */ + void * (*realloc)(void *, size_t); /* alternate realloc */ }; +#ifdef __cplusplus +extern "C" { +#endif + int -buf_init(struct buf *); +buf_init(struct buf *, const char *); void buf_set(struct buf *, const char *, ...);
--- a/pack.c Mon Sep 24 23:03:50 2012 +0200 +++ b/pack.c Mon Sep 24 23:04:25 2012 +0200 @@ -62,6 +62,15 @@ char * dst; /* destination */ }; +#define SC_SET(s, tp, ed, f, b, al) do { \ + memset((s), 0, sizeof (struct pack_sc)); \ + (s)->type = tp; \ + (s)->endian = ed; \ + (s)->fp = f; \ + (s)->buf = b; \ + (s)->allocated = al; \ +} while (/* CONSTCOND */ 0) + #define TOKNARG(tok, nelem) do { \ if (p[1] == '[') { \ char *end; \ @@ -331,9 +340,9 @@ long pack_vfwrite(short ed, FILE *fp, const char *fmt, va_list ap) { - struct pack_sc sc = { - TYPE_FP, ed, fp, NULL, 0, 0, 0 - }; + struct pack_sc sc; + + SC_SET(&sc, TYPE_FP, ed, fp, NULL, 0); return packit(&sc, writeto, fmt, ap); } @@ -354,9 +363,9 @@ long pack_vswrite(short ed, char *buf, size_t max, const char *fmt, va_list ap) { - struct pack_sc sc = { - TYPE_FIXEDBUF, ed, NULL, buf, 0, max, 0 - }; + struct pack_sc sc; + + SC_SET(&sc, TYPE_FIXEDBUF, ed, NULL, buf, max); return packit(&sc, writeto, fmt, ap); } @@ -377,11 +386,10 @@ long pack_vaswrite(short ed, char **bufp, const char *fmt, va_list ap) { - struct pack_sc sc = { - TYPE_AUTOBUF, ed, NULL, NULL, 0, PACK_CHKSIZE, 0 - }; + struct pack_sc sc; + int rv; - int rv; + SC_SET(&sc, TYPE_AUTOBUF, ed, NULL, NULL, PACK_CHKSIZE); if ((sc.buf = malloc(PACK_CHKSIZE)) == NULL) return -1; @@ -436,9 +444,9 @@ long pack_vfread(short ed, FILE *fp, const char *fmt, va_list ap) { - struct pack_sc sc = { - TYPE_FP, ed, fp, NULL, 0, 0, 0 - }; + struct pack_sc sc; + + SC_SET(&sc, TYPE_FP, ed, fp, NULL, 0); return packit(&sc, readfrom, fmt, ap); } @@ -459,9 +467,9 @@ long pack_vsread(short ed, const char *buf, size_t max, const char *fmt, va_list ap) { - struct pack_sc sc = { - TYPE_FIXEDBUF, ed, NULL, (char *)buf, 0, max, 0 - }; + struct pack_sc sc; + + SC_SET(&sc, TYPE_FIXEDBUF, ed, NULL, (char *)buf, max); return packit(&sc, readfrom, fmt, ap); }