--- a/ucx/ucx/buffer.h Mon Feb 04 14:11:57 2019 +0100 +++ b/ucx/ucx/buffer.h Mon Feb 04 17:17:48 2019 +0100 @@ -137,6 +137,67 @@ #define ucx_buffer_clone(src,flags) \ ucx_buffer_extract(src, 0, (src)->capacity, flags) + +/** + * Shifts the contents of the buffer by the given offset. + * + * If the offset is positive, the contents are shifted to the right. + * If auto extension is enabled, the buffer grows, if necessary. + * In case the auto extension fails, this function returns a non-zero value and + * no contents are changed. + * If auto extension is disabled, the contents that do not fit into the buffer + * are discarded. + * + * If the offset is negative, the contents are shifted to the left where the + * first <code>shift</code> bytes are discarded. + * The new size of the buffer is the old size minus + * the absolute shift value. + * If this value is larger than the buffer size, the buffer is emptied (but + * not cleared, see the security note below). + * + * The buffer position gets shifted alongside with the content but is kept + * within the boundaries of the buffer. + * + * <b>Security note:</b> the shifting operation does <em>not</em> erase the + * previously occupied memory cells. You can easily do that manually, e.g. by + * calling <code>memset(buffer->space, 0, shift)</code> for a right shift or + * <code>memset(buffer->size, 0, buffer->capacity-buffer->size)</code> + * for a left shift. + * + * @param buffer the buffer + * @param shift the shift offset (negative means left shift) + * @return 0 on success, non-zero if a required auto-extension fails + */ +int ucx_buffer_shift(UcxBuffer* buffer, off_t shift); + +/** + * Shifts the buffer to the right. + * See ucx_buffer_shift() for details. + * + * @param buffer the buffer + * @param shift the shift offset + * @return 0 on success, non-zero if a required auto-extension fails + * @see ucx_buffer_shift() + */ +int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift); + +/** + * Shifts the buffer to the left. + * + * See ucx_buffer_shift() for details. Note, however, that this method expects + * a positive shift offset. + * + * Since a left shift cannot fail due to memory allocation problems, this + * function always returns zero. + * + * @param buffer the buffer + * @param shift the shift offset + * @return always zero + * @see ucx_buffer_shift() + */ +int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift); + + /** * Moves the position of the buffer. * @@ -165,8 +226,8 @@ * * @param buffer the buffer to be cleared */ -#define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ - buffer->size = 0; buffer->pos = 0; +#define ucx_buffer_clear(buffer) memset((buffer)->space, 0, (buffer)->size); \ + (buffer)->size = 0; (buffer)->pos = 0; /** * Tests, if the buffer position has exceeded the buffer capacity. @@ -260,7 +321,15 @@ * @param str the string * @return the number of bytes written */ -size_t ucx_buffer_puts(UcxBuffer *buffer, char *str); +size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str); + +/** + * Returns the complete buffer content as sstr_t. + * @param buffer the buffer + * @return the result of <code>sstrn()</code> with the buffer space and size + * as arguments + */ +#define ucx_buffer_to_sstr(buffer) sstrn((buffer)->space, (buffer)->size) #ifdef __cplusplus }