/[public]/psiconv/trunk/lib/psiconv/list.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2 Revision 71
1/* 1/*
2 list.c - Part of psiconv, a PSION 5 file formats converter 2 list.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18*/
19 19
20 20
21#include "config.h" 21#include "config.h"
22#include "compat.h"
22#include <stddef.h> 23#include <stddef.h>
23#include <stdlib.h> 24#include <stdlib.h>
24#include <stdio.h> 25#include <stdio.h>
25#include "list.h" 26#include "list.h"
27#include "error.h"
26 28
27static void psiconv_list_resize(psiconv_list l,unsigned int nr); 29static int psiconv_list_resize(psiconv_list l,unsigned int nr);
28 30
29struct psiconv_list { 31struct psiconv_list_s {
30 int cur_len; 32 int cur_len;
31 int max_len; 33 int max_len;
32 int el_size; 34 int el_size;
33 void *els; 35 void *els;
34}; 36};
35 37
36psiconv_list psiconv_list_new(int element_size) 38psiconv_list psiconv_list_new(int element_size)
37{ 39{
38 psiconv_list l; 40 psiconv_list l;
39 l = malloc(sizeof(*l)); 41 l = malloc(sizeof(*l));
42 if (!l)
43 return NULL;
40 l->cur_len = 0; 44 l->cur_len = 0;
41 l->max_len = 0; 45 l->max_len = 0;
42 l->el_size=element_size; 46 l->el_size=element_size;
43 l->els = NULL; 47 l->els = NULL;
44 return l; 48 return l;
75 return NULL; 79 return NULL;
76 else 80 else
77 return ((char *) (l->els)) + indx * l->el_size; 81 return ((char *) (l->els)) + indx * l->el_size;
78} 82}
79 83
80void psiconv_list_add(psiconv_list l, void *el) 84int psiconv_list_add(psiconv_list l, const void *el)
81{ 85{
82 psiconv_list_resize(l,l->cur_len + 1); 86 if (psiconv_list_resize(l,l->cur_len + 1))
87 return -PSICONV_E_NOMEM;
83 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); 88 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
84 l->cur_len ++; 89 l->cur_len ++;
90 return 0;
85} 91}
86 92
87void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) 93void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
88{ 94{
89 int i; 95 int i;
94psiconv_list psiconv_list_clone(const psiconv_list l) 100psiconv_list psiconv_list_clone(const psiconv_list l)
95{ 101{
96 psiconv_list l2; 102 psiconv_list l2;
97 int i; 103 int i;
98 l2 = psiconv_list_new(l->el_size); 104 l2 = psiconv_list_new(l->el_size);
105 if (!l2)
106 return NULL;
99 for (i = 0; i < l->cur_len; i ++) 107 for (i = 0; i < l->cur_len; i ++)
100 psiconv_list_add(l2,psiconv_list_get(l,i)); 108 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
109 psiconv_list_free(l2);
110 return NULL;
111 }
101 return l2; 112 return l2;
102 113
103} 114}
104 115
105psiconv_list psiconv_list_clone_el(const psiconv_list l,void clone_el(void *el))
106{
107 psiconv_list l2 = psiconv_list_clone(l);
108 psiconv_list_foreach_el(l2,clone_el);
109 return l2;
110}
111 116
112size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) 117size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
113{ 118{
114 size_t res; 119 size_t res;
115 psiconv_list_resize(l,l->cur_len + size); 120 if (psiconv_list_resize(l,l->cur_len + size))
121 return 0;
116 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); 122 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f);
117 l->cur_len += res; 123 l->cur_len += res;
118 return res; 124 return res;
119} 125}
120 126
121void psiconv_list_resize(psiconv_list l,unsigned int nr) 127int psiconv_list_resize(psiconv_list l,unsigned int nr)
122{ 128{
129 void * temp;
123 if (nr > l->max_len) { 130 if (nr > l->max_len) {
124 l->max_len = 1.1 * nr; 131 l->max_len = 1.1 * nr;
125 l->max_len += 16 - l->max_len % 16; 132 l->max_len += 16 - l->max_len % 16;
126 l->els = realloc(l->els,l->max_len * l->el_size); 133 temp = realloc(l->els,l->max_len * l->el_size);
134 if (temp) {
135 l->els = temp;
136 return 0;
137 } else
138 return -PSICONV_E_NOMEM;
127 } 139 }
140 return 0;
128} 141}
129 142

Legend:
Removed from v.2  
changed lines
  Added in v.71

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26