/[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 79 - (hide 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 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 79 static int psiconv_list_resize(psiconv_list l,psiconv_u32 nr);
31 frodo 2
32 frodo 56 struct psiconv_list_s {
33 frodo 79 psiconv_u32 cur_len;
34     psiconv_u32 max_len;
35     size_t el_size;
36 frodo 2 void *els;
37     };
38    
39 frodo 79 psiconv_list psiconv_list_new(size_t element_size)
40 frodo 2 {
41     psiconv_list l;
42     l = malloc(sizeof(*l));
43 frodo 62 if (!l)
44     return NULL;
45 frodo 2 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 frodo 79 psiconv_u32 psiconv_list_length(const psiconv_list l)
68 frodo 2 {
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 frodo 79 void *psiconv_list_get(const psiconv_list l, psiconv_u32 indx)
78 frodo 2 {
79     if (indx >= l->cur_len)
80     return NULL;
81     else
82     return ((char *) (l->els)) + indx * l->el_size;
83     }
84    
85 frodo 70 int psiconv_list_add(psiconv_list l, const void *el)
86 frodo 2 {
87 frodo 72 int res;
88     if ((res = psiconv_list_resize(l,l->cur_len + 1)))
89     return res;
90 frodo 2 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
91     l->cur_len ++;
92 frodo 62 return 0;
93 frodo 2 }
94    
95     void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
96     {
97 frodo 79 psiconv_u32 i;
98 frodo 2 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 frodo 79 psiconv_u32 i;
106 frodo 2 l2 = psiconv_list_new(l->el_size);
107 frodo 62 if (!l2)
108     return NULL;
109 frodo 2 for (i = 0; i < l->cur_len; i ++)
110 frodo 62 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
111     psiconv_list_free(l2);
112     return NULL;
113     }
114 frodo 2 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 frodo 62 if (psiconv_list_resize(l,l->cur_len + size))
122 frodo 74 return 0;
123 frodo 2 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 frodo 74 int psiconv_list_fread_all(psiconv_list l, FILE *f)
129     {
130     while (!feof(f)) {
131 frodo 76 if (!psiconv_list_fread(l,1024,f) && !feof(f))
132 frodo 74 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 frodo 79 psiconv_u32 pos = 0;
140     psiconv_u32 written;
141     psiconv_u32 len = psiconv_list_length(l);
142 frodo 74 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 frodo 79 int psiconv_list_resize(psiconv_list l,psiconv_u32 nr)
152 frodo 2 {
153 frodo 62 void * temp;
154 frodo 2 if (nr > l->max_len) {
155     l->max_len = 1.1 * nr;
156     l->max_len += 16 - l->max_len % 16;
157 frodo 62 temp = realloc(l->els,l->max_len * l->el_size);
158     if (temp) {
159     l->els = temp;
160 frodo 76 return -PSICONV_E_OK;
161 frodo 62 } else
162     return -PSICONV_E_NOMEM;
163 frodo 2 }
164 frodo 76 return -PSICONV_E_OK;
165 frodo 2 }
166    
167 frodo 72 int psiconv_list_concat(psiconv_list l, const psiconv_list extra)
168     {
169     int res;
170 frodo 76 if (l->el_size != extra->el_size)
171     return -PSICONV_E_OTHER;
172 frodo 72 if ((res = psiconv_list_resize(l,
173 frodo 76 l->cur_len + extra->cur_len)))
174 frodo 72 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 frodo 76 l->cur_len += extra->cur_len;
179 frodo 72 return 0;
180     }

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