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