/[public]/psiconv/trunk/lib/psiconv/list.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/list.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 98 - (show annotations)
Mon Jan 29 21:57:05 2001 UTC (19 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 4823 byte(s)
(Frodo) Base formula work

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

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26