]> granicus.if.org Git - mutt/commitdiff
Rearrange and clean up buffer functions.
authorKevin McCarthy <kevin@8t8.us>
Sat, 6 Oct 2018 20:08:13 +0000 (13:08 -0700)
committerKevin McCarthy <kevin@8t8.us>
Sat, 6 Oct 2018 20:18:20 +0000 (13:18 -0700)
buffer.c
buffer.h

index d95cfe2114fbe7778f3fccb32c22f50a8663fe87..ab02c15a6b6fd6080f82db5d9120e391f4fa8ad5 100644 (file)
--- a/buffer.c
+++ b/buffer.c
 #include "mutt.h"
 #include "buffer.h"
 
-/* creates and initializes a BUFFER */
-BUFFER *mutt_buffer_new(void) {
+/* Creates and initializes a BUFFER */
+BUFFER *mutt_buffer_new (void)
+{
   BUFFER *b;
 
-  b = safe_malloc(sizeof(BUFFER));
+  b = safe_malloc (sizeof(BUFFER));
 
-  mutt_buffer_init(b);
+  mutt_buffer_init (b);
 
   return b;
 }
 
-/* initialize a new BUFFER */
-BUFFER *mutt_buffer_init (BUFFER *b) {
-  memset(b, 0, sizeof(BUFFER));
+/* Initialize a new BUFFER */
+BUFFER *mutt_buffer_init (BUFFER *b)
+{
+  memset (b, 0, sizeof(BUFFER));
   return b;
 }
 
-/* Increases the allocated size of the buffer */
-void mutt_buffer_increase_size (BUFFER *buf, size_t new_size)
+void mutt_buffer_free (BUFFER **p)
 {
-  size_t offset;
+  if (!p || !*p)
+    return;
 
-  if (buf->dsize < new_size)
-  {
-    offset = buf->dptr - buf->data;
-    buf->dsize = new_size;
-    safe_realloc (&buf->data, buf->dsize);
-    buf->dptr = buf->data + offset;
-  }
+   FREE (&(*p)->data);
+   /* dptr is just an offset to data and shouldn't be freed */
+   FREE (p);           /* __FREE_CHECKED__ */
 }
 
-/*
- * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
- * just initializes. Frees anything already in the buffer. Copies in
- * the seed string.
- *
- * Disregards the 'destroy' flag, which seems reserved for caller.
- * This is bad, but there's no apparent protocol for it.
- */
-BUFFER *mutt_buffer_from (char *seed) {
+/* Creates and initializes a BUFFER by copying the seed string. */
+BUFFER *mutt_buffer_from (char *seed)
+{
   BUFFER *b;
 
   if (!seed)
     return NULL;
 
   b = mutt_buffer_new ();
-  b->data = safe_strdup(seed);
-  b->dsize = mutt_strlen(seed);
+  b->data = safe_strdup (seed);
+  b->dsize = mutt_strlen (seed);
   b->dptr = (char *) b->data + b->dsize;
   return b;
 }
 
+/* Increases the allocated size of the buffer */
+void mutt_buffer_increase_size (BUFFER *buf, size_t new_size)
+{
+  size_t offset;
+
+  if (buf->dsize < new_size)
+  {
+    offset = buf->dptr - buf->data;
+    buf->dsize = new_size;
+    safe_realloc (&buf->data, buf->dsize);
+    buf->dptr = buf->data + offset;
+  }
+}
+
 int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
 {
   va_list ap, ap_retry;
   int len, blen, doff;
-  
+
   va_start (ap, fmt);
   va_copy (ap_retry, ap);
 
@@ -94,18 +100,14 @@ int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
   if (!blen)
   {
     blen = 128;
-    buf->dsize += blen;
-    safe_realloc (&buf->data, buf->dsize);
-    buf->dptr = buf->data + doff;
+    mutt_buffer_increase_size (buf, buf->dsize + blen);
   }
   if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
   {
     blen = ++len - blen;
     if (blen < 128)
       blen = 128;
-    buf->dsize += blen;
-    safe_realloc (&buf->data, buf->dsize);
-    buf->dptr = buf->data + doff;
+    mutt_buffer_increase_size (buf, buf->dsize + blen);
     len = vsnprintf (buf->dptr, len, fmt, ap_retry);
   }
   if (len > 0)
@@ -117,6 +119,18 @@ int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
   return len;
 }
 
+/* Dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
+ * Always one byte bigger than necessary for the null terminator, and
+ * the buffer is always null-terminated */
+static void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
+{
+  if (buf->dptr + len + 1 > buf->data + buf->dsize)
+    mutt_buffer_increase_size (buf, buf->dsize + (len < 128 ? 128 : len + 1));
+  memcpy (buf->dptr, s, len);
+  buf->dptr += len;
+  *(buf->dptr) = '\0';
+}
+
 void mutt_buffer_addstr (BUFFER* buf, const char* s)
 {
   mutt_buffer_add (buf, s, mutt_strlen (s));
@@ -126,33 +140,3 @@ void mutt_buffer_addch (BUFFER* buf, char c)
 {
   mutt_buffer_add (buf, &c, 1);
 }
-
-void mutt_buffer_free (BUFFER **p)
-{
-  if (!p || !*p) 
-    return;
-
-   FREE(&(*p)->data);
-   /* dptr is just an offset to data and shouldn't be freed */
-   FREE(p);            /* __FREE_CHECKED__ */
-}
-
-/* dynamically grows a BUFFER to accommodate s, in increments of 128 bytes.
- * Always one byte bigger than necessary for the null terminator, and
- * the buffer is always null-terminated */
-void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
-{
-  size_t offset;
-
-  if (buf->dptr + len + 1 > buf->data + buf->dsize)
-  {
-    offset = buf->dptr - buf->data;
-    buf->dsize += len < 128 ? 128 : len + 1;
-    /* suppress compiler aliasing warning */
-    safe_realloc ((void**) (void*) &buf->data, buf->dsize);
-    buf->dptr = buf->data + offset;
-  }
-  memcpy (buf->dptr, s, len);
-  buf->dptr += len;
-  *(buf->dptr) = '\0';
-}
index a73600e976f34a2a2ecb4e5087a4498c0e4d09c3..328884031023f1cc8e987b51ea5a5e6c123fd603 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -30,12 +30,13 @@ typedef struct
 } BUFFER;
 
 BUFFER *mutt_buffer_new (void);
-BUFFER * mutt_buffer_init (BUFFER *);
+BUFFER *mutt_buffer_init (BUFFER *);
+void mutt_buffer_free (BUFFER **);
+BUFFER *mutt_buffer_from (char *);
+
 void mutt_buffer_increase_size (BUFFER *, size_t);
-BUFFER * mutt_buffer_from (char *);
-void mutt_buffer_free(BUFFER **);
+
 int mutt_buffer_printf (BUFFER*, const char*, ...);
-void mutt_buffer_add (BUFFER*, const char*, size_t);
 void mutt_buffer_addstr (BUFFER*, const char*);
 void mutt_buffer_addch (BUFFER*, char);