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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 141 - (show annotations)
Mon Jan 28 21:04:05 2002 UTC (22 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 4793 byte(s)
(Frodo) Fix memory leak

1 /*
2 list.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4
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
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20
21 #include "config.h"
22 #include "compat.h"
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "general.h"
27 #include "list.h"
28 #include "error.h"
29
30 static int psiconv_list_resize(psiconv_list l,psiconv_u32 nr);
31
32 struct psiconv_list_s {
33 psiconv_u32 cur_len;
34 psiconv_u32 max_len;
35 size_t el_size;
36 void *els;
37 };
38
39 psiconv_list psiconv_list_new(size_t element_size)
40 {
41 psiconv_list l;
42 l = malloc(sizeof(*l));
43 if (!l)
44 return NULL;
45 l->cur_len = 0;
46 l->max_len = 0;
47 l->el_size=element_size;
48 l->els = NULL;
49 return l;
50 }
51
52 void psiconv_list_free(psiconv_list l)
53 {
54 if (l->max_len)
55 free(l->els);
56 free(l);
57 l = NULL;
58 }
59
60 void psiconv_list_free_el(psiconv_list l, void free_el(void *el))
61 {
62 psiconv_list_foreach_el(l,free_el);
63 psiconv_list_free(l);
64 }
65
66 psiconv_u32 psiconv_list_length(const psiconv_list l)
67 {
68 return l->cur_len;
69 }
70
71 int psiconv_list_is_empty(const psiconv_list l)
72 {
73 return l->cur_len == 0;
74 }
75
76 void psiconv_list_empty(psiconv_list l)
77 {
78 l->cur_len = 0;
79 }
80
81 void *psiconv_list_get(const psiconv_list l, psiconv_u32 indx)
82 {
83 if (indx >= l->cur_len)
84 return NULL;
85 else
86 return ((char *) (l->els)) + indx * l->el_size;
87 }
88
89 int psiconv_list_add(psiconv_list l, const void *el)
90 {
91 int res;
92 if ((res = psiconv_list_resize(l,l->cur_len + 1)))
93 return res;
94 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
95 l->cur_len ++;
96 return 0;
97 }
98
99 int psiconv_list_pop(psiconv_list l, void *el)
100 {
101 if (! l->cur_len)
102 return -PSICONV_E_OTHER;
103 l->cur_len --;
104 memcpy(el,((char *)(l->els)) + l->cur_len * l->el_size,l->el_size);
105 return -PSICONV_E_OK;
106 }
107
108 int psiconv_list_replace(psiconv_list l, psiconv_u32 indx, const void *el)
109 {
110 if (indx >= l->cur_len)
111 return -PSICONV_E_OTHER;
112 memcpy(((char *) (l->els)) + indx * l->el_size,el, l->el_size);
113 return -PSICONV_E_OK;
114 }
115
116 void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
117 {
118 psiconv_u32 i;
119 for (i = 0; i < l->cur_len; i ++)
120 action(psiconv_list_get(l,i));
121 }
122
123 psiconv_list psiconv_list_clone(const psiconv_list l)
124 {
125 psiconv_list l2;
126 psiconv_u32 i;
127 l2 = psiconv_list_new(l->el_size);
128 if (!l2)
129 return NULL;
130 for (i = 0; i < l->cur_len; i ++)
131 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
132 psiconv_list_free(l2);
133 return NULL;
134 }
135 return l2;
136
137 }
138
139 size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
140 {
141 size_t res;
142 if (psiconv_list_resize(l,l->cur_len + size))
143 return 0;
144 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f);
145 l->cur_len += res;
146 return res;
147 }
148
149 int psiconv_list_fread_all(psiconv_list l, FILE *f)
150 {
151 while (!feof(f)) {
152 if (!psiconv_list_fread(l,1024,f) && !feof(f))
153 return -PSICONV_E_NOMEM;
154 }
155 return -PSICONV_E_OK;
156 }
157
158 int psiconv_list_fwrite_all(const psiconv_list l, FILE *f)
159 {
160 psiconv_u32 pos = 0;
161 psiconv_u32 written;
162 psiconv_u32 len = psiconv_list_length(l);
163 while (pos < len) {
164 if (!(written = fwrite(((char *)(l->els)) + pos * l->el_size,l->el_size,
165 len - pos,f)))
166 return -PSICONV_E_OTHER;
167 pos += written;
168 }
169 return -PSICONV_E_OK;
170 }
171
172 int psiconv_list_resize(psiconv_list l,psiconv_u32 nr)
173 {
174 void * temp;
175 if (nr > l->max_len) {
176 l->max_len = 1.1 * nr;
177 l->max_len += 16 - l->max_len % 16;
178 temp = realloc(l->els,l->max_len * l->el_size);
179 if (temp) {
180 l->els = temp;
181 return -PSICONV_E_OK;
182 } else
183 return -PSICONV_E_NOMEM;
184 }
185 return -PSICONV_E_OK;
186 }
187
188 int psiconv_list_concat(psiconv_list l, const psiconv_list extra)
189 {
190 int res;
191 if (l->el_size != extra->el_size)
192 return -PSICONV_E_OTHER;
193 if ((res = psiconv_list_resize(l,
194 l->cur_len + extra->cur_len)))
195 return res;
196 /* Unreadable but correct. */
197 memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els,
198 extra->cur_len * extra->el_size);
199 l->cur_len += extra->cur_len;
200 return 0;
201 }
202
203

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