view buffer.h @ 69:8f6d3850fac1

Remove useless attributes and add [P]ARRAY_FULL
author David Demelier <markand@malikania.fr>
date Fri, 11 Nov 2011 02:21:59 +0100
parents f773c76b1f3c
children 580d76781c9e
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_

#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * 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

#define	BUFFER_DEFAULT_BSIZE	512

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;

#ifdef __cplusplus
}
#endif

#endif /* _BUFFER_H_ */