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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (hide annotations)
Tue Jan 29 18:38:38 2002 UTC (18 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 4838 byte(s)
(Frodo) DMALLOC support

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

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