src/cx/buffer.h

changeset 529
814d51173f20
parent 501
9a08f5e515cc
child 530
e866516cac17
equal deleted inserted replaced
528:4fbfac557df8 529:814d51173f20
82 * Data is interpreted as binary. 82 * Data is interpreted as binary.
83 */ 83 */
84 unsigned char *bytes; 84 unsigned char *bytes;
85 }; 85 };
86 /** The allocator to use for automatic memory management. */ 86 /** The allocator to use for automatic memory management. */
87 CxAllocator *allocator; 87 CxAllocator const *allocator;
88 /** Current position of the buffer. */ 88 /** Current position of the buffer. */
89 size_t pos; 89 size_t pos;
90 /** Current capacity (i.e. maximum size) of the buffer. */ 90 /** Current capacity (i.e. maximum size) of the buffer. */
91 size_t capacity; 91 size_t capacity;
92 /** Current size of the buffer content. */ 92 /** Current size of the buffer content. */
118 * @param capacity the capacity of the buffer 118 * @param capacity the capacity of the buffer
119 * @param allocator the allocator this buffer shall use for automatic memory management 119 * @param allocator the allocator this buffer shall use for automatic memory management
120 * @param flags buffer features (see cx_buffer_s.flags) 120 * @param flags buffer features (see cx_buffer_s.flags)
121 * @return zero on success, non-zero if a required allocation failed 121 * @return zero on success, non-zero if a required allocation failed
122 */ 122 */
123 __attribute__((__nonnull__(1, 4)))
123 int cxBufferInit( 124 int cxBufferInit(
124 CxBuffer *buffer, 125 CxBuffer *buffer,
125 void *space, 126 void *space,
126 size_t capacity, 127 size_t capacity,
127 CxAllocator *allocator, 128 CxAllocator const *allocator,
128 int flags 129 int flags
129 ); 130 );
130 131
131 /** 132 /**
132 * Destroys the buffer contents. 133 * Destroys the buffer contents.
133 * 134 *
134 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled. 135 * Has no effect if the #CX_BUFFER_FREE_CONTENTS feature is not enabled.
135 * 136 *
136 * @param buffer the buffer which contents shall be destroyed 137 * @param buffer the buffer which contents shall be destroyed
137 */ 138 */
139 __attribute__((__nonnull__))
138 void cxBufferDestroy(CxBuffer *buffer); 140 void cxBufferDestroy(CxBuffer *buffer);
139
140 /**
141 * Creates a new buffer and fills it with content copied from another buffer.
142 *
143 * \note The #CX_BUFFER_FREE_CONTENTS feature is enforced for the new buffer.
144 *
145 * @param src the source buffer
146 * @param start the start position of extraction
147 * @param length the count of bytes to extract (must not be zero)
148 * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
149 * @return a new buffer containing the extraction
150 */
151 CxBuffer *cxBufferExtract(
152 CxBuffer *src,
153 size_t start,
154 size_t length,
155 int flags
156 );
157
158 /**
159 * A shorthand macro for copying an entire buffer.
160 *
161 * @param src the source buffer
162 * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
163 * @return a new buffer with the copied content
164 */
165 #define cxBufferClone(src, flags) cxBufferExtract(src, 0, (src)->capacity, flags)
166
167 141
168 /** 142 /**
169 * Shifts the contents of the buffer by the given offset. 143 * Shifts the contents of the buffer by the given offset.
170 * 144 *
171 * If the offset is positive, the contents are shifted to the right. 145 * If the offset is positive, the contents are shifted to the right.
196 * 170 *
197 * @param buffer the buffer 171 * @param buffer the buffer
198 * @param shift the shift offset (negative means left shift) 172 * @param shift the shift offset (negative means left shift)
199 * @return 0 on success, non-zero if a required auto-extension fails 173 * @return 0 on success, non-zero if a required auto-extension fails
200 */ 174 */
175 __attribute__((__nonnull__))
201 int cxBufferShift( 176 int cxBufferShift(
202 CxBuffer *buffer, 177 CxBuffer *buffer,
203 off_t shift 178 off_t shift
204 ); 179 );
205 180
210 * @param buffer the buffer 185 * @param buffer the buffer
211 * @param shift the shift offset 186 * @param shift the shift offset
212 * @return 0 on success, non-zero if a required auto-extension fails 187 * @return 0 on success, non-zero if a required auto-extension fails
213 * @see cxBufferShift() 188 * @see cxBufferShift()
214 */ 189 */
190 __attribute__((__nonnull__))
215 int cxBufferShiftRight( 191 int cxBufferShiftRight(
216 CxBuffer *buffer, 192 CxBuffer *buffer,
217 size_t shift 193 size_t shift
218 ); 194 );
219 195
227 * @param buffer the buffer 203 * @param buffer the buffer
228 * @param shift the positive shift offset 204 * @param shift the positive shift offset
229 * @return always zero 205 * @return always zero
230 * @see cxBufferShift() 206 * @see cxBufferShift()
231 */ 207 */
208 __attribute__((__nonnull__))
232 int cxBufferShiftLeft( 209 int cxBufferShiftLeft(
233 CxBuffer *buffer, 210 CxBuffer *buffer,
234 size_t shift 211 size_t shift
235 ); 212 );
236 213
252 * @param offset position offset relative to \p whence 229 * @param offset position offset relative to \p whence
253 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END 230 * @param whence one of \c SEEK_SET, \c SEEK_CUR or \c SEEK_END
254 * @return 0 on success, non-zero if the position is invalid 231 * @return 0 on success, non-zero if the position is invalid
255 * 232 *
256 */ 233 */
234 __attribute__((__nonnull__))
257 int cxBufferSeek( 235 int cxBufferSeek(
258 CxBuffer *buffer, 236 CxBuffer *buffer,
259 off_t offset, 237 off_t offset,
260 int whence 238 int whence
261 ); 239 );
265 * 243 *
266 * The data is deleted by zeroing it with a call to memset(). 244 * The data is deleted by zeroing it with a call to memset().
267 * 245 *
268 * @param buffer the buffer to be cleared 246 * @param buffer the buffer to be cleared
269 */ 247 */
270 #define cxBufferClear(buffer) memset((buffer)->bytes, 0, (buffer)->size); \ 248 __attribute__((__nonnull__))
271 (buffer)->size = 0; (buffer)->pos = 0; 249 void cxBufferClear(CxBuffer *buffer);
272 250
273 /** 251 /**
274 * Tests, if the buffer position has exceeded the buffer capacity. 252 * Tests, if the buffer position has exceeded the buffer capacity.
275 * 253 *
276 * @param buffer the buffer to test 254 * @param buffer the buffer to test
277 * @return non-zero, if the current buffer position has exceeded the last 255 * @return non-zero, if the current buffer position has exceeded the last
278 * available byte of the buffer. 256 * available byte of the buffer.
279 */ 257 */
280 int cxBufferEof(CxBuffer *buffer); 258 __attribute__((__nonnull__))
259 int cxBufferEof(CxBuffer const *buffer);
281 260
282 261
283 /** 262 /**
284 * Ensures that the buffer has a minimum capacity. 263 * Ensures that the buffer has a minimum capacity.
285 * 264 *
287 * 266 *
288 * @param buffer the buffer 267 * @param buffer the buffer
289 * @param capacity the minimum required capacity for this buffer 268 * @param capacity the minimum required capacity for this buffer
290 * @return 0 on success or a non-zero value on failure 269 * @return 0 on success or a non-zero value on failure
291 */ 270 */
271 __attribute__((__nonnull__))
292 int cxBufferMinimumCapacity( 272 int cxBufferMinimumCapacity(
293 CxBuffer *buffer, 273 CxBuffer *buffer,
294 size_t capacity 274 size_t capacity
295 ); 275 );
296 276
305 * @param size the length of one element 285 * @param size the length of one element
306 * @param nitems the element count 286 * @param nitems the element count
307 * @param buffer the CxBuffer to write to 287 * @param buffer the CxBuffer to write to
308 * @return the total count of bytes written 288 * @return the total count of bytes written
309 */ 289 */
290 __attribute__((__nonnull__))
310 size_t cxBufferWrite( 291 size_t cxBufferWrite(
311 void const *ptr, 292 void const *ptr,
312 size_t size, 293 size_t size,
313 size_t nitems, 294 size_t nitems,
314 CxBuffer *buffer 295 CxBuffer *buffer
325 * @param size the length of one element 306 * @param size the length of one element
326 * @param nitems the element count 307 * @param nitems the element count
327 * @param buffer the CxBuffer to read from 308 * @param buffer the CxBuffer to read from
328 * @return the total number of elements read 309 * @return the total number of elements read
329 */ 310 */
311 __attribute__((__nonnull__))
330 size_t cxBufferRead( 312 size_t cxBufferRead(
331 void *ptr, 313 void *ptr,
332 size_t size, 314 size_t size,
333 size_t nitems, 315 size_t nitems,
334 CxBuffer *buffer 316 CxBuffer *buffer
347 * @param buffer the buffer to write to 329 * @param buffer the buffer to write to
348 * @param c the character to write 330 * @param c the character to write
349 * @return the byte that has bean written or \c EOF when the end of the stream is 331 * @return the byte that has bean written or \c EOF when the end of the stream is
350 * reached and automatic extension is not enabled or not possible 332 * reached and automatic extension is not enabled or not possible
351 */ 333 */
334 __attribute__((__nonnull__))
352 int cxBufferPut( 335 int cxBufferPut(
353 CxBuffer *buffer, 336 CxBuffer *buffer,
354 int c 337 int c
338 );
339
340 /**
341 * Writes a string to a buffer.
342 *
343 * @param buffer the buffer
344 * @param str the zero-terminated string
345 * @return the number of bytes written
346 */
347 __attribute__((__nonnull__))
348 size_t cxBufferPutString(
349 CxBuffer *buffer,
350 const char *str
355 ); 351 );
356 352
357 /** 353 /**
358 * Gets a character from a buffer. 354 * Gets a character from a buffer.
359 * 355 *
360 * The current position of the buffer is increased after a successful read. 356 * The current position of the buffer is increased after a successful read.
361 * 357 *
362 * @param buffer the buffer to read from 358 * @param buffer the buffer to read from
363 * @return the character or \c EOF, if the end of the buffer is reached 359 * @return the character or \c EOF, if the end of the buffer is reached
364 */ 360 */
361 __attribute__((__nonnull__))
365 int cxBufferGet(CxBuffer *buffer); 362 int cxBufferGet(CxBuffer *buffer);
366
367 /**
368 * Writes a string to a buffer.
369 *
370 * @param buffer the buffer
371 * @param str the zero-terminated string
372 * @return the number of bytes written
373 */
374 size_t cxBufferPutString(
375 CxBuffer *buffer,
376 const char *str
377 );
378 363
379 #ifdef __cplusplus 364 #ifdef __cplusplus
380 } 365 }
381 #endif 366 #endif
382 367

mercurial