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