/[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 2 - (show annotations)
Sun Oct 3 21:10:47 1999 UTC (22 years ago) by frodo
File MIME type: text/plain
File size: 3062 byte(s)
Imported sources

1 /*
2 list.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999 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 <stddef.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "list.h"
26
27 static void psiconv_list_resize(psiconv_list l,unsigned int nr);
28
29 struct psiconv_list {
30 int cur_len;
31 int max_len;
32 int el_size;
33 void *els;
34 };
35
36 psiconv_list psiconv_list_new(int element_size)
37 {
38 psiconv_list l;
39 l = malloc(sizeof(*l));
40 l->cur_len = 0;
41 l->max_len = 0;
42 l->el_size=element_size;
43 l->els = NULL;
44 return l;
45 }
46
47 void psiconv_list_free(psiconv_list l)
48 {
49 if (l->max_len)
50 free(l->els);
51 l->max_len = 0;
52 l->cur_len = 0;
53 l->els = NULL;
54 }
55
56 void psiconv_list_free_el(psiconv_list l, void free_el(void *el))
57 {
58 psiconv_list_foreach_el(l,free_el);
59 psiconv_list_free(l);
60 }
61
62 int psiconv_list_length(const psiconv_list l)
63 {
64 return l->cur_len;
65 }
66
67 int psiconv_list_is_empty(const psiconv_list l)
68 {
69 return l->cur_len == 0;
70 }
71
72 void *psiconv_list_get(const psiconv_list l, unsigned int indx)
73 {
74 if (indx >= l->cur_len)
75 return NULL;
76 else
77 return ((char *) (l->els)) + indx * l->el_size;
78 }
79
80 void psiconv_list_add(psiconv_list l, void *el)
81 {
82 psiconv_list_resize(l,l->cur_len + 1);
83 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
84 l->cur_len ++;
85 }
86
87 void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
88 {
89 int i;
90 for (i = 0; i < l->cur_len; i ++)
91 action(psiconv_list_get(l,i));
92 }
93
94 psiconv_list psiconv_list_clone(const psiconv_list l)
95 {
96 psiconv_list l2;
97 int i;
98 l2 = psiconv_list_new(l->el_size);
99 for (i = 0; i < l->cur_len; i ++)
100 psiconv_list_add(l2,psiconv_list_get(l,i));
101 return l2;
102
103 }
104
105 psiconv_list psiconv_list_clone_el(const psiconv_list l,void clone_el(void *el))
106 {
107 psiconv_list l2 = psiconv_list_clone(l);
108 psiconv_list_foreach_el(l2,clone_el);
109 return l2;
110 }
111
112 size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
113 {
114 size_t res;
115 psiconv_list_resize(l,l->cur_len + size);
116 res = fread(((char *) (l->els)) + l->cur_len * l->el_size,l->el_size,size,f);
117 l->cur_len += res;
118 return res;
119 }
120
121 void psiconv_list_resize(psiconv_list l,unsigned int nr)
122 {
123 if (nr > l->max_len) {
124 l->max_len = 1.1 * nr;
125 l->max_len += 16 - l->max_len % 16;
126 l->els = realloc(l->els,l->max_len * l->el_size);
127 }
128 }
129

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