/[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 71 - (hide annotations)
Fri Dec 22 22:31:50 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 3229 byte(s)
(Frodo) First generate routines! Reshuffled a few things to make it all work out

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     #include "list.h"
27 frodo 62 #include "error.h"
28 frodo 2
29 frodo 62 static int psiconv_list_resize(psiconv_list l,unsigned int nr);
30 frodo 2
31 frodo 56 struct psiconv_list_s {
32 frodo 2 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 frodo 62 if (!l)
43     return NULL;
44 frodo 2 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 frodo 70 int psiconv_list_add(psiconv_list l, const void *el)
85 frodo 2 {
86 frodo 62 if (psiconv_list_resize(l,l->cur_len + 1))
87     return -PSICONV_E_NOMEM;
88 frodo 2 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
89     l->cur_len ++;
90 frodo 62 return 0;
91 frodo 2 }
92    
93     void psiconv_list_foreach_el(psiconv_list l, void action(void *el))
94     {
95     int i;
96     for (i = 0; i < l->cur_len; i ++)
97     action(psiconv_list_get(l,i));
98     }
99    
100     psiconv_list psiconv_list_clone(const psiconv_list l)
101     {
102     psiconv_list l2;
103     int i;
104     l2 = psiconv_list_new(l->el_size);
105 frodo 62 if (!l2)
106     return NULL;
107 frodo 2 for (i = 0; i < l->cur_len; i ++)
108 frodo 62 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
109     psiconv_list_free(l2);
110     return NULL;
111     }
112 frodo 2 return l2;
113    
114     }
115    
116    
117     size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f)
118     {
119     size_t res;
120 frodo 62 if (psiconv_list_resize(l,l->cur_len + size))
121     return 0;
122 frodo 2 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 frodo 62 int psiconv_list_resize(psiconv_list l,unsigned int nr)
128 frodo 2 {
129 frodo 62 void * temp;
130 frodo 2 if (nr > l->max_len) {
131     l->max_len = 1.1 * nr;
132     l->max_len += 16 - l->max_len % 16;
133 frodo 62 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 frodo 2 }
140 frodo 65 return 0;
141 frodo 2 }
142    

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