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 void buffer_resize(buffer_t *, ssize_t newsize);
54 static inline void buffer_ensure(buffer_t *buf, ssize_t extra) {
56 if (buf->len + extra >= buf->size) {
57 buffer_resize(buf, buf->len + extra);
60 static inline void buffer_extend(buffer_t *buf, ssize_t extra) {
61 buffer_ensure(buf, extra);
63 buf->data[buf->len] = '\0';
65 static inline void buffer_extendch(buffer_t *buf, ssize_t extra, int c) {
66 buffer_ensure(buf, extra);
67 memset(buf->data + buf->len, c, extra);
69 buf->data[buf->len] = '\0';
73 static inline void buffer_add(buffer_t *buf, const void *data, ssize_t len) {
74 buffer_ensure(buf, len);
75 memcpy(buf->data + buf->len, data, len);
77 buf->data[buf->len] = '\0';
79 static inline void buffer_addstr(buffer_t *buf, const char *s) {
80 buffer_add(buf, s, m_strlen(s));
82 static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) {
83 buffer_add(buf, buf2->data, buf2->len);
85 static inline void buffer_addch(buffer_t *buf, int c) {
86 buffer_extendch(buf, 1, c);
90 /****** LEGACY BUFFERS *******/
93 char *data; /* pointer to data */
94 char *dptr; /* current read/write position */
95 ssize_t dsize; /* length of data */
96 int destroy; /* destroy `data' when done? */
99 BUFFER *mutt_buffer_init(BUFFER *);
100 void mutt_buffer_free(BUFFER **);
102 BUFFER *mutt_buffer_from(BUFFER *, const char *);
103 void mutt_buffer_add(BUFFER *, const char *, ssize_t);
104 static inline void mutt_buffer_addstr(BUFFER *b, const char *s) {
105 mutt_buffer_add(b, s, m_strlen(s));
108 static inline void mutt_buffer_addch(BUFFER *b, char c) {
109 mutt_buffer_add(b, &c, 1);
112 static inline void mutt_buffer_reset(BUFFER *b) {
113 *(b->dptr = b->data) = '\0';
116 #endif /* MUTT_LIB_LIB_BUFFER_H */