1 /**
2 * @file
3 * General purpose object for storing and parsing strings
4 *
5 * @authors
6 * Copyright (C) 2017 Ian Zimmerman <itz@primate.net>
7 * Copyright (C) 2017-2019 Richard Russon <rich@flatcap.org>
8 *
9 * @copyright
10 * This program is free software: you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free Software
12 * Foundation, either version 2 of the License, or (at your option) any later
13 * version.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef MUTT_LIB_BUFFER_H
25 #define MUTT_LIB_BUFFER_H
26
27 #include <stddef.h>
28 #include <stdbool.h>
29
30 /**
31 * struct Buffer - String manipulation buffer
32 */
33 struct Buffer
34 {
35 char *data; ///< Pointer to data
36 char *dptr; ///< Current read/write position
37 size_t dsize; ///< Length of data
38 };
39
40 #define MoreArgs(buf) (*(buf)->dptr && (*(buf)->dptr != ';') && (*(buf)->dptr != '#'))
41
42 void mutt_buffer_alloc (struct Buffer *buf, size_t size);
43 void mutt_buffer_dealloc (struct Buffer *buf);
44 void mutt_buffer_fix_dptr (struct Buffer *buf);
45 struct Buffer *mutt_buffer_init (struct Buffer *buf);
46 bool mutt_buffer_is_empty (const struct Buffer *buf);
47 size_t mutt_buffer_len (const struct Buffer *buf);
48 struct Buffer mutt_buffer_make (size_t size);
49 void mutt_buffer_reset (struct Buffer *buf);
50 char * mutt_buffer_strdup (const struct Buffer *buf);
51 void mutt_buffer_seek (struct Buffer *buf, size_t offset);
52
53 // Functions that APPEND to a Buffer
54 size_t mutt_buffer_addch (struct Buffer *buf, char c);
55 size_t mutt_buffer_addstr (struct Buffer *buf, const char *s);
56 size_t mutt_buffer_addstr_n (struct Buffer *buf, const char *s, size_t len);
57 int mutt_buffer_add_printf (struct Buffer *buf, const char *fmt, ...);
58
59 // Functions that OVERWRITE a Buffer
60 size_t mutt_buffer_concat_path (struct Buffer *buf, const char *dir, const char *fname);
61 size_t mutt_buffer_concatn_path (struct Buffer *dst, const char *dir, size_t dirlen, const char *fname, size_t fnamelen);
62 size_t mutt_buffer_copy (struct Buffer *dst, const struct Buffer *src);
63 int mutt_buffer_printf (struct Buffer *buf, const char *fmt, ...);
64 size_t mutt_buffer_strcpy (struct Buffer *buf, const char *s);
65 size_t mutt_buffer_strcpy_n (struct Buffer *buf, const char *s, size_t len);
66 size_t mutt_buffer_substrcpy (struct Buffer *buf, const char *beg, const char *end);
67
68 /**
69 * mutt_buffer_string - Convert a buffer to a const char * "string"
70 * @param buf Buffer to that is to be converted
71 * @retval ptr String inside the Buffer
72 *
73 * This method exposes Buffer's underlying data field
74 *
75 * @note Returns an empty string if Buffer isn't initialised
76 */
mutt_buffer_string(const struct Buffer * buf)77 static inline const char *mutt_buffer_string(const struct Buffer *buf)
78 {
79 if (!buf || !buf->data)
80 return "";
81
82 return buf->data;
83 }
84
85 #endif /* MUTT_LIB_BUFFER_H */
86