/[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 65 - (show annotations)
Fri Dec 15 01:16:43 2000 UTC (19 years, 6 months ago) by frodo
File MIME type: text/plain
File size: 3203 byte(s)
(Frodo) First typos eliminated. Word files seem to work now. Need to test
        other types.

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

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