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 |
|
|
#include "list.h" |
27 |
frodo |
62 |
#include "error.h" |
28 |
frodo |
2 |
|
29 |
frodo |
62 |
static int psiconv_list_resize(psiconv_list l,unsigned int nr); |
30 |
frodo |
2 |
|
31 |
frodo |
56 |
struct psiconv_list_s { |
32 |
frodo |
2 |
int cur_len; |
33 |
|
|
int max_len; |
34 |
|
|
int el_size; |
35 |
|
|
void *els; |
36 |
|
|
}; |
37 |
|
|
|
38 |
|
|
psiconv_list psiconv_list_new(int element_size) |
39 |
|
|
{ |
40 |
|
|
psiconv_list l; |
41 |
|
|
l = malloc(sizeof(*l)); |
42 |
frodo |
62 |
if (!l) |
43 |
|
|
return NULL; |
44 |
frodo |
2 |
l->cur_len = 0; |
45 |
|
|
l->max_len = 0; |
46 |
|
|
l->el_size=element_size; |
47 |
|
|
l->els = NULL; |
48 |
|
|
return l; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
void psiconv_list_free(psiconv_list l) |
52 |
|
|
{ |
53 |
|
|
if (l->max_len) |
54 |
|
|
free(l->els); |
55 |
|
|
l->max_len = 0; |
56 |
|
|
l->cur_len = 0; |
57 |
|
|
l->els = 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 |
|
|
int 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_get(const psiconv_list l, unsigned int indx) |
77 |
|
|
{ |
78 |
|
|
if (indx >= l->cur_len) |
79 |
|
|
return NULL; |
80 |
|
|
else |
81 |
|
|
return ((char *) (l->els)) + indx * l->el_size; |
82 |
|
|
} |
83 |
|
|
|
84 |
frodo |
70 |
int psiconv_list_add(psiconv_list l, const void *el) |
85 |
frodo |
2 |
{ |
86 |
frodo |
72 |
int res; |
87 |
|
|
if ((res = psiconv_list_resize(l,l->cur_len + 1))) |
88 |
|
|
return res; |
89 |
frodo |
2 |
memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); |
90 |
|
|
l->cur_len ++; |
91 |
frodo |
62 |
return 0; |
92 |
frodo |
2 |
} |
93 |
|
|
|
94 |
|
|
void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) |
95 |
|
|
{ |
96 |
|
|
int i; |
97 |
|
|
for (i = 0; i < l->cur_len; i ++) |
98 |
|
|
action(psiconv_list_get(l,i)); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
psiconv_list psiconv_list_clone(const psiconv_list l) |
102 |
|
|
{ |
103 |
|
|
psiconv_list l2; |
104 |
|
|
int i; |
105 |
|
|
l2 = psiconv_list_new(l->el_size); |
106 |
frodo |
62 |
if (!l2) |
107 |
|
|
return NULL; |
108 |
frodo |
2 |
for (i = 0; i < l->cur_len; i ++) |
109 |
frodo |
62 |
if (psiconv_list_add(l2,psiconv_list_get(l,i))) { |
110 |
|
|
psiconv_list_free(l2); |
111 |
|
|
return NULL; |
112 |
|
|
} |
113 |
frodo |
2 |
return l2; |
114 |
|
|
|
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) |
118 |
|
|
{ |
119 |
|
|
size_t res; |
120 |
frodo |
62 |
if (psiconv_list_resize(l,l->cur_len + size)) |
121 |
frodo |
72 |
return -PSICONV_E_NOMEM; |
122 |
frodo |
2 |
res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); |
123 |
|
|
l->cur_len += res; |
124 |
|
|
return res; |
125 |
|
|
} |
126 |
|
|
|
127 |
frodo |
62 |
int psiconv_list_resize(psiconv_list l,unsigned int nr) |
128 |
frodo |
2 |
{ |
129 |
frodo |
62 |
void * temp; |
130 |
frodo |
2 |
if (nr > l->max_len) { |
131 |
|
|
l->max_len = 1.1 * nr; |
132 |
|
|
l->max_len += 16 - l->max_len % 16; |
133 |
frodo |
62 |
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; |
139 |
frodo |
2 |
} |
140 |
frodo |
65 |
return 0; |
141 |
frodo |
2 |
} |
142 |
|
|
|
143 |
frodo |
72 |
int psiconv_list_concat(psiconv_list l, const psiconv_list extra) |
144 |
|
|
{ |
145 |
|
|
int res; |
146 |
|
|
if ((res = psiconv_list_resize(l, |
147 |
|
|
l->cur_len + extra->cur_len * extra->el_size))) |
148 |
|
|
return res; |
149 |
|
|
/* Unreadable but correct. */ |
150 |
|
|
memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els, |
151 |
|
|
extra->cur_len * extra->el_size); |
152 |
|
|
return 0; |
153 |
|
|
} |