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 |
#ifdef DMALLOC |
31 |
#include <dmalloc.h> |
32 |
#endif |
33 |
|
34 |
|
35 |
static int psiconv_list_resize(psiconv_list l,psiconv_u32 nr); |
36 |
|
37 |
struct psiconv_list_s { |
38 |
psiconv_u32 cur_len; |
39 |
psiconv_u32 max_len; |
40 |
size_t el_size; |
41 |
void *els; |
42 |
}; |
43 |
|
44 |
psiconv_list psiconv_list_new(size_t element_size) |
45 |
{ |
46 |
psiconv_list l; |
47 |
l = malloc(sizeof(*l)); |
48 |
if (!l) |
49 |
return NULL; |
50 |
l->cur_len = 0; |
51 |
l->max_len = 0; |
52 |
l->el_size=element_size; |
53 |
l->els = NULL; |
54 |
return l; |
55 |
} |
56 |
|
57 |
void psiconv_list_free(psiconv_list l) |
58 |
{ |
59 |
if (l->max_len) |
60 |
free(l->els); |
61 |
free(l); |
62 |
l = NULL; |
63 |
} |
64 |
|
65 |
void psiconv_list_free_el(psiconv_list l, void free_el(void *el)) |
66 |
{ |
67 |
psiconv_list_foreach_el(l,free_el); |
68 |
psiconv_list_free(l); |
69 |
} |
70 |
|
71 |
psiconv_u32 psiconv_list_length(const psiconv_list l) |
72 |
{ |
73 |
return l->cur_len; |
74 |
} |
75 |
|
76 |
int psiconv_list_is_empty(const psiconv_list l) |
77 |
{ |
78 |
return l->cur_len == 0; |
79 |
} |
80 |
|
81 |
void psiconv_list_empty(psiconv_list l) |
82 |
{ |
83 |
l->cur_len = 0; |
84 |
} |
85 |
|
86 |
void *psiconv_list_get(const psiconv_list l, psiconv_u32 indx) |
87 |
{ |
88 |
if (indx >= l->cur_len) |
89 |
return NULL; |
90 |
else |
91 |
return ((char *) (l->els)) + indx * l->el_size; |
92 |
} |
93 |
|
94 |
int psiconv_list_add(psiconv_list l, const void *el) |
95 |
{ |
96 |
int res; |
97 |
if ((res = psiconv_list_resize(l,l->cur_len + 1))) |
98 |
return res; |
99 |
memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); |
100 |
l->cur_len ++; |
101 |
return 0; |
102 |
} |
103 |
|
104 |
int psiconv_list_pop(psiconv_list l, void *el) |
105 |
{ |
106 |
if (! l->cur_len) |
107 |
return -PSICONV_E_OTHER; |
108 |
l->cur_len --; |
109 |
memcpy(el,((char *)(l->els)) + l->cur_len * l->el_size,l->el_size); |
110 |
return -PSICONV_E_OK; |
111 |
} |
112 |
|
113 |
int psiconv_list_replace(psiconv_list l, psiconv_u32 indx, const void *el) |
114 |
{ |
115 |
if (indx >= l->cur_len) |
116 |
return -PSICONV_E_OTHER; |
117 |
memcpy(((char *) (l->els)) + indx * l->el_size,el, l->el_size); |
118 |
return -PSICONV_E_OK; |
119 |
} |
120 |
|
121 |
void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) |
122 |
{ |
123 |
psiconv_u32 i; |
124 |
for (i = 0; i < l->cur_len; i ++) |
125 |
action(psiconv_list_get(l,i)); |
126 |
} |
127 |
|
128 |
psiconv_list psiconv_list_clone(const psiconv_list l) |
129 |
{ |
130 |
psiconv_list l2; |
131 |
psiconv_u32 i; |
132 |
l2 = psiconv_list_new(l->el_size); |
133 |
if (!l2) |
134 |
return NULL; |
135 |
for (i = 0; i < l->cur_len; i ++) |
136 |
if (psiconv_list_add(l2,psiconv_list_get(l,i))) { |
137 |
psiconv_list_free(l2); |
138 |
return NULL; |
139 |
} |
140 |
return l2; |
141 |
|
142 |
} |
143 |
|
144 |
size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) |
145 |
{ |
146 |
size_t res; |
147 |
if (psiconv_list_resize(l,l->cur_len + size)) |
148 |
return 0; |
149 |
res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); |
150 |
l->cur_len += res; |
151 |
return res; |
152 |
} |
153 |
|
154 |
int psiconv_list_fread_all(psiconv_list l, FILE *f) |
155 |
{ |
156 |
while (!feof(f)) { |
157 |
if (!psiconv_list_fread(l,1024,f) && !feof(f)) |
158 |
return -PSICONV_E_NOMEM; |
159 |
} |
160 |
return -PSICONV_E_OK; |
161 |
} |
162 |
|
163 |
int psiconv_list_fwrite_all(const psiconv_list l, FILE *f) |
164 |
{ |
165 |
psiconv_u32 pos = 0; |
166 |
psiconv_u32 written; |
167 |
psiconv_u32 len = psiconv_list_length(l); |
168 |
while (pos < len) { |
169 |
if (!(written = fwrite(((char *)(l->els)) + pos * l->el_size,l->el_size, |
170 |
len - pos,f))) |
171 |
return -PSICONV_E_OTHER; |
172 |
pos += written; |
173 |
} |
174 |
return -PSICONV_E_OK; |
175 |
} |
176 |
|
177 |
int psiconv_list_resize(psiconv_list l,psiconv_u32 nr) |
178 |
{ |
179 |
void * temp; |
180 |
if (nr > l->max_len) { |
181 |
l->max_len = 1.1 * nr; |
182 |
l->max_len += 16 - l->max_len % 16; |
183 |
temp = realloc(l->els,l->max_len * l->el_size); |
184 |
if (temp) { |
185 |
l->els = temp; |
186 |
return -PSICONV_E_OK; |
187 |
} else |
188 |
return -PSICONV_E_NOMEM; |
189 |
} |
190 |
return -PSICONV_E_OK; |
191 |
} |
192 |
|
193 |
int psiconv_list_concat(psiconv_list l, const psiconv_list extra) |
194 |
{ |
195 |
int res; |
196 |
if (l->el_size != extra->el_size) |
197 |
return -PSICONV_E_OTHER; |
198 |
if ((res = psiconv_list_resize(l, |
199 |
l->cur_len + extra->cur_len))) |
200 |
return res; |
201 |
/* Unreadable but correct. */ |
202 |
memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els, |
203 |
extra->cur_len * extra->el_size); |
204 |
l->cur_len += extra->cur_len; |
205 |
return 0; |
206 |
} |
207 |
|
208 |
|