cruft
[apps/madmutt.git] / lib-lib / buffer.h
1 /*
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.
6  *
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.
11  *
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,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  *
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.
27  */
28
29 #ifndef MUTT_LIB_LIB_BUFFER_H
30 #define MUTT_LIB_LIB_BUFFER_H
31
32 typedef struct buffer_t {
33     char *data;
34     ssize_t len;
35     ssize_t size;
36 } buffer_t;
37
38 DO_INIT(buffer_t, buffer);
39 static inline void buffer_wipe(buffer_t *buf) {
40     p_delete(&buf->data);
41 }
42 DO_NEW(buffer_t, buffer);
43 DO_DELETE(buffer_t, buffer);
44
45 static inline char *buffer_unwrap(buffer_t **buf) {
46     char *res = (*buf)->data;
47     (*buf)->data = NULL;
48     buffer_delete(buf);
49     return res;
50 }
51
52
53 void buffer_resize(buffer_t *, ssize_t newsize);
54 static inline void buffer_ensure(buffer_t *buf, ssize_t extra) {
55     assert (extra >= 0);
56     if (buf->len + extra >= buf->size) {
57         buffer_resize(buf, buf->len + extra);
58     }
59 }
60 static inline void buffer_extend(buffer_t *buf, ssize_t extra) {
61     buffer_ensure(buf, extra);
62     buf->len += extra;
63     buf->data[buf->len] = '\0';
64 }
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);
68     buf->len += extra;
69     buf->data[buf->len] = '\0';
70 }
71
72
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);
76     buf->len += len;
77     buf->data[buf->len] = '\0';
78 }
79 static inline void buffer_addstr(buffer_t *buf, const char *s) {
80     buffer_add(buf, s, m_strlen(s));
81 }
82 static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) {
83     buffer_add(buf, buf2->data, buf2->len);
84 }
85 static inline void buffer_addch(buffer_t *buf, int c) {
86     buffer_extendch(buf, 1, c);
87 }
88
89 static inline void buffer_reset(buffer_t *buf) {
90     if (buf->len) {
91         buf->len = 0;
92         buf->data[0] = '\0';
93     }
94 }
95
96
97 /****** LEGACY BUFFERS *******/
98
99 typedef struct {
100     char *data;          /* pointer to data */
101     char *dptr;          /* current read/write position */
102     ssize_t dsize;       /* length of data */
103     int destroy;         /* destroy `data' when done? */
104 } BUFFER;
105
106 BUFFER *mutt_buffer_init(BUFFER *);
107 void mutt_buffer_free(BUFFER **);
108
109 BUFFER *mutt_buffer_from(BUFFER *, const char *);
110 void mutt_buffer_add(BUFFER *, const char *, ssize_t);
111 static inline void mutt_buffer_addstr(BUFFER *b, const char *s) {
112     mutt_buffer_add(b, s, m_strlen(s));
113 }
114
115 static inline void mutt_buffer_addch(BUFFER *b, char c) {
116     mutt_buffer_add(b, &c, 1);
117 }
118
119 static inline void mutt_buffer_reset(BUFFER *b) {
120     *(b->dptr = b->data) = '\0';
121 }
122
123 #endif /* MUTT_LIB_LIB_BUFFER_H */