changeset 142:e3cf5ac9a5aa

[p]array_insert now returns -1 or index of added element
author David Demelier <markand@malikania.fr>
date Tue, 08 May 2012 15:02:17 +0200
parents 5ed54050ae31
children 0cf53c588a83
files array.c array.h ini.h parray.c parray.h
diffstat 5 files changed, 40 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Wed May 02 20:20:22 2012 +0200
+++ b/array.c	Tue May 08 15:02:17 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	Wed May 02 20:20:22 2012 +0200
+++ b/array.h	Tue May 08 15:02:17 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/ini.h	Wed May 02 20:20:22 2012 +0200
+++ b/ini.h	Tue May 08 15:02:17 2012 +0200
@@ -34,6 +34,20 @@
 
 typedef void (*INI_ConvertFunc)(void *, const char *, void *);
 
+#if !defined(LIST_ENTRY)
+#define LIST_ENTRY(type)						\
+struct {								\
+	struct type *le_next;						\
+	struct type **le_prev;						\
+}
+#endif
+
+#if !define(LIST_HEAD)
+#define LIST_HEAD(name, type)						\
+struct name {								\
+	struct type *lh_first;
+}
+
 /* --------------------------------------------------------
  * Structure definitions
  * -------------------------------------------------------- */
@@ -61,7 +75,8 @@
 struct ini_option {
 	char		*key;		/* (rw) option name */
 	char		*value;		/* (rw) option value */
-	INI_Option	*next;		/* (ro) next option */
+
+	LIST_ENTRY(ini_option) link;	/* (rw) link entry */
 };
 
 struct ini_section {
--- a/parray.c	Wed May 02 20:20:22 2012 +0200
+++ b/parray.c	Tue May 08 15:02:17 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	Wed May 02 20:20:22 2012 +0200
+++ b/parray.h	Tue May 08 15:02:17 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 *);