2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
22 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
23 * just initializes. Frees anything already in the buffer.
25 * Disregards the 'destroy' flag, which seems reserved for caller.
26 * This is bad, but there's no apparent protocol for it.
28 BUFFER *mutt_buffer_init (BUFFER * b)
31 b = mem_malloc (sizeof (BUFFER));
38 memset (b, 0, sizeof (BUFFER));
43 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
44 * just initializes. Frees anything already in the buffer. Copies in
47 * Disregards the 'destroy' flag, which seems reserved for caller.
48 * This is bad, but there's no apparent protocol for it.
50 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
55 b = mutt_buffer_init (b);
56 b->data = str_dup (seed);
57 b->dsize = str_len (seed);
58 b->dptr = (char *) b->data + b->dsize;
62 void mutt_buffer_addstr (BUFFER * buf, const char *s)
64 mutt_buffer_add (buf, s, str_len (s));
67 void mutt_buffer_addch (BUFFER * buf, char c)
69 mutt_buffer_add (buf, &c, 1);
72 void mutt_buffer_free (BUFFER ** p)
77 mem_free (&(*p)->data);
78 /* dptr is just an offset to data and shouldn't be freed */
82 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
83 * Always one byte bigger than necessary for the null terminator, and
84 * the buffer is always null-terminated */
85 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
89 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
90 offset = buf->dptr - buf->data;
91 buf->dsize += len < 128 ? 128 : len + 1;
92 mem_realloc ((void **) &buf->data, buf->dsize);
93 buf->dptr = buf->data + offset;
95 memcpy (buf->dptr, s, len);