Replace deprecated luaL_openlib() by luaL_register()
[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 static inline void buffer_resize(buffer_t *buf, ssize_t newsize) {
54     p_allocgrow(&buf->data, newsize + 1, &buf->size);
55 }
56 static inline void buffer_ensure(buffer_t *buf, ssize_t extra) {
57     assert (extra >= 0);
58     if (buf->len + extra >= buf->size) {
59         buffer_resize(buf, buf->len + extra);
60     }
61 }
62 static inline void buffer_setlen(buffer_t *buf, ssize_t len) {
63     assert (buf->size > len);
64     buf->len = len;
65     buf->data[len] = '\0';
66 }
67 static inline void buffer_extend(buffer_t *buf, ssize_t extra) {
68     buffer_ensure(buf, extra);
69     buf->len += extra;
70     buf->data[buf->len] = '\0';
71 }
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);
75     buf->len += extra;
76     buf->data[buf->len] = '\0';
77 }
78
79
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);
83     buf->len += len;
84     buf->data[buf->len] = '\0';
85 }
86 static inline void buffer_addstr(buffer_t *buf, const char *s) {
87     buffer_add(buf, s, m_strlen(s));
88 }
89 static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) {
90     buffer_add(buf, buf2->data, buf2->len);
91 }
92 static inline void buffer_addch(buffer_t *buf, int c) {
93     buffer_extendch(buf, 1, c);
94 }
95
96 static inline void buffer_reset(buffer_t *buf) {
97     if (buf->len) {
98         buf->len = 0;
99         buf->data[0] = '\0';
100     }
101 }
102
103 void buffer_splice(buffer_t *, ssize_t pos, ssize_t len, const void *, ssize_t);
104 void buffer_consume_upto(buffer_t *, const char *s);
105
106 ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list)
107     __attribute__((format(printf, 2, 0)));
108
109 ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...)
110     __attribute__((format(printf, 2, 3)));
111
112
113 /****** LEGACY BUFFERS *******/
114
115 typedef struct {
116     char *data;          /* pointer to data */
117     char *dptr;          /* current read/write position */
118     ssize_t dsize;       /* length of data */
119     int destroy;         /* destroy `data' when done? */
120 } BUFFER;
121
122 BUFFER *mutt_buffer_init(BUFFER *);
123 void mutt_buffer_free(BUFFER **);
124
125 BUFFER *mutt_buffer_from(BUFFER *, const char *);
126 void mutt_buffer_add(BUFFER *, const char *, ssize_t);
127 static inline void mutt_buffer_addstr(BUFFER *b, const char *s) {
128     mutt_buffer_add(b, s, m_strlen(s));
129 }
130
131 static inline void mutt_buffer_addch(BUFFER *b, char c) {
132     mutt_buffer_add(b, &c, 1);
133 }
134
135 static inline void mutt_buffer_reset(BUFFER *b) {
136     *(b->dptr = b->data) = '\0';
137 }
138
139 #endif /* MUTT_LIB_LIB_BUFFER_H */