/[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 80 - (show annotations)
Wed Dec 27 02:12:23 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 4611 byte(s)
(Frodo) Many changes. Layout sections now work!

* Added list_empty, list_replace
* Added relocation to buffers, removed base address
* Added buffer_resolve, buffer_add_reference, buffer_add_target,
  psiconv_unique_id functions
* Modifified other buffer functions to work with references
* Added comments to buffer.h
* Added write_offset function
* Added reference support in functions where needed
* Corrected extra/rewrite error message
* Corrected numerous bugs to make layouts 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_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 void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
109 {
110 psiconv_u32 i;
111 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 psiconv_u32 i;
119 l2 = psiconv_list_new(l->el_size);
120 if (!l2)
121 return NULL;
122 for (i = 0; i < l->cur_len; i ++)
123 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
124 psiconv_list_free(l2);
125 return NULL;
126 }
127 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 if (psiconv_list_resize(l,l->cur_len + size))
135 return 0;
136 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 int psiconv_list_fread_all(psiconv_list l, FILE *f)
142 {
143 while (!feof(f)) {
144 if (!psiconv_list_fread(l,1024,f) && !feof(f))
145 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 psiconv_u32 pos = 0;
153 psiconv_u32 written;
154 psiconv_u32 len = psiconv_list_length(l);
155 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 int psiconv_list_resize(psiconv_list l,psiconv_u32 nr)
165 {
166 void * temp;
167 if (nr > l->max_len) {
168 l->max_len = 1.1 * nr;
169 l->max_len += 16 - l->max_len % 16;
170 temp = realloc(l->els,l->max_len * l->el_size);
171 if (temp) {
172 l->els = temp;
173 return -PSICONV_E_OK;
174 } else
175 return -PSICONV_E_NOMEM;
176 }
177 return -PSICONV_E_OK;
178 }
179
180 int psiconv_list_concat(psiconv_list l, const psiconv_list extra)
181 {
182 int res;
183 if (l->el_size != extra->el_size)
184 return -PSICONV_E_OTHER;
185 if ((res = psiconv_list_resize(l,
186 l->cur_len + extra->cur_len)))
187 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 l->cur_len += extra->cur_len;
192 return 0;
193 }
194
195

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