/[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 168 - (hide annotations)
Tue Nov 25 17:57:05 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 4858 byte(s)
(Frodo) config stuff and image generation stuff

* All parse and generate functions have a new config parameter
* New files configuration.[ch] in the psiconv lib
* Some image generation stuff (not ready, but won't do any harm)

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

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