Code

utils strbuf: Added sdb_strbuf_read().
[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>
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_memcpy, sdb_strbuf_memappend:
98  * Copy or a append a memory area to the buffer. These functions do not
99  * interpret any information in the data pointer (including \0 bytes).
100  *
101  * These functions may be used to handle arbitrary byte arrays. Mixing these
102  * function calls with any of the printf-style function works but will usually
103  * lead to arbitrary behavior.
104  *
105  * Returns:
106  *  - the number of bytes written
107  *  - a negative value on error
108  */
109 ssize_t
110 sdb_strbuf_memcpy(sdb_strbuf_t *strbuf, const void *data, size_t n);
111 ssize_t
112 sdb_strbuf_memappend(sdb_strbuf_t *strbuf, const void *data, size_t n);
114 /*
115  * sdb_strbuf_read:
116  * Read from an open file-descriptor and append the data to the buffer. The
117  * function does not handle *any* read errors. This allows for more
118  * flexibility for the caller regarding the handling of EAGAIN or EWOULDBLOCK.
119  *
120  * Returns:
121  *  - the number of bytes read (zero on EOF)
122  *  - a negative value on error
123  */
124 ssize_t
125 sdb_strbuf_read(sdb_strbuf_t *strbuf, int fd, size_t n);
127 /*
128  * sdb_strbuf_chomp:
129  * Remove all consecutive newline characters from the end of the string buffer
130  * content.
131  *
132  * Returns:
133  *  - the number of bytes removed
134  *  - a negative value on error
135  */
136 ssize_t
137 sdb_strbuf_chomp(sdb_strbuf_t *strbuf);
139 /*
140  * sdb_strbuf_string:
141  * Returns the content of the string buffer. The caller may not modify the
142  * string.
143  */
144 const char *
145 sdb_strbuf_string(sdb_strbuf_t *strbuf);
147 /*
148  * sdb_strbuf_len:
149  * Returns the length of the string buffer's content.
150  */
151 size_t
152 sdb_strbuf_len(sdb_strbuf_t *strbuf);
154 #ifdef __cplusplus
155 } /* extern "C" */
156 #endif
158 #endif /* ! SDB_UTILS_STRBUF_H */
160 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */