--- /dev/null
+/*
+ * 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++;
+}
--- /dev/null
+/*
+ * 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
+ */
+
+#ifndef MUTT_LIB_LIB_ARRAY_H
+#define MUTT_LIB_LIB_ARRAY_H
+
+#define DO_ARRAY_TYPE(type, prefix) \
+ typedef struct prefix##_array { \
+ type **arr; \
+ ssize_t len; \
+ ssize_t size; \
+ } prefix##_array
+
+#define DO_ARRAY_FUNCS(type, prefix, dtor) \
+ \
+ static inline void prefix##_array_init(prefix##_array *array) { \
+ p_clear(array, 1); \
+ } \
+ static inline void \
+ prefix##_array_wipe(prefix##_array *array) { \
+ while (array->len) { \
+ (dtor)(&array->arr[--array->len]); \
+ } \
+ p_delete(&array->arr); \
+ } \
+ \
+ static inline void \
+ prefix##_array_insert(prefix##_array *array, ssize_t pos, type *item) { \
+ __array_insert((__array *)array, pos, (void*)item); \
+ } \
+ static inline void \
+ prefix##_array_append(prefix##_array *array, type *item) { \
+ __array_insert((__array *)array, array->len, (void*)item); \
+ } \
+ static inline type * \
+ prefix##_array_take(prefix##_array *array, ssize_t pos) { \
+ return (type *)__array_take((__array *)array, pos); \
+ }
+
+DO_ARRAY_TYPE(void, _);
+void __array_insert(__array *array, ssize_t pos, void *item);
+void *__array_take(__array *array, ssize_t pos);
+
+#endif /* MUTT_LIB_LIB_ARRAY_H */