view buffer.h @ 1:22d7bb03e569

for buffer: o added a new function buffer_putc(struct buffer *, char) that append the character to the end of the buffer o fix buffer_printf on BUFFER_AUTO
author David Demelier <markand@malikania.fr>
date Tue, 06 Sep 2011 17:43:59 +0200
parents a20beb164de8
children 86c845f86868
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 HAVE_BUFFER_H
#define HAVE_BUFFER_H

#define	BUFFER_DEFAULT_BSIZE	512

#include <stdarg.h>

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

#define BUFFER_AUTO	0x00000001
#define BUFFER_FIXED	0x00000010
	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 /* HAVE_BUFFER_H */