/[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 72 - (show annotations)
Sat Dec 23 20:21:40 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 3605 byte(s)
(Frodo) New generation routines in generate_layout

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 -PSICONV_E_NOMEM;
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_resize(psiconv_list l,unsigned int nr)
128 {
129 void * temp;
130 if (nr > l->max_len) {
131 l->max_len = 1.1 * nr;
132 l->max_len += 16 - l->max_len % 16;
133 temp = realloc(l->els,l->max_len * l->el_size);
134 if (temp) {
135 l->els = temp;
136 return 0;
137 } else
138 return -PSICONV_E_NOMEM;
139 }
140 return 0;
141 }
142
143 int psiconv_list_concat(psiconv_list l, const psiconv_list extra)
144 {
145 int res;
146 if ((res = psiconv_list_resize(l,
147 l->cur_len + extra->cur_len * extra->el_size)))
148 return res;
149 /* Unreadable but correct. */
150 memcpy(((char *) (l->els)) + l->cur_len * l->el_size,extra->els,
151 extra->cur_len * extra->el_size);
152 return 0;
153 }

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