1 /*
2 * SysDB - src/include/utils/strbuf.h
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * SysDB string buffer:
30 * This is an implementation of an automatically growing string. Whenever
31 * writing to the buffer, it will be ensured that enough space is allocated to
32 * store all of the string.
33 */
35 #ifndef SDB_UTILS_STRBUF_H
36 #define SDB_UTILS_STRBUF_H 1
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <unistd.h>
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 typedef struct sdb_strbuf sdb_strbuf_t;
48 /*
49 * sdb_strbuf_create, sdb_strbuf_destroy:
50 * Allocate / deallocate string buffer objects. The initial size of a newly
51 * created string buffer is determined by the 'size' argument of the create
52 * function.
53 *
54 * sdb_strbuf_create returns:
55 * - the new string buffer object on success
56 * - NULL else
57 */
58 sdb_strbuf_t *
59 sdb_strbuf_create(size_t size);
61 void
62 sdb_strbuf_destroy(sdb_strbuf_t *strbuf);
64 /*
65 * sdb_strbuf_vappend, sdb_strbuf_append:
66 * Append to an existing string buffer. The new text will be added at the end
67 * of the current content of the buffer.
68 *
69 * The 'fmt' and all following arguments are identical to those passed to the
70 * sprintf / vsprintf functions.
71 *
72 * Returns:
73 * - the number of bytes written
74 * - a negative value on error
75 */
76 ssize_t
77 sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
78 __attribute__((format(printf, 2, 0)));
79 ssize_t
80 sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...)
81 __attribute__((format(printf, 2, 3)));
83 /*
84 * sdb_strbuf_vsprintf, sdb_strbuf_sprintf:
85 * Write to an existing string buffer, overwriting any previous content.
86 *
87 * The 'fmt' and all following arguments are identical to those passed to the
88 * sprintf / vsprintf functions.
89 *
90 * Returns:
91 * - the number of bytes written
92 * - a negative value on error
93 */
94 ssize_t
95 sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
96 __attribute__((format(printf, 2, 0)));
97 ssize_t
98 sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...)
99 __attribute__((format(printf, 2, 3)));
101 /*
102 * sdb_strbuf_memcpy, sdb_strbuf_memappend:
103 * Copy or a append a memory area to the buffer. These functions do not
104 * interpret any information in the data pointer (including \0 bytes).
105 *
106 * These functions may be used to handle arbitrary byte arrays. Mixing these
107 * function calls with any of the printf-style function works fine but the
108 * entire buffer content should then be treated as arbitrary bytes.
109 *
110 * Returns:
111 * - the number of bytes written
112 * - a negative value on error
113 */
114 ssize_t
115 sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n);
116 ssize_t
117 sdb_strbuf_memappend(sdb_strbuf_t *strbuf, const void *data, size_t n);
119 /*
120 * sdb_strbuf_read:
121 * Read from an open file-descriptor and append the data to the buffer. The
122 * function does not handle *any* read errors. This allows for more
123 * flexibility for the caller regarding the handling of EAGAIN or EWOULDBLOCK.
124 *
125 * Returns:
126 * - the number of bytes read (zero on EOF)
127 * - a negative value on error
128 */
129 ssize_t
130 sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n);
132 /*
133 * sdb_strbuf_chomp:
134 * Remove all consecutive newline characters from the end of the string buffer
135 * content.
136 *
137 * Returns:
138 * - the number of bytes removed
139 * - a negative value on error
140 */
141 ssize_t
142 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
144 /*
145 * sdb_strbuf_skip:
146 * Removes 'n' bytes from the buffer starting at offset 'offset'.
147 */
148 void
149 sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t offset, size_t n);
151 /*
152 * sdb_strbuf_clear:
153 * Clear the buffer but do not deallocate memory.
154 */
155 void
156 sdb_strbuf_clear(sdb_strbuf_t *strbuf);
158 /*
159 * sdb_strbuf_string:
160 * Returns the content of the string buffer. The caller may not modify the
161 * string.
162 */
163 const char *
164 sdb_strbuf_string(sdb_strbuf_t *strbuf);
166 /*
167 * sdb_strbuf_len:
168 * Returns the length of the string buffer's content.
169 */
170 size_t
171 sdb_strbuf_len(sdb_strbuf_t *strbuf);
173 /*
174 * sdb_strbuf_cap:
175 * Returns the current capacity of the string buffer. It describes the max
176 * length of the buffer's content (including terminating nul byte) that may be
177 * stored in the buffer without resizing it.
178 */
179 size_t
180 sdb_strbuf_cap(sdb_strbuf_t *strbuf);
182 #ifdef __cplusplus
183 } /* extern "C" */
184 #endif
186 #endif /* ! SDB_UTILS_STRBUF_H */
188 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */