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>
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 typedef struct sdb_strbuf sdb_strbuf_t;
47 /*
48 * sdb_strbuf_create, sdb_strbuf_destroy:
49 * Allocate / deallocate string buffer objects. The initial size of a newly
50 * created string buffer is determined by the 'size' argument of the create
51 * function.
52 *
53 * sdb_strbuf_create returns:
54 * - the new string buffer object on success
55 * - NULL else
56 */
57 sdb_strbuf_t *
58 sdb_strbuf_create(size_t size);
60 void
61 sdb_strbuf_destroy(sdb_strbuf_t *strbuf);
63 /*
64 * sdb_strbuf_vappend, sdb_strbuf_append:
65 * Append to an existing string buffer. The new text will be added at the end
66 * of the current content of the buffer.
67 *
68 * The 'fmt' and all following arguments are identical to those passed to the
69 * sprintf / vsprintf functions.
70 *
71 * Returns:
72 * - the number of bytes written
73 * - a negative value on error
74 */
75 ssize_t
76 sdb_strbuf_vappend(sdb_strbuf_t *strbuf, const char *fmt, va_list ap);
77 ssize_t
78 sdb_strbuf_append(sdb_strbuf_t *strbuf, const char *fmt, ...);
80 /*
81 * sdb_strbuf_vsprintf, sdb_strbuf_sprintf:
82 * Write to an existing string buffer, overwriting any previous content.
83 *
84 * The 'fmt' and all following arguments are identical to those passed to the
85 * sprintf / vsprintf functions.
86 *
87 * Returns:
88 * - the number of bytes written
89 * - a negative value on error
90 */
91 ssize_t
92 sdb_strbuf_vsprintf(sdb_strbuf_t *strbuf, const char *fmt, va_list ap);
93 ssize_t
94 sdb_strbuf_sprintf(sdb_strbuf_t *strbuf, const char *fmt, ...);
96 /*
97 * sdb_strbuf_chomp:
98 * Remove all consecutive newline characters from the end of the string buffer
99 * content.
100 *
101 * Returns:
102 * - the number of bytes removed
103 * - a negative value on error
104 */
105 ssize_t
106 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
108 /*
109 * sdb_strbuf_string:
110 * Returns the content of the string buffer. The caller may not modify the
111 * string.
112 */
113 const char *
114 sdb_strbuf_string(sdb_strbuf_t *strbuf);
116 /*
117 * sdb_strbuf_len:
118 * Returns the length of the string buffer's content.
119 */
120 size_t
121 sdb_strbuf_len(sdb_strbuf_t *strbuf);
123 #ifdef __cplusplus
124 } /* extern "C" */
125 #endif
127 #endif /* ! SDB_UTILS_STRBUF_H */
129 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */