view buffer.h @ 44:295e76f7bd28

For parray, added a better PARRAY_INDEX that evaluate to the following: if (index < 0 || arr->length == 0) data = a->datas[0]; else if (index >= arr->length) data = a->datas[arr->length - 1]; else data = a->datas[index];
author David Demelier <markand@malikania.fr>
date Sun, 02 Oct 2011 19:57:07 +0200
parents 5252fa9b5cb1
children fea13579acbe
line wrap: on
line source

/*
 * buffer.h -- safe unlimited size string
 *
 * Copyright (c) 2011, David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _BUFFER_H_
#define _BUFFER_H_

#define	BUFFER_DEFAULT_BSIZE	512

#define BUFFER_FIXED	0x00
#define BUFFER_AUTO	0x01

#include <stdarg.h>

struct buffer {
	char	*data;		/* string buffer */
	size_t	length;		/* string's length */

	int	flags;		/* buffer's flags (default AUTO) */

	/* Private should not be modified by user */
	size_t	size;		/* current size */
	size_t	bsize;		/* block size */
};

struct buffer	*buffer_new(const char *, size_t, int);
#define buffer_new_auto(size)	buffer_new(NULL, size, BUFFER_AUTO)
#define buffer_new_fixed(max)	buffer_new(NULL, max, BUFFER_FIXED)

int	buffer_strcat(struct buffer *, const char *);
int	buffer_putc(struct buffer *buf, char);
int	buffer_bcat(struct buffer *, const void *, size_t);
int	buffer_vprintf(struct buffer *, const char *, va_list);
int	buffer_printf(struct buffer *, const char *, ...);
int	buffer_shrink(struct buffer *);
char	*buffer_end(struct buffer *);
void	buffer_clear(struct buffer *);
void	buffer_free(struct buffer *);

#endif /* _BUFFER_H_ */