*oops* I forgot to add those.
[apps/madmutt.git] / lib-lib / array.c
diff --git a/lib-lib/array.c b/lib-lib/array.c
new file mode 100644 (file)
index 0000000..c497462
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2006 Pierre Habouzit
+ */
+
+#include "lib-lib.h"
+
+void *__array_take(__array *a, ssize_t pos)
+{
+    void *ptr;
+
+    if (pos >= a->len || pos < 0) {
+        return NULL;
+    }
+
+    ptr = a->arr[pos];
+    a->len--;
+    memmove(a->arr + pos, a->arr + pos + 1, (a->len - pos) * sizeof(void*));
+    return ptr;
+}
+
+#define SIZE_AUGMENT  32
+
+void __array_insert(__array *a, ssize_t pos, void *item)
+{
+    pos = MAX(0, MIN(a->len, pos));
+
+    if (a->len >= a->size) {
+        p_realloc(&a->arr, a->size + SIZE_AUGMENT);
+        a->size += SIZE_AUGMENT;
+    }
+
+    memmove(a->arr + pos + 1, a->arr + pos, (a->len - pos) * sizeof(void *));
+    a->arr[pos] = item;
+    a->len++;
+}