X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Fbuffer.h;h=b73e506bdd7f3ac9930f9c9688dba83f88211446;hp=50e39041ade79a0fdb03c837fb32ac095df4ce0c;hb=6d838d9aef36d95fa439b3f7cc06d4e81c8581bd;hpb=c8bf978fc5c4f6c793620a515fa2456a3fa9eb13 diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index 50e3904..b73e506 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -29,22 +29,93 @@ #ifndef MUTT_LIB_LIB_BUFFER_H #define MUTT_LIB_LIB_BUFFER_H -#include +typedef struct buffer_t { + char *data; + ssize_t len; + ssize_t size; +} buffer_t; -/* flags for mutt_extract_token() */ -#define M_TOKEN_EQUAL 1 /* treat '=' as a special */ -#define M_TOKEN_CONDENSE (1<<1) /* ^(char) to control chars (macros) */ -#define M_TOKEN_SPACE (1<<2) /* don't treat whitespace as a term */ -#define M_TOKEN_QUOTE (1<<3) /* don't interpret quotes */ -#define M_TOKEN_PATTERN (1<<4) /* !)|~ are terms (for patterns) */ -#define M_TOKEN_COMMENT (1<<5) /* don't reap comments */ -#define M_TOKEN_SEMICOLON (1<<6) /* don't treat ; as special */ +DO_INIT(buffer_t, buffer); +static inline void buffer_wipe(buffer_t *buf) { + p_delete(&buf->data); +} +DO_NEW(buffer_t, buffer); +DO_DELETE(buffer_t, buffer); + +static inline char *buffer_unwrap(buffer_t **buf) { + char *res = (*buf)->data; + (*buf)->data = NULL; + buffer_delete(buf); + return res; +} +static inline void buffer_resize(buffer_t *buf, ssize_t newsize) { + p_allocgrow(&buf->data, newsize + 1, &buf->size); +} +static inline void buffer_ensure(buffer_t *buf, ssize_t extra) { + assert (extra >= 0); + if (buf->len + extra >= buf->size) { + buffer_resize(buf, buf->len + extra); + } +} +static inline void buffer_setlen(buffer_t *buf, ssize_t len) { + assert (buf->size > len); + buf->len = len; + buf->data[len] = '\0'; +} +static inline void buffer_extend(buffer_t *buf, ssize_t extra) { + buffer_ensure(buf, extra); + buf->len += extra; + buf->data[buf->len] = '\0'; +} +static inline void buffer_extendch(buffer_t *buf, ssize_t extra, int c) { + buffer_ensure(buf, extra); + memset(buf->data + buf->len, c, extra); + buf->len += extra; + buf->data[buf->len] = '\0'; +} + + +static inline void buffer_add(buffer_t *buf, const void *data, ssize_t len) { + buffer_ensure(buf, len); + memcpy(buf->data + buf->len, data, len); + buf->len += len; + buf->data[buf->len] = '\0'; +} +static inline void buffer_addstr(buffer_t *buf, const char *s) { + buffer_add(buf, s, m_strlen(s)); +} +static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) { + buffer_add(buf, buf2->data, buf2->len); +} +static inline void buffer_addch(buffer_t *buf, int c) { + buffer_extendch(buf, 1, c); +} + +static inline void buffer_reset(buffer_t *buf) { + if (buf->len) { + buf->len = 0; + buf->data[0] = '\0'; + } +} + +void buffer_splice(buffer_t *, ssize_t pos, ssize_t len, const void *, ssize_t); +void buffer_consume_upto(buffer_t *, const char *s); + +ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list) + __attribute__((format(printf, 2, 0))); + +ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); + + +/****** LEGACY BUFFERS *******/ + typedef struct { char *data; /* pointer to data */ char *dptr; /* current read/write position */ - size_t dsize; /* length of data */ + ssize_t dsize; /* length of data */ int destroy; /* destroy `data' when done? */ } BUFFER; @@ -52,9 +123,7 @@ BUFFER *mutt_buffer_init(BUFFER *); void mutt_buffer_free(BUFFER **); BUFFER *mutt_buffer_from(BUFFER *, const char *); -int mutt_extract_token(BUFFER *, BUFFER *, int); - -void mutt_buffer_add(BUFFER *, const char *, size_t); +void mutt_buffer_add(BUFFER *, const char *, ssize_t); static inline void mutt_buffer_addstr(BUFFER *b, const char *s) { mutt_buffer_add(b, s, m_strlen(s)); } @@ -63,4 +132,8 @@ static inline void mutt_buffer_addch(BUFFER *b, char c) { mutt_buffer_add(b, &c, 1); } +static inline void mutt_buffer_reset(BUFFER *b) { + *(b->dptr = b->data) = '\0'; +} + #endif /* MUTT_LIB_LIB_BUFFER_H */