Code

store, plugin: Let the plugin module determine an objects backends.
[sysdb.git] / src / include / utils / strbuf.h
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_STR:
50  * Return a tuple of a character array and its length representing the content
51  * of the string buffer.
52  */
53 #define SDB_STRBUF_STR(buf) sdb_strbuf_string(buf), sdb_strbuf_len(buf)
55 /*
56  * sdb_strbuf_create, sdb_strbuf_destroy:
57  * Allocate / deallocate string buffer objects. The initial size of a newly
58  * created string buffer is determined by the 'size' argument of the create
59  * function.
60  *
61  * sdb_strbuf_create returns:
62  *  - the new string buffer object on success
63  *  - NULL else
64  */
65 sdb_strbuf_t *
66 sdb_strbuf_create(size_t size);
68 void
69 sdb_strbuf_destroy(sdb_strbuf_t *strbuf);
71 /*
72  * sdb_strbuf_vappend, sdb_strbuf_append:
73  * Append to an existing string buffer. The new text will be added at the end
74  * of the current content of the buffer.
75  *
76  * The 'fmt' and all following arguments are identical to those passed to the
77  * sprintf / vsprintf functions.
78  *
79  * Returns:
80  *  - the number of bytes written
81  *  - a negative value on error
82  */
83 ssize_t
84 sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
85                 __attribute__((format(printf, 2, 0)));
86 ssize_t
87 sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...)
88                 __attribute__((format(printf, 2, 3)));
90 /*
91  * sdb_strbuf_vsprintf, sdb_strbuf_sprintf:
92  * Write to an existing string buffer, overwriting any previous content.
93  *
94  * The 'fmt' and all following arguments are identical to those passed to the
95  * sprintf / vsprintf functions.
96  *
97  * Returns:
98  *  - the number of bytes written
99  *  - a negative value on error
100  */
101 ssize_t
102 sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap)
103                 __attribute__((format(printf, 2, 0)));
104 ssize_t
105 sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...)
106                 __attribute__((format(printf, 2, 3)));
108 /*
109  * sdb_strbuf_memcpy, sdb_strbuf_memappend:
110  * Copy or a append a memory area to the buffer. These functions do not
111  * interpret any information in the data pointer (including \0 bytes).
112  *
113  * These functions may be used to handle arbitrary byte arrays. Mixing these
114  * function calls with any of the printf-style function works fine but the
115  * entire buffer content should then be treated as arbitrary bytes.
116  *
117  * Returns:
118  *  - the number of bytes written
119  *  - a negative value on error
120  */
121 ssize_t
122 sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n);
123 ssize_t
124 sdb_strbuf_memappend(sdb_strbuf_t *strbuf, const void *data, size_t n);
126 /*
127  * sdb_strbuf_read:
128  * Read from an open file-descriptor and append the data to the buffer. The
129  * function does not handle *any* read errors. This allows for more
130  * flexibility for the caller regarding the handling of EAGAIN or EWOULDBLOCK.
131  *
132  * Returns:
133  *  - the number of bytes read (zero on EOF)
134  *  - a negative value on error
135  */
136 ssize_t
137 sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n);
139 /*
140  * sdb_strbuf_chomp:
141  * Remove all consecutive newline characters from the end of the string buffer
142  * content.
143  *
144  * Returns:
145  *  - the number of bytes removed
146  *  - a negative value on error
147  */
148 ssize_t
149 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
151 /*
152  * sdb_strbuf_skip:
153  * Removes 'n' bytes from the buffer starting at offset 'offset'.
154  */
155 void
156 sdb_strbuf_skip(sdb_strbuf_t *strbuf, size_t offset, size_t n);
158 /*
159  * sdb_strbuf_clear:
160  * Clear the buffer but do not deallocate memory.
161  */
162 void
163 sdb_strbuf_clear(sdb_strbuf_t *strbuf);
165 /*
166  * sdb_strbuf_string:
167  * Returns the content of the string buffer. The caller may not modify the
168  * string.
169  */
170 const char *
171 sdb_strbuf_string(sdb_strbuf_t *strbuf);
173 /*
174  * sdb_strbuf_len:
175  * Returns the length of the string buffer's content.
176  */
177 size_t
178 sdb_strbuf_len(sdb_strbuf_t *strbuf);
180 /*
181  * sdb_strbuf_cap:
182  * Returns the current capacity of the string buffer. It describes the max
183  * length of the buffer's content (including terminating nul byte) that may be
184  * stored in the buffer without resizing it.
185  */
186 size_t
187 sdb_strbuf_cap(sdb_strbuf_t *strbuf);
189 #ifdef __cplusplus
190 } /* extern "C" */
191 #endif
193 #endif /* ! SDB_UTILS_STRBUF_H */
195 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */