Tue, 13 Aug 2013 14:20:12 +0200
completed documentation + changed API for buffer/stream generic copy functions
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. 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 are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 /**
30 * @file buffer.h
31 *
32 * Advanced buffer implementation.
33 *
34 * Instances of UcxBuffer can be used to read from or to write to like one
35 * would do with a stream. This allows the use of ucx_stream_copy() to copy
36 * contents from one buffer to another.
37 *
38 * Some features for convenient use of the buffer
39 * can be enabled. See the documentation of the macro constants for more
40 * information.
41 *
42 * @author Mike Becker
43 * @author Olaf Wintermann
44 */
46 #ifndef UCX_BUFFER_H
47 #define UCX_BUFFER_H
49 #include "ucx.h"
50 #include <sys/types.h>
51 #include <stdio.h>
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
57 /**
58 * No buffer features enabled (all flags cleared).
59 */
60 #define UCX_BUFFER_DEFAULT 0x00
61 /**
62 * If this flag is enabled, the buffer will automatically free its contents.
63 */
64 #define UCX_BUFFER_AUTOFREE 0x01
65 /**
66 * If this flag is enabled, the buffer will automatically extends its capacity.
67 */
68 #define UCX_BUFFER_AUTOEXTEND 0x02
70 /** UCX Buffer. */
71 typedef struct {
72 /** A pointer to the buffer contents. */
73 char *space;
74 /** Current position of the buffer. */
75 size_t pos;
76 /** Current capacity (i.e. maximum size) of the buffer. */
77 size_t capacity;
78 /** Current size of the buffer content. */
79 size_t size;
80 /**
81 * Flag register for buffer features.
82 * @see #UCX_BUFFER_DEFAULT
83 * @see #UCX_BUFFER_AUTOFREE
84 * @see #UCX_BUFFER_AUTOEXTEND
85 */
86 int flags;
87 } UcxBuffer;
89 /**
90 * Creates a new buffer.
91 *
92 * <b>Note:</b> you may provide <code>NULL</code> as argument for
93 * <code>space</code>. Then this function will allocate the space and enforce
94 * the #UCX_BUFFER_AUTOFREE flag.
95 *
96 * @param space pointer to the memory area, or <code>NULL</code> to allocate
97 * new memory
98 * @param size the size of the buffer
99 * @param flags buffer features (see UcxBuffer.flags)
100 * @return the new buffer
101 */
102 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags);
104 /**
105 * Destroys a buffer.
106 *
107 * If the #UCX_BUFFER_AUTOFREE feature is enabled, the contents of the buffer
108 * are also freed.
109 *
110 * @param buffer the buffer to destroy
111 */
112 void ucx_buffer_free(UcxBuffer* buffer);
114 /**
115 * Creates a new buffer and fills it with extracted content from another buffer.
116 *
117 * <b>Note:</b> the #UCX_BUFFER_AUTOFREE feature is enforced for the new buffer.
118 *
119 * @param src the source buffer
120 * @param start the start position of extraction
121 * @param length the count of bytes to extract or 0 if all of the remaining
122 * bytes shall be extracted
123 * @param flags feature mask for the new buffer
124 * @return
125 */
126 UcxBuffer* ucx_buffer_extract(UcxBuffer *src,
127 size_t start, size_t length, int flags);
129 /**
130 * A shorthand macro for the full extraction of the buffer.
131 *
132 * @param src the source buffer
133 * @param flags feature mask for the new buffer
134 * @return a new buffer with the extracted content
135 */
136 #define ucx_buffer_clone(src,flags) \
137 ucx_buffer_extract(src, 0, 0, flags)
139 /**
140 * Moves the position of the buffer.
141 *
142 * The new position is relative to the <code>whence</code> argument.
143 *
144 * SEEK_SET marks the start of the buffer.
145 * SEEK_CUR marks the current position.
146 * SEEK_END marks the first 0-byte in the buffer.
147 *
148 * @param buffer
149 * @param offset position offset relative to <code>whence</code>
150 * @param whence one of SEEK_SET, SEEK_CUR or SEEK_END
151 * @return 0 on success, non-zero if the position is invalid
152 *
153 */
154 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
156 /**
157 * Clears the buffer by resetting the position and deleting the data.
158 *
159 * The data is deleted by a zeroing it with call to <code>memset()</code>.
160 *
161 * @param buffer the buffer to be cleared
162 */
163 #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \
164 buffer->size = 0; buffer->pos = 0;
166 /**
167 * Tests, if the buffer position has exceeded the buffer capacity.
168 *
169 * @param buffer the buffer to test
170 * @return non-zero, if the current buffer position has exceeded the last
171 * available byte of the buffer.
172 */
173 int ucx_buffer_eof(UcxBuffer *buffer);
176 /**
177 * Extends the capacity of the buffer.
178 *
179 * <b>Note:</b> The buffer capacity increased by a power of two. I.e.
180 * the buffer capacity is doubled, as long as it would not hold the current
181 * content plus the additional required bytes.
182 *
183 * <b>Attention:</b> the argument provided is the count of <i>additional</i>
184 * bytes the buffer shall hold. It is <b>NOT</b> the total count of bytes the
185 * buffer shall hold.
186 *
187 * @param buffer the buffer to extend
188 * @param additional_bytes the count of additional bytes the buffer shall
189 * <i>at least</i> hold
190 * @return 0 on success or a non-zero value on failure
191 */
192 int ucx_buffer_extend(UcxBuffer *buffer, size_t additional_bytes);
194 /**
195 * Writes data to an UcxBuffer.
196 *
197 * The position of the buffer is increased by the number of bytes read.
198 *
199 * @param ptr a pointer to the memory area containing the bytes to be written
200 * @param size the length of one element
201 * @param nitems the element count
202 * @param buffer the UcxBuffer to write to
203 * @return the total count of bytes written
204 */
205 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
206 UcxBuffer *buffer);
208 /**
209 * Reads data from an UcxBuffer.
210 *
211 * The position of the buffer is increased by the number of bytes read.
212 *
213 * @param ptr a pointer to the memory area where to store the read data
214 * @param size the length of one element
215 * @param nitems the element count
216 * @param buffer the UcxBuffer to read from
217 * @return the total count of bytes read
218 */
219 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
220 UcxBuffer *buffer);
222 /**
223 * Writes a character to a buffer.
224 *
225 * The least significant byte of the argument is written to the buffer. If the
226 * end of the buffer is reached and #UCX_BUFFER_AUTOEXTEND feature is enabled,
227 * the buffer capacity is extended by ucx_buffer_extend(). If the feature is
228 * disabled or buffer extension fails, <code>EOF</code> is returned.
229 *
230 * On successful write the position of the buffer is increased.
231 *
232 * @param buffer the buffer to write to
233 * @param c the character to write as <code>int</code> value
234 * @return the byte that has bean written as <code>int</code> value or
235 * <code>EOF</code> when the end of the stream is reached and automatic
236 * extension is not enabled or not possible
237 */
238 int ucx_buffer_putc(UcxBuffer *buffer, int c);
240 /**
241 * Gets a character from a buffer.
242 *
243 * The current position of the buffer is increased after a successful read.
244 *
245 * @param buffer the buffer to read from
246 * @return the character as <code>int</code> value or <code>EOF</code>, if the
247 * end of the buffer is reached
248 */
249 int ucx_buffer_getc(UcxBuffer *buffer);
251 /**
252 * Writes a string to a buffer.
253 *
254 * @param buffer the buffer
255 * @param str the string
256 * @return the number of bytes written
257 */
258 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str);
260 #ifdef __cplusplus
261 }
262 #endif
264 #endif /* UCX_BUFFER_H */