preliminary work for the configuration parser.
authorPierre Habouzit <madcoder@debian.org>
Sat, 9 Dec 2006 00:48:25 +0000 (01:48 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sat, 9 Dec 2006 00:48:25 +0000 (01:48 +0100)
lib-lib/buffer.c
lib-lib/buffer.h
lib-lib/str.h

index f186bfd..b442721 100644 (file)
 
 #include "lib-lib.h"
 
 
 #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.
 /*
  * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
  * just initializes. Frees anything already in the buffer.
index a0c4588..a4d1912 100644 (file)
 #ifndef MUTT_LIB_LIB_BUFFER_H
 #define MUTT_LIB_LIB_BUFFER_H
 
 #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 */
 typedef struct {
     char *data;          /* pointer to data */
     char *dptr;          /* current read/write position */
index 1beac83..0136569 100644 (file)
@@ -94,6 +94,16 @@ extern char const __m_b36chars_upper[36];
 /* conversions                                                              */
 /****************************************************************************/
 
 /* conversions                                                              */
 /****************************************************************************/
 
+/** \brief Converts an octal digit into an int.
+ * \param[in]  c    the octal char
+ * \return
+ *   - 0&ndash;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
 /** \brief Converts an hexadecimal digit into an int.
  * \param[in]  c    the hexadecimal char
  * \return