view buf.h @ 129:2f71e113be63

Remove copy and ncopy and putc/puts
author David Demelier <markand@malikania.fr>
date Thu, 08 Mar 2012 23:48:36 +0100
parents e00bc8f7eabf
children cbfad4fb80c8
line wrap: on
line source

/*
 * buf.h -- easy way to manipulate strings
 *
 * Copyright (c) 2011, 2012, 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 _BUF_H_
#define _BUF_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>

#ifndef BUF_DEFAULT_BSIZE
#define BUF_DEFAULT_BSIZE	128
#endif

enum {
	BUF_AUTO	= 0,		/* string grows automatically */
	BUF_FIXED	= (1 << 0),	/* fixed size string */
	BUF_UNSAFE	= (1 << 1),	/* string may be truncated */
};

struct buf {
	int		flags;	/* string flags */
	char		*text;	/* string text */
	size_t		length;	/* string length */
	size_t		alsize;	/* allocated size */
	int		bsize;	/* block size (used when growing array) */

	/* Own allocation functions */
	void * (*malloc)(size_t);
	void * (*realloc)(void *, size_t);
};

int	buf_init(struct buf *);
void	buf_set(struct buf *, const char *, ...);
int	buf_ncat(struct buf *, const char *, size_t);
int	buf_copy(struct buf *, const char *);
int	buf_vprintf(struct buf *, const char *, va_list);
int	buf_printf(struct buf *, const char *, ...);
int	buf_putc(struct buf *, int);
int	buf_puts(struct buf *, const char *);
void	buf_clear(struct buf *);
void	buf_free(struct buf *);

#ifdef __cplusplus
}
#endif

#endif /* _BUF_H_ */