From: Pierre Habouzit Date: Sat, 9 Dec 2006 00:48:25 +0000 (+0100) Subject: preliminary work for the configuration parser. X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=643b8bf1557f533756a5563ff1e5c416561c58ed preliminary work for the configuration parser. --- diff --git a/lib-lib/buffer.c b/lib-lib/buffer.c index f186bfd..b442721 100644 --- a/lib-lib/buffer.c +++ b/lib-lib/buffer.c @@ -28,6 +28,22 @@ #include "lib-lib.h" +#define BUFSIZ_INCREMENT 1024 + +void buffer_resize(buffer_t *buf, ssize_t newsize) +{ + if (newsize >= buf->size) { + /* rounds newsize to the 1024 multiple just after newsize+1 */ + newsize = (newsize + BUFSIZ_INCREMENT) & ~(BUFSIZ_INCREMENT - 1); + p_realloc(&buf->data, newsize); + } +} + + + + +/****** LEGACY BUFFERS *******/ + /* * Creates and initializes a BUFFER*. If passed an existing BUFFER*, * just initializes. Frees anything already in the buffer. diff --git a/lib-lib/buffer.h b/lib-lib/buffer.h index a0c4588..a4d1912 100644 --- a/lib-lib/buffer.h +++ b/lib-lib/buffer.h @@ -29,6 +29,66 @@ #ifndef MUTT_LIB_LIB_BUFFER_H #define MUTT_LIB_LIB_BUFFER_H +typedef struct buffer_t { + char *data; + ssize_t len; + ssize_t size; +} buffer_t; + +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; +} + + +void buffer_resize(buffer_t *, ssize_t newsize); +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_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); +} + + +/****** LEGACY BUFFERS *******/ + typedef struct { char *data; /* pointer to data */ char *dptr; /* current read/write position */ diff --git a/lib-lib/str.h b/lib-lib/str.h index 1beac83..0136569 100644 --- a/lib-lib/str.h +++ b/lib-lib/str.h @@ -94,6 +94,16 @@ extern char const __m_b36chars_upper[36]; /* conversions */ /****************************************************************************/ +/** \brief Converts an octal digit into an int. + * \param[in] c the octal char + * \return + * - 0–7 if c is a valid octal digit, + * - -1 on error. + */ +static inline int octval(int c) { + return !(c & ~127) && __m_strdigits[c] < 7 ? __m_strdigits[c] : -1; +} + /** \brief Converts an hexadecimal digit into an int. * \param[in] c the hexadecimal char * \return