diff C/buf.h @ 186:d4b8416e9ab1

Move C
author David Demelier <markand@malikania.fr>
date Sat, 23 Nov 2013 16:14:05 +0100
parents buf.h@7f214f26a4c0
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C/buf.h	Sat Nov 23 16:14:05 2013 +0100
@@ -0,0 +1,88 @@
+/*
+ * 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_
+
+#include <stdarg.h>
+
+#ifdef __GNUC__
+#  define _buf_at_printf(i1, i2)	__attribute__ ((format (printf, i1, i2)))
+#else
+#  define _buf_at_printf(i1, i2)
+#endif
+
+enum buf_flags {
+	BUF_UNSAFE	= (1 << 0),	/* string may be truncated */
+	BUF_FIXED	= (1 << 1),	/* string has fixed length */
+};
+
+struct buf {
+	enum buf_flags	flags;		/* string flags */
+	char		*text;		/* string text */
+	size_t		length;		/* string length */
+	size_t		alsize;		/* allocated size */
+	int		chksize;	/* chunk size */
+	int		maxsize;	/* max fixed length size */
+
+	void * (*malloc)(size_t);		/* alternate malloc */
+	void * (*realloc)(void *, size_t);	/* alternate realloc */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int
+buf_init(struct buf *, const char *);
+
+void
+buf_set(struct buf *, const char *, ...);
+
+int
+buf_ncat(struct buf *, const char *, size_t);
+
+int
+buf_cat(struct buf *, const char *);
+
+int
+buf_putc(struct buf *, int);
+
+int
+buf_vprintf(struct buf *, const char *, va_list);
+
+int
+buf_printf(struct buf *, const char *, ...)	_buf_at_printf(2, 3);
+
+int
+buf_trim(struct buf *);
+
+void
+buf_cut(struct buf *, int);
+
+void
+buf_clear(struct buf *);
+
+void
+buf_free(struct buf *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _BUF_H_ */