changeset 143:0cf53c588a83

Merge
author David Demelier <markand@malikania.fr>
date Fri, 11 May 2012 00:10:37 +0200
parents d62f2f657768 (current diff) e3cf5ac9a5aa (diff)
children 594ca7f7139b
files ini.h
diffstat 8 files changed, 54 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Fri May 11 00:09:30 2012 +0200
+++ b/array.c	Fri May 11 00:10:37 2012 +0200
@@ -86,7 +86,7 @@
 /*
  * Add to the head of array. NOTE: this may be very slow when adding a lot
  * of object (about 100000). If you need to add a lot of data please consider
- * using linked list instead.
+ * using linked list instead. Returns -1 on failure or 0 on success.
  */
 
 int
@@ -103,7 +103,7 @@
 
 /*
  * Insert the data at the specified index. The function returns -1 on
- * allocation failure or when the index is outof bounds otherwise 0 is returned.
+ * allocation failure or the position of the added element.
  */
 
 int
@@ -119,11 +119,12 @@
 	    (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index));
 	memcpy((char *)arr->data + OFFSET(index), data, arr->unit);
 
-	return 0;
+	return index;
 }
 
 /*
- * Append the data to the end of array.
+ * Append the data to the end of array. Returns -1 on failure or the position
+ * of the added element.
  */
 
 int
@@ -134,7 +135,7 @@
 
 	memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit);
 
-	return 0;
+	return (arr->length - 1);
 }
 
 /*
@@ -267,7 +268,7 @@
  */
 
 void
-array_map(const struct array *arr, array_map_fn fn, void *udata)
+array_map(const struct array *arr, array_map_t fn, void *udata)
 {
 	int i;
 
@@ -282,7 +283,7 @@
  */
 
 int
-array_find(const struct array *arr, array_cmp_fn fn, void *dst, void *u)
+array_find(const struct array *arr, array_cmp_t fn, void *dst, void *u)
 {
 	int st, i;
 
--- a/array.h	Fri May 11 00:09:30 2012 +0200
+++ b/array.h	Fri May 11 00:10:37 2012 +0200
@@ -49,8 +49,8 @@
 	void * (*realloc)(void *, size_t);
 };
 
-typedef void (*array_map_fn)(void *, void *);
-typedef int (*array_cmp_fn)(void *, void *);
+typedef void (*array_map_t)(void *, void *);
+typedef int (*array_cmp_t)(void *, void *);
 
 int
 array_init(struct array *, size_t);
@@ -86,10 +86,10 @@
 array_pswap(struct array *, const void *, const void *);
 
 void
-array_map(const struct array *, array_map_fn, void *);
+array_map(const struct array *, array_map_t, void *);
 
 int
-array_find(const struct array *, array_cmp_fn, void *, void *);
+array_find(const struct array *, array_cmp_t, void *, void *);
 
 void *
 array_first(const struct array *);
--- a/buf.c	Fri May 11 00:09:30 2012 +0200
+++ b/buf.c	Fri May 11 00:10:37 2012 +0200
@@ -130,7 +130,9 @@
 int
 buf_putc(struct buf *buf, int c)
 {
-	return buf_printf(buf, "%c", c);
+	char str[2] = { c, '\0' };
+
+	return buf_ncat(buf, str, 1);
 }
 
 /*
@@ -204,6 +206,28 @@
 }
 
 /*
+ * Remove `n' characters from the buffer, a positive value will cut the string
+ * from beginning, negative value will cut from end.
+ */
+
+void
+buf_cut(struct buf *buf, int n)
+{
+	int pos;
+
+	if (n < 0 || (unsigned int)n >= buf->length)
+		return;
+
+	if (n < 0 && buf->length - (-n) > 0)
+		pos = buf->length - (-n);
+	else if ((unsigned int)n < buf->length)
+		pos = n;
+
+	buf->text[pos]	= '\0';
+	buf->length	-= buf->length - pos;
+}
+
+/*
  * Clear the string buffer.
  */
 
--- a/buf.h	Fri May 11 00:09:30 2012 +0200
+++ b/buf.h	Fri May 11 00:10:37 2012 +0200
@@ -31,10 +31,6 @@
 
 #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
@@ -82,15 +78,14 @@
 buf_trim(struct buf *);
 
 void
+buf_cut(struct buf *, int);
+
+void
 buf_clear(struct buf *);
 
 void
 buf_free(struct buf *);
 
-#if defined(__GNUC__) && __GNUC_MINOR__ > 2
-#  pragma GCC diagnostic pop
-#endif
-
 #ifdef __cplusplus
 }
 #endif
