Code

strbuf.h: Include unistd.h.
[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_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_chomp:
99  * Remove all consecutive newline characters from the end of the string buffer
100  * content.
101  *
102  * Returns:
103  *  - the number of bytes removed
104  *  - a negative value on error
105  */
106 ssize_t
107 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
109 /*
110  * sdb_strbuf_string:
111  * Returns the content of the string buffer. The caller may not modify the
112  * string.
113  */
114 const char *
115 sdb_strbuf_string(sdb_strbuf_t *strbuf);
117 /*
118  * sdb_strbuf_len:
119  * Returns the length of the string buffer's content.
120  */
121 size_t
122 sdb_strbuf_len(sdb_strbuf_t *strbuf);
124 #ifdef __cplusplus
125 } /* extern "C" */
126 #endif
128 #endif /* ! SDB_UTILS_STRBUF_H */
130 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */