view buffer.h @ 63:d78c4d9fad13

Move to port/ code that I didn't write
author David Demelier <markand@malikania.fr>
date Wed, 09 Nov 2011 19:30:33 +0100
parents cda80ba48029
children f773c76b1f3c
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

#include <stdarg.h>

/*
 * Some useful attributes with gcc or clang.
 */

#if defined(__GNUC__)
# pragma GCC diagnostic warning	"-Wnonnull"
# pragma GCC diagnostic warning	"-Wformat"
# define __at_malloc		__attribute__ ((malloc))
# define __at_nonnull		__attribute__ ((nonnull(1)))
# define __at_printf(...)	__attribute__ ((format(printf, __VA_ARGS__)))
#else
# define __at_malloc
# define __at_nonnull
# define __at_printf(...)
#endif

enum buffer_type {
	BUFFER_FIXED	= 0,
	BUFFER_AUTO	= 1
};

struct buffer {
	enum buffer_type type;		/* buffer type (default AUTO) */
	char		*text;		/* string buffer */
	size_t		length;		/* string's real length */
	size_t		size;		/* current allocated size */
	int		bsize;		/* initial length */
};

struct buffer	*buffer_new(enum buffer_type, int) __at_malloc;
int		buffer_strcat(struct buffer *, const char *) __at_nonnull;
int		buffer_putc(struct buffer *buf, char) __at_nonnull;
int		buffer_bcat(struct buffer *, const void *, size_t) __at_nonnull;
int		buffer_vprintf(struct buffer *, const char *, va_list) __at_nonnull;
int		buffer_printf(struct buffer *, const char *, ...) __at_printf(2, 3);
int		buffer_shrink(struct buffer *) __at_nonnull;
char		*buffer_end(struct buffer *) __at_nonnull;
void		buffer_clear(struct buffer *) __at_nonnull;
void		buffer_free(struct buffer *) __at_nonnull;

#endif /* _BUFFER_H_ */