--- a/ini.h	Fri May 11 00:09:30 2012 +0200
+++ b/ini.h	Fri May 11 00:10:37 2012 +0200
@@ -124,52 +124,6 @@
 void
 ini_free(struct ini_config *, int);
 
-#if 0
-
-/* --------------------------------------------------------
- * Convenient api to query and convert data
- * -------------------------------------------------------- */
-
-/*
- * For the config, read all available values and store them in
- * the array ini_handler.
- */
-
-void
-ini_dispatch(INI_Section *, INI_Handler *, int);
-
-/*
- * Convert to bool. dst must be (char *).
- * It converts "yes" "true" "1" or opposites. Only lower
- * case is supported right now.
- */
-
-void
-ini_convert_bool(void *, const char *, void *);
-
-/*
- * Convert to an int. dst must be (int *).
- */
-
-void
-ini_convert_int(void *, const char *, void *);
-
-/*
- * Convert to a short. dst must be (short *).
- */
-
-void
-ini_convert_short(void *, const char *, void *);
-
-/*
- * Convert to a char *. dst must be (char **). This
- * function uses strdup(). You need to free the dst
- * pointer.
- */
-
-void
-ini_convert_string(void *, const char *, void *);
-
 #endif
 
 #ifdef __cplusplus
--- a/pack.c	Fri May 11 00:09:30 2012 +0200
+++ b/pack.c	Fri May 11 00:10:37 2012 +0200
@@ -394,7 +394,8 @@
 			continue;
 
 		for (i = 0; i < nelem; ++i) {
-			fread(&ptr[tocopy * i], tocopy, 1, fp);
+			if (fread(&ptr[tocopy * i], tocopy, 1, fp) <= 0)
+				return -1;
 
 			/* Convert if needed */
 			convert = pack_getconvert_by_tok(tok);
--- a/parray.c	Fri May 11 00:09:30 2012 +0200
+++ b/parray.c	Fri May 11 00:10:37 2012 +0200
@@ -82,7 +82,7 @@
 /*
  * Add to the head of array. NOTE: this may be very slow when adding a lot
  * of object (about 100000). If you need to add a lot of data please consider
- * using linked list instead.
+ * using linked list instead. Returns -1 on failure or 0 on success.
  */
 
 int
@@ -102,7 +102,7 @@
 
 /*
  * Insert the data at the specified index. The function returns -1 on
- * allocation failure.
+ * allocation failure or the position of the added element.
  */
 
 int
@@ -121,11 +121,12 @@
 	if (arr->flags & PARRAY_NULLEND)
 		arr->data[arr->length] = NULL;
 
-	return 0;
+	return index;
 }
 
 /*
- * Append the data to the end of array.
+ * Append the data to the end of array. Returns -1 on failure or the position
+ * of the added element.
  */
 
 int
@@ -139,7 +140,7 @@
 	if (arr->flags & PARRAY_NULLEND)
 		arr->data[arr->length] = NULL;
 
-	return 0;
+	return (arr->length - 1);
 }
 
 /*
@@ -253,7 +254,7 @@
  */
 
 void
-parray_map(const struct parray *arr, parray_map_fn fn, void *udata)
+parray_map(const struct parray *arr, parray_map_t fn, void *udata)
 {
 	int i;
 
@@ -269,7 +270,7 @@
  */
 
 int
-parray_find(const struct parray *arr, parray_cmp_fn fn, void *ptr, void *u)
+parray_find(const struct parray *arr, parray_cmp_t fn, void *ptr, void *u)
 {
 	int st, i;
 
--- a/parray.h	Fri May 11 00:09:30 2012 +0200
+++ b/parray.h	Fri May 11 00:10:37 2012 +0200
@@ -48,8 +48,8 @@
 	void * (*realloc)(void *, size_t);
 };
 
-typedef void (*parray_map_fn)(void *, void *);
-typedef int (*parray_cmp_fn)(void *, void *);
+typedef void (*parray_map_t)(void *, void *);
+typedef int (*parray_cmp_t)(void *, void *);
 
 int
 parray_init(struct parray *);
@@ -85,10 +85,10 @@
 parray_pswap(struct parray *, const void *, const void *);
 
 void
-parray_map(const struct parray *, parray_map_fn, void *);
+parray_map(const struct parray *, parray_map_t, void *);
 
 int
-parray_find(const struct parray *, parray_cmp_fn, void *, void *);
+parray_find(const struct parray *, parray_cmp_t, void *, void *);
 
 void *
 parray_first(const struct parray *);