2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
24 * This file is part of mutt-ng, see http://www.muttng.org/.
25 * It's licensed under the GNU General Public License,
26 * please see the file GPL in the top level source directory.
29 #ifndef MUTT_LIB_LIB_BUFFER_H
30 #define MUTT_LIB_LIB_BUFFER_H
32 typedef struct buffer_t {
38 DO_INIT(buffer_t, buffer);
39 static inline void buffer_wipe(buffer_t *buf) {
42 DO_NEW(buffer_t, buffer);
43 DO_DELETE(buffer_t, buffer);
45 static inline char *buffer_unwrap(buffer_t **buf) {
46 char *res = (*buf)->data;
53 static inline void buffer_resize(buffer_t *buf, ssize_t newsize) {
54 p_allocgrow(&buf->data, newsize + 1, &buf->size);
56 static inline void buffer_ensure(buffer_t *buf, ssize_t extra) {
58 if (buf->len + extra >= buf->size) {
59 buffer_resize(buf, buf->len + extra);
62 static inline void buffer_setlen(buffer_t *buf, ssize_t len) {
63 assert (buf->size > len);
65 buf->data[len] = '\0';
67 static inline void buffer_extend(buffer_t *buf, ssize_t extra) {
68 buffer_ensure(buf, extra);
70 buf->data[buf->len] = '\0';
72 static inline void buffer_extendch(buffer_t *buf, ssize_t extra, int c) {
73 buffer_ensure(buf, extra);
74 memset(buf->data + buf->len, c, extra);
76 buf->data[buf->len] = '\0';
80 static inline void buffer_add(buffer_t *buf, const void *data, ssize_t len) {
81 buffer_ensure(buf, len);
82 memcpy(buf->data + buf->len, data, len);
84 buf->data[buf->len] = '\0';
86 static inline void buffer_addstr(buffer_t *buf, const char *s) {
87 buffer_add(buf, s, m_strlen(s));
89 static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) {
90 buffer_add(buf, buf2->data, buf2->len);
92 static inline void buffer_addch(buffer_t *buf, int c) {
93 buffer_extendch(buf, 1, c);
96 static inline void buffer_reset(buffer_t *buf) {
103 void buffer_splice(buffer_t *, ssize_t pos, ssize_t len, const void *, ssize_t);
105 ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list)
106 __attribute__((format(printf, 2, 0)));
108 ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...)
109 __attribute__((format(printf, 2, 3)));
112 /****** LEGACY BUFFERS *******/
115 char *data; /* pointer to data */
116 char *dptr; /* current read/write position */
117 ssize_t dsize; /* length of data */
118 int destroy; /* destroy `data' when done? */
121 BUFFER *mutt_buffer_init(BUFFER *);
122 void mutt_buffer_free(BUFFER **);
124 BUFFER *mutt_buffer_from(BUFFER *, const char *);
125 void mutt_buffer_add(BUFFER *, const char *, ssize_t);
126 static inline void mutt_buffer_addstr(BUFFER *b, const char *s) {
127 mutt_buffer_add(b, s, m_strlen(s));
130 static inline void mutt_buffer_addch(BUFFER *b, char c) {
131 mutt_buffer_add(b, &c, 1);
134 static inline void mutt_buffer_reset(BUFFER *b) {
135 *(b->dptr = b->data) = '\0';
138 #endif /* MUTT_LIB_LIB_BUFFER_H */