/[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 76 - (show annotations)
Mon Dec 25 00:26:53 2000 UTC (18 years, 11 months ago) by frodo
File MIME type: text/plain
File size: 4233 byte(s)
(Frodo) Added word-specific generation routines

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

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