#include <stddef.h>
#include <string.h>
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+
#include "list.h"
-#include "mem.h"
list2_t* list_new (void) {
- return (mem_calloc (1, sizeof (list2_t)));
+ return p_new(list2_t, 1);
}
void list_del (list2_t** l, list_del_t* del) {
- size_t i = 0;
+ ssize_t i = 0;
if (!l || !*l)
return;
if (del)
for (i = 0; i < (*l)->length; i++)
del (&(*l)->data[i]);
- mem_free(&(*l)->data);
- mem_free(l);
+ p_delete(&(*l)->data);
+ p_delete(l);
}
void list_push_back (list2_t** l, void* p) {
if (!*l)
*l = list_new ();
- mem_realloc (&(*l)->data, (++(*l)->length)*sizeof(void*));
+ p_realloc(&(*l)->data, ++(*l)->length);
(*l)->data[(*l)->length-1] = p;
}
void list_push_front (list2_t** l, void* p) {
if (!*l)
*l = list_new ();
- mem_realloc (&(*l)->data, (++(*l)->length)*sizeof(void*));
+ p_realloc(&(*l)->data, ++(*l)->length);
if ((*l)->length > 1)
memmove (&(*l)->data[1], &(*l)->data[0], ((*l)->length-1)*sizeof(void*));
(*l)->data[0] = p;
if (list_empty(l))
return (NULL);
p = l->data[l->length-1];
- mem_realloc (&l->data, --(l->length)*sizeof(void*));
+ p_realloc(&l->data, --(l->length));
return (p);
}
return (NULL);
p = l->data[0];
memmove (&l->data[0], &l->data[1], (--(l->length))*sizeof(void*));
- mem_realloc (&l->data, l->length*sizeof(void*));
+ p_realloc(&l->data, l->length);
return (p);
}
return (list_pop_back (l));
p = l->data[c];
memmove (&l->data[c], &l->data[c+1], (l->length-c)*sizeof(void*));
- mem_realloc (&l->data, (--(l->length))*sizeof(void*));
+ p_realloc(&l->data, --(l->length));
return (p);
}
-list2_t* list_cpy (list2_t* l) {
- list2_t* ret = NULL;
- if (list_empty(l))
- return (NULL);
- ret = list_new ();
- ret->length = l->length;
- ret->data = mem_malloc (l->length*sizeof(void*));
- memcpy (ret->data, l->data, l->length*sizeof(void*));
- return (ret);
+list2_t *list_cpy(list2_t *l) {
+ list2_t* ret = NULL;
+ if (list_empty(l))
+ return NULL;
+ ret = list_new();
+ ret->length = l->length;
+ ret->data = p_dup(l->data, l->length);
+ return ret;
}
-list2_t* list_dup (list2_t* l, void* (*dup) (void*)) {
+list2_t* list_dup (list2_t* l, void* (*dup_f) (void*)) {
list2_t* ret = NULL;
int i = 0;
- if (list_empty(l) || !*dup)
+ if (list_empty(l) || !*dup_f)
return (NULL);
ret = list_new ();
ret->length = l->length;
- ret->data = mem_malloc (l->length*sizeof(void*));
+ ret->data = p_new(void*, l->length);
for (i = 0; i < l->length; i++)
- ret->data[i] = dup (l->data[i]);
+ ret->data[i] = dup_f (l->data[i]);
return (ret);
}
return (i);
return (-1);
}
+
+list2_t* list_from_str (const char* str, const char* delim) {
+ list2_t* ret = NULL;
+ char* tmp = NULL, *p = NULL;
+
+ if (!str || !*str || !delim || !*delim)
+ return (NULL);
+
+ tmp = m_strdup(str);
+ for (p = strtok (tmp, delim); p; p = strtok (NULL, delim)) {
+ list_push_back (&ret, m_strdup(p));
+ }
+ p_delete(&tmp);
+ return (ret);
+}
+