--- psiconv/trunk/lib/psiconv/list.c 1999/10/03 21:10:47 2 +++ psiconv/trunk/lib/psiconv/list.c 2000/12/23 20:21:40 72 @@ -1,6 +1,6 @@ /* list.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 1999 Frodo Looijaard + Copyright (c) 1999, 2000 Frodo Looijaard 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 @@ -19,14 +19,16 @@ #include "config.h" +#include "compat.h" #include #include #include #include "list.h" +#include "error.h" -static void psiconv_list_resize(psiconv_list l,unsigned int nr); +static int psiconv_list_resize(psiconv_list l,unsigned int nr); -struct psiconv_list { +struct psiconv_list_s { int cur_len; int max_len; int el_size; @@ -37,6 +39,8 @@ { psiconv_list l; l = malloc(sizeof(*l)); + if (!l) + return NULL; l->cur_len = 0; l->max_len = 0; l->el_size=element_size; @@ -77,11 +81,14 @@ return ((char *) (l->els)) + indx * l->el_size; } -void psiconv_list_add(psiconv_list l, void *el) +int psiconv_list_add(psiconv_list l, const void *el) { - psiconv_list_resize(l,l->cur_len + 1); + int res; + if ((res = psiconv_list_resize(l,l->cur_len + 1))) + return res; memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); l->cur_len ++; + return 0; } void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) @@ -96,34 +103,51 @@ psiconv_list l2; int i; l2 = psiconv_list_new(l->el_size); + if (!l2) + return NULL; for (i = 0; i < l->cur_len; i ++) - psiconv_list_add(l2,psiconv_list_get(l,i)); + if (psiconv_list_add(l2,psiconv_list_get(l,i))) { + psiconv_list_free(l2); + return NULL; + } return l2; } -psiconv_list psiconv_list_clone_el(const psiconv_list l,void clone_el(void *el)) -{ - psiconv_list l2 = psiconv_list_clone(l); - psiconv_list_foreach_el(l2,clone_el); - return l2; -} - size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) { size_t res; - psiconv_list_resize(l,l->cur_len + size); + if (psiconv_list_resize(l,l->cur_len + size)) + return -PSICONV_E_NOMEM; res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); l->cur_len += res; return res; } -void psiconv_list_resize(psiconv_list l,unsigned int nr) +int psiconv_list_resize(psiconv_list l,unsigned int nr) { + void * temp; if (nr > l->max_len) { l->max_len = 1.1 * nr; l->max_len += 16 - l->max_len % 16; - l->els = realloc(l->els,l->max_len * l->el_size); + temp = realloc(l->els,l->max_len * l->el_size); + if (temp) { + l->els = temp; + return 0; + } else + return -PSICONV_E_NOMEM; } + return 0; } +int psiconv_list_concat(psiconv_list l, const psiconv_list extra) +{ + int res; + if ((res = psiconv_list_resize(l, + l->cur_len + extra->cur_len * extra->el_size))) + return res; + /* Unreadable but correct. */ + memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els, + extra->cur_len * extra->el_size); + return 0; +}