changeset 133:d85dd98cb49d

Add attributes
author David Demelier <markand@malikania.fr>
date Mon, 12 Mar 2012 21:46:45 +0100
parents bdd511d1a556
children 195cdc5f4a32
files buf.c buf.h
diffstat 2 files changed, 97 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/buf.c	Mon Mar 12 21:46:39 2012 +0100
+++ b/buf.c	Mon Mar 12 21:46:45 2012 +0100
@@ -48,6 +48,42 @@
 }
 
 /*
+ * Valid options that can be set for a buffer :
+ * l -> optional buf block size of type int
+ * m -> malloc function that must matches void * (*malloc)(size_t)
+ * r -> realloc function that must matches void * (*realloc)(void *, size_t)
+ * f -> buffer flags that are OR'ed
+ */
+
+void
+buf_set(struct buf *buf, const char *fmt, ...)
+{
+	va_list ap;
+	const char *p;
+
+	va_start(ap, fmt);
+	for (p = fmt; *p != '\0'; ++p)
+		switch (*p) {
+		case 'l':
+			buf->bsize = va_arg(ap, int);
+			break;
+		case 'm':
+			buf->malloc = va_arg(ap, void *(*)(size_t));
+			break;
+		case 'r':
+			buf->realloc = va_arg(ap, void *(*)(void *, size_t));
+			break;
+		case 'f':
+		case 't':
+			buf->flags = va_arg(ap, int);
+			break;
+		default:
+			break;
+		}
+}
+
+
+/*
  * This function appends not more than max characters from str.
  */
 
@@ -89,6 +125,16 @@
 }
 
 /*
+ * Append the caracter c to the end of buffer
+ */
+
+int
+buf_putc(struct buf *buf, int c)
+{
+	return buf_printf(buf, "%c", c);
+}
+
+/*
  * Concatenate the printf(3) like call to the string buffer, this function
  * returns -1 on fixed safe buffer, otherwise 0 is returned if there
  * is no allocation failure.
@@ -142,23 +188,20 @@
 }
 
 /*
- * Append the caracter c to the end of buffer
+ * Realloc the string to it's size and remove useless bytes.
  */
 
 int
-buf_putc(struct buf *buf, int c)
+buf_trim(struct buf *buf)
 {
-	return buf_printf(buf, "%c", c);
-}
+	if ((buf->text = realloc(buf->text, buf->length + 1)) == NULL) {
+		buf->alsize = 0;
+		return -1;
+	}
 
-/*
- * Just an alias for buf_cat.
- */
+	buf->alsize = buf->length + 1;
 
-int
-buf_puts(struct buf *buf, const char *str)
-{
-	return buf_cat(buf, str);
+	return 0;
 }
 
 /*
@@ -176,45 +219,13 @@
 void
 buf_free(struct buf *buf)
 {
-	buf_clear(buf);
-
-	free(buf->text);
-	buf->text = NULL;
-}
-
-/*
- * Valid options that can be set for a buffer :
- * l -> optional buf block size of type int
- * m -> malloc function that must matches void * (*malloc)(size_t)
- * r -> realloc function that must matches void * (*realloc)(void *, size_t)
- * f -> buffer flags that are OR'ed
- */
-
-void
-buf_set(struct buf *buf, const char *fmt, ...)
-{
-	va_list ap;
-	const char *p;
+	if (buf != NULL) {
+		buf_clear(buf);
+		free(buf->text);
 
-	va_start(ap, fmt);
-	for (p = fmt; *p != '\0'; ++p)
-		switch (*p) {
-		case 'l':
-			buf->bsize = va_arg(ap, int);
-			break;
-		case 'm':
-			buf->malloc = va_arg(ap, void *(*)(size_t));
-			break;
-		case 'r':
-			buf->realloc = va_arg(ap, void *(*)(void *, size_t));
-			break;
-		case 'f':
-		case 't':
-			buf->flags = va_arg(ap, int);
-			break;
-		default:
-			break;
-		}
+		buf->text = NULL;
+		buf->alsize = 0;
+	}
 }
 
 /*
--- a/buf.h	Mon Mar 12 21:46:39 2012 +0100
+++ b/buf.h	Mon Mar 12 21:46:45 2012 +0100
@@ -31,6 +31,10 @@
 
 #ifdef __GNUC__
 #  define _buf_at_printf(i1, i2)	__attribute__ ((format (printf, i1, i2)))
+#    if __GNUC_MINOR__ > 2
+#      pragma GCC diagnostic push
+#      pragma GCC diagnostic warning "-Wformat"
+#    endif
 #else
 #  define _buf_at_printf(i1, i2)
 #endif
@@ -53,15 +57,39 @@
 	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 *, ...) _buf_at_printf(2, 3);
-int	buf_putc(struct buf *, int);
-void	buf_clear(struct buf *);
-void	buf_free(struct buf *);
+int
+buf_init(struct buf *);
+
+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_clear(struct buf *);
+
+void
+buf_free(struct buf *);
+
+#if defined(__GNUC__) && __GNUC_MINOR__ > 2
+#  pragma GCC diagnostic pop
+#endif
 
 #ifdef __cplusplus
 }