1 | /* |
1 | /* |
2 | list.c - Part of psiconv, a PSION 5 file formats converter |
2 | list.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
18 | */ |
19 | |
19 | |
20 | |
20 | |
21 | #include "config.h" |
21 | #include "config.h" |
|
|
22 | #include "compat.h" |
22 | #include <stddef.h> |
23 | #include <stddef.h> |
23 | #include <stdlib.h> |
24 | #include <stdlib.h> |
24 | #include <stdio.h> |
25 | #include <stdio.h> |
25 | #include "list.h" |
26 | #include "list.h" |
|
|
27 | #include "error.h" |
26 | |
28 | |
27 | static void psiconv_list_resize(psiconv_list l,unsigned int nr); |
29 | static int psiconv_list_resize(psiconv_list l,unsigned int nr); |
28 | |
30 | |
29 | struct psiconv_list_s { |
31 | struct psiconv_list_s { |
30 | int cur_len; |
32 | int cur_len; |
31 | int max_len; |
33 | int max_len; |
32 | int el_size; |
34 | int el_size; |
… | |
… | |
35 | |
37 | |
36 | psiconv_list psiconv_list_new(int element_size) |
38 | psiconv_list psiconv_list_new(int element_size) |
37 | { |
39 | { |
38 | psiconv_list l; |
40 | psiconv_list l; |
39 | l = malloc(sizeof(*l)); |
41 | l = malloc(sizeof(*l)); |
|
|
42 | if (!l) |
|
|
43 | return NULL; |
40 | l->cur_len = 0; |
44 | l->cur_len = 0; |
41 | l->max_len = 0; |
45 | l->max_len = 0; |
42 | l->el_size=element_size; |
46 | l->el_size=element_size; |
43 | l->els = NULL; |
47 | l->els = NULL; |
44 | return l; |
48 | return l; |
… | |
… | |
75 | return NULL; |
79 | return NULL; |
76 | else |
80 | else |
77 | return ((char *) (l->els)) + indx * l->el_size; |
81 | return ((char *) (l->els)) + indx * l->el_size; |
78 | } |
82 | } |
79 | |
83 | |
80 | void psiconv_list_add(psiconv_list l, void *el) |
84 | int psiconv_list_add(psiconv_list l, const void *el) |
81 | { |
85 | { |
|
|
86 | int res; |
82 | psiconv_list_resize(l,l->cur_len + 1); |
87 | if ((res = psiconv_list_resize(l,l->cur_len + 1))) |
|
|
88 | return res; |
83 | memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); |
89 | memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size); |
84 | l->cur_len ++; |
90 | l->cur_len ++; |
|
|
91 | return 0; |
85 | } |
92 | } |
86 | |
93 | |
87 | void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) |
94 | void psiconv_list_foreach_el(psiconv_list l, void action(void *el)) |
88 | { |
95 | { |
89 | int i; |
96 | int i; |
… | |
… | |
94 | psiconv_list psiconv_list_clone(const psiconv_list l) |
101 | psiconv_list psiconv_list_clone(const psiconv_list l) |
95 | { |
102 | { |
96 | psiconv_list l2; |
103 | psiconv_list l2; |
97 | int i; |
104 | int i; |
98 | l2 = psiconv_list_new(l->el_size); |
105 | l2 = psiconv_list_new(l->el_size); |
|
|
106 | if (!l2) |
|
|
107 | return NULL; |
99 | for (i = 0; i < l->cur_len; i ++) |
108 | for (i = 0; i < l->cur_len; i ++) |
100 | psiconv_list_add(l2,psiconv_list_get(l,i)); |
109 | if (psiconv_list_add(l2,psiconv_list_get(l,i))) { |
|
|
110 | psiconv_list_free(l2); |
|
|
111 | return NULL; |
|
|
112 | } |
101 | return l2; |
113 | return l2; |
102 | |
114 | |
103 | } |
|
|
104 | |
|
|
105 | psiconv_list psiconv_list_clone_el(const psiconv_list l,void clone_el(void *el)) |
|
|
106 | { |
|
|
107 | psiconv_list l2 = psiconv_list_clone(l); |
|
|
108 | psiconv_list_foreach_el(l2,clone_el); |
|
|
109 | return l2; |
|
|
110 | } |
115 | } |
111 | |
116 | |
112 | size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) |
117 | size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f) |
113 | { |
118 | { |
114 | size_t res; |
119 | size_t res; |
115 | psiconv_list_resize(l,l->cur_len + size); |
120 | if (psiconv_list_resize(l,l->cur_len + size)) |
|
|
121 | return 0; |
116 | res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); |
122 | res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f); |
117 | l->cur_len += res; |
123 | l->cur_len += res; |
118 | return res; |
124 | return res; |
119 | } |
125 | } |
120 | |
126 | |
121 | void psiconv_list_resize(psiconv_list l,unsigned int nr) |
127 | int psiconv_list_fread_all(psiconv_list l, FILE *f) |
122 | { |
128 | { |
|
|
129 | while (!feof(f)) { |
|
|
130 | if (!psiconv_list_fread(l,1024,f)) |
|
|
131 | return -PSICONV_E_NOMEM; |
|
|
132 | } |
|
|
133 | return -PSICONV_E_OK; |
|
|
134 | } |
|
|
135 | |
|
|
136 | int psiconv_list_fwrite_all(const psiconv_list l, FILE *f) |
|
|
137 | { |
|
|
138 | int pos = 0; |
|
|
139 | int written; |
|
|
140 | int len = psiconv_list_length(l); |
|
|
141 | while (pos < len) { |
|
|
142 | if (!(written = fwrite(((char *)(l->els)) + pos * l->el_size,l->el_size, |
|
|
143 | len - pos,f))) |
|
|
144 | return -PSICONV_E_OTHER; |
|
|
145 | pos += written; |
|
|
146 | } |
|
|
147 | return -PSICONV_E_OK; |
|
|
148 | } |
|
|
149 | |
|
|
150 | int psiconv_list_resize(psiconv_list l,unsigned int nr) |
|
|
151 | { |
|
|
152 | void * temp; |
123 | if (nr > l->max_len) { |
153 | if (nr > l->max_len) { |
124 | l->max_len = 1.1 * nr; |
154 | l->max_len = 1.1 * nr; |
125 | l->max_len += 16 - l->max_len % 16; |
155 | l->max_len += 16 - l->max_len % 16; |
126 | l->els = realloc(l->els,l->max_len * l->el_size); |
156 | temp = realloc(l->els,l->max_len * l->el_size); |
|
|
157 | if (temp) { |
|
|
158 | l->els = temp; |
|
|
159 | return 0; |
|
|
160 | } else |
|
|
161 | return -PSICONV_E_NOMEM; |
127 | } |
162 | } |
|
|
163 | return 0; |
128 | } |
164 | } |
129 | |
165 | |
|
|
166 | int psiconv_list_concat(psiconv_list l, const psiconv_list extra) |
|
|
167 | { |
|
|
168 | int res; |
|
|
169 | if ((res = psiconv_list_resize(l, |
|
|
170 | l->cur_len + extra->cur_len * extra->el_size))) |
|
|
171 | return res; |
|
|
172 | /* Unreadable but correct. */ |
|
|
173 | memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els, |
|
|
174 | extra->cur_len * extra->el_size); |
|
|
175 | return 0; |
|
|
176 | } |