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 ssize_t
79 sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...);
81 /*
82 * sdb_strbuf_vsprintf, sdb_strbuf_sprintf:
83 * Write to an existing string buffer, overwriting any previous content.
84 *
85 * The 'fmt' and all following arguments are identical to those passed to the
86 * sprintf / vsprintf functions.
87 *
88 * Returns:
89 * - the number of bytes written
90 * - a negative value on error
91 */
92 ssize_t
93 sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap);
94 ssize_t
95 sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...);
97 /*
98 * sdb_strbuf_memcpy, sdb_strbuf_memappend:
99 * Copy or a append a memory area to the buffer. These functions do not
100 * interpret any information in the data pointer (including \0 bytes).
101 *
102 * These functions may be used to handle arbitrary byte arrays. Mixing these
103 * function calls with any of the printf-style function works but will usually
104 * lead to arbitrary behavior.
105 *
106 * Returns:
107 * - the number of bytes written
108 * - a negative value on error
109 */
110 ssize_t
111 sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n);
112 ssize_t
113 sdb_strbuf_memappend(sdb_strbuf_t *strbuf, const void *data, size_t n);
115 /*
116 * sdb_strbuf_read:
117 * Read from an open file-descriptor and append the data to the buffer. The
118 * function does not handle *any* read errors. This allows for more
119 * flexibility for the caller regarding the handling of EAGAIN or EWOULDBLOCK.
120 *
121 * Returns:
122 * - the number of bytes read (zero on EOF)
123 * - a negative value on error
124 */
125 ssize_t
126 sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n);
128 /*
129 * sdb_strbuf_chomp:
130 * Remove all consecutive newline characters from the end of the string buffer
131 * content.
132 *
133 * Returns:
134 * - the number of bytes removed
135 * - a negative value on error
136 */
137 ssize_t
138 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
140 /*
141 * sdb_strbuf_skip:
142 * Removes 'n' bytes from the buffer starting at offset 'offset'.
143 */
144 void
145 sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t offset, size_t n);
147 /*
148 * sdb_strbuf_clear:
149 * Clear the buffer but do not deallocate memory.
150 */
151 void
152 sdb_strbuf_clear(sdb_strbuf_t *strbuf);
154 /*
155 * sdb_strbuf_string:
156 * Returns the content of the string buffer. The caller may not modify the
157 * string.
158 */
159 const char *
160 sdb_strbuf_string(sdb_strbuf_t *strbuf);
162 /*
163 * sdb_strbuf_len:
164 * Returns the length of the string buffer's content.
165 */
166 size_t
167 sdb_strbuf_len(sdb_strbuf_t *strbuf);
169 /*
170 * sdb_strbuf_cap:
171 * Returns the current capacity of the string buffer. It describes the max
172 * length of the buffer's content (including terminating nul byte) that may be
173 * stored in the buffer without resizing it.
174 */
175 size_t
176 sdb_strbuf_cap(sdb_strbuf_t *strbuf);
178 #ifdef __cplusplus
179 } /* extern "C" */
180 #endif
182 #endif /* ! SDB_UTILS_STRBUF_H */
184 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */