/[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 79 - (show annotations)
Mon Dec 25 22:25:33 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 4321 byte(s)
(Frodo) Added a complete buffer abstraction

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_get(const psiconv_list l, psiconv_u32 indx)
78 {
79 if (indx >= l->cur_len)
80 return NULL;
81 else
82 return ((char *) (l->els)) + indx * l->el_size;
83 }
84
85 int psiconv_list_add(psiconv_list l, const void *el)
86 {
87 int res;
88 if ((res = psiconv_list_resize(l,l->cur_len + 1)))
89 return res;
90 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
91 l->cur_len ++;
92 return 0;
93 }
94
95 void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
96 {
97 psiconv_u32 i;
98 for (i = 0; i < l->cur_len; i ++)
99 action(psiconv_list_get(l,i));
100 }
101
102 psiconv_list psiconv_list_clone(const psiconv_list l)
103 {
104 psiconv_list l2;
105 psiconv_u32 i;
106 l2 = psiconv_list_new(l->el_size);
107 if (!l2)
108 return NULL;
109 for (i = 0; i < l->cur_len; i ++)
110 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
111 psiconv_list_free(l2);
112 return NULL;
113 }
114 return l2;
115
116 }
117
118 size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
119 {
120 size_t res;
121 if (psiconv_list_resize(l,l->cur_len + size))
122 return 0;
123 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f);
124 l->cur_len += res;
125 return res;
126 }
127
128 int psiconv_list_fread_all(psiconv_list l, FILE *f)
129 {
130 while (!feof(f)) {
131 if (!psiconv_list_fread(l,1024,f) && !feof(f))
132 return -PSICONV_E_NOMEM;
133 }
134 return -PSICONV_E_OK;
135 }
136
137 int psiconv_list_fwrite_all(const psiconv_list l, FILE *f)
138 {
139 psiconv_u32 pos = 0;
140 psiconv_u32 written;
141 psiconv_u32 len = psiconv_list_length(l);
142 while (pos < len) {
143 if (!(written = fwrite(((char *)(l->els)) + pos * l->el_size,l->el_size,
144 len - pos,f)))
145 return -PSICONV_E_OTHER;
146 pos += written;
147 }
148 return -PSICONV_E_OK;
149 }
150
151 int psiconv_list_resize(psiconv_list l,psiconv_u32 nr)
152 {
153 void * temp;
154 if (nr > l->max_len) {
155 l->max_len = 1.1 * nr;
156 l->max_len += 16 - l->max_len % 16;
157 temp = realloc(l->els,l->max_len * l->el_size);
158 if (temp) {
159 l->els = temp;
160 return -PSICONV_E_OK;
161 } else
162 return -PSICONV_E_NOMEM;
163 }
164 return -PSICONV_E_OK;
165 }
166
167 int psiconv_list_concat(psiconv_list l, const psiconv_list extra)
168 {
169 int res;
170 if (l->el_size != extra->el_size)
171 return -PSICONV_E_OTHER;
172 if ((res = psiconv_list_resize(l,
173 l->cur_len + extra->cur_len)))
174 return res;
175 /* Unreadable but correct. */
176 memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els,
177 extra->cur_len * extra->el_size);
178 l->cur_len += extra->cur_len;
179 return 0;
180 }

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