1 |
/* |
2 |
list.h - 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 |
/* A generic list type. In C++, this would be much neater. All elements must |
21 |
be of the same size (solve it with pointers, if needed) */ |
22 |
|
23 |
#ifndef PSICONV_LIST_H |
24 |
#define PSICONV_LIST_H |
25 |
|
26 |
#include <psiconv/general.h> |
27 |
#include <stddef.h> |
28 |
#include <stdio.h> |
29 |
|
30 |
#ifdef __cplusplus |
31 |
extern "C" { |
32 |
#endif /* __cplusplus */ |
33 |
|
34 |
/* Always use psiconv_list, never struct psiconv_list */ |
35 |
/* No need to export the actual internal format */ |
36 |
typedef struct psiconv_list_s *psiconv_list; |
37 |
|
38 |
/* Before using a list, call list_new. It takes the size of a single element |
39 |
as its argument. Always compute it with a sizeof() expression, just to be |
40 |
safe. The returned list is empty. |
41 |
If there is not enough memory available, NULL is returned. You should |
42 |
always test for this explicitely, because the other functions do not |
43 |
like a psiconv_list argument that is equal to NULL */ |
44 |
extern psiconv_list psiconv_list_new(size_t element_size); |
45 |
|
46 |
/* This frees the list. If elements contain pointers that need to be freed |
47 |
separately, call list_free_el below. */ |
48 |
extern void psiconv_list_free(psiconv_list l); |
49 |
|
50 |
/* This calls free_el first for each element, before doing a list_free. |
51 |
Note that you should *not* do 'free(el)' at any time; that is taken care of |
52 |
automatically. */ |
53 |
extern void psiconv_list_free_el(psiconv_list l, void free_el(void *el)); |
54 |
|
55 |
/* Return the number of allocated elements */ |
56 |
extern psiconv_u32 psiconv_list_length(const psiconv_list l); |
57 |
|
58 |
/* Return 1 if the list is empty, 0 if not */ |
59 |
extern int psiconv_list_is_empty(const psiconv_list l); |
60 |
|
61 |
/* Empty a list. Note this does not reclaim any memory space! */ |
62 |
extern void psiconv_list_empty(psiconv_list l); |
63 |
|
64 |
/* Get an element from the list, and return a pointer to it. Note: you can |
65 |
directly modify this element, but be careful not to write beyond the |
66 |
element memory space. |
67 |
If indx is out of range, NULL is returned. */ |
68 |
extern void * psiconv_list_get(const psiconv_list l, psiconv_u32 indx); |
69 |
|
70 |
/* Add an element at the end of the list. The element is copied from the |
71 |
supplied element. Of course, this does not help if the element contains |
72 |
pointers. |
73 |
As the lists extends itself, it may be necessary to allocate new |
74 |
memory. If this fails, a negative error-code is returned. If everything, |
75 |
succeeds, 0 is returned. */ |
76 |
extern int psiconv_list_add(psiconv_list l, const void *el); |
77 |
|
78 |
/* Replace an element within the list. The element is copied from the |
79 |
supplied element. Fails if you try to write at or after the end of |
80 |
the list. */ |
81 |
extern int psiconv_list_replace(psiconv_list l, psiconv_u32 indx, |
82 |
const void *el); |
83 |
|
84 |
/* Do some action for each element. Note: you can directly modify the |
85 |
elements supplied to action, and they will be changed in the list, |
86 |
but never try a free(el)! */ |
87 |
extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); |
88 |
|
89 |
/* Clone the list, that is, copy it. If elements contain pointers, you |
90 |
should call the next routine. If not enough memory is available, |
91 |
NULL is returned. */ |
92 |
extern psiconv_list psiconv_list_clone(const psiconv_list l); |
93 |
|
94 |
/* Read upto size_t elements from file f, and put them at the end of list l. |
95 |
Returned is the actual number of elements added. This assumes the file |
96 |
layout and the memory layout of elements is the same. Note that if |
97 |
not enough memory could be allocated, 0 is simply returned. */ |
98 |
extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); |
99 |
|
100 |
/* Read the whole file f to list l. Returns 0 on succes, and an errorcode |
101 |
on failure. */ |
102 |
extern int psiconv_list_fread_all(psiconv_list l, FILE *f); |
103 |
|
104 |
/* Write the whole list l to the opened file f. Returns 0 on succes, and |
105 |
an errorcode on failure. */ |
106 |
extern int psiconv_list_fwrite_all(const psiconv_list l, FILE *f); |
107 |
|
108 |
/* Concatenate two lists. The element sized does not have to be the same, |
109 |
but the result may be quite unexpected if it is not. */ |
110 |
int psiconv_list_concat(psiconv_list l, const psiconv_list extra); |
111 |
|
112 |
|
113 |
#ifdef __cplusplus |
114 |
} |
115 |
#endif /* __cplusplus */ |
116 |
|
117 |
#endif |