/[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 62 - (hide annotations)
Wed Dec 13 16:17:54 2000 UTC (19 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 3185 byte(s)
(Frodo) Several important changes:
  * Created new misc.c, error.c and error.h files
  * Split parse_aux.c among them
  * Made list.c, data.c, error.c, checkuid.c and misc.c failsafe.

1 frodo 2 /*
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 frodo 62 #include "error.h"
27 frodo 2
28 frodo 62 static int psiconv_list_resize(psiconv_list l,unsigned int nr);
29 frodo 2
30 frodo 56 struct psiconv_list_s {
31 frodo 2 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 frodo 62 if (!l)
42     return NULL;
43 frodo 2 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 frodo 62 int psiconv_list_add(psiconv_list l, void *el)
84 frodo 2 {
85 frodo 62 if (psiconv_list_resize(l,l->cur_len + 1))
86     return -PSICONV_E_NOMEM;
87 frodo 2 memcpy(((char *) (l->els)) + l->cur_len * l->el_size, el, l->el_size);
88     l->cur_len ++;
89 frodo 62 return 0;
90 frodo 2 }
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 frodo 62 if (!l2)
105     return NULL;
106 frodo 2 for (i = 0; i < l->cur_len; i ++)
107 frodo 62 if (psiconv_list_add(l2,psiconv_list_get(l,i))) {
108     psiconv_list_free(l2);
109     return NULL;
110     }
111 frodo 2 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 frodo 62 if (psiconv_list_resize(l,l->cur_len + size))
120     return 0;
121 frodo 2 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 frodo 62 int psiconv_list_resize(psiconv_list l,unsigned int nr)
127 frodo 2 {
128 frodo 62 void * temp;
129 frodo 2 if (nr > l->max_len) {
130     l->max_len = 1.1 * nr;
131     l->max_len += 16 - l->max_len % 16;
132 frodo 62 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 frodo 2 }
139     }
140    

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