1 | /* |
1 | /* |
2 | list.h - Part of psiconv, a PSION 5 file formats converter |
2 | list.h - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
21 | be of the same size (solve it with pointers, if needed) */ |
21 | be of the same size (solve it with pointers, if needed) */ |
22 | |
22 | |
23 | #ifndef PSICONV_LIST_H |
23 | #ifndef PSICONV_LIST_H |
24 | #define PSICONV_LIST_H |
24 | #define PSICONV_LIST_H |
25 | |
25 | |
|
|
26 | #include <psiconv/general.h> |
26 | #include <stddef.h> |
27 | #include <stddef.h> |
27 | #include <stdio.h> |
28 | #include <stdio.h> |
28 | |
29 | |
29 | #ifdef __cplusplus |
30 | #ifdef __cplusplus |
30 | extern "C" { |
31 | extern "C" { |
31 | #endif /* __cplusplus */ |
32 | #endif /* __cplusplus */ |
32 | |
33 | |
33 | /* Always use psiconv_list, never struct psiconv_list */ |
34 | /* Always use psiconv_list, never struct psiconv_list */ |
34 | /* No need to export the actual internal format */ |
35 | /* No need to export the actual internal format */ |
35 | typedef struct psiconv_list *psiconv_list; |
36 | typedef struct psiconv_list_s *psiconv_list; |
36 | |
37 | |
37 | /* Before using a list, call list_new. It takes the size of a single element |
38 | /* Before using a list, call list_new. It takes the size of a single element |
38 | as its argument. Always compute it with a sizeof() expression, just to be |
39 | as its argument. Always compute it with a sizeof() expression, just to be |
39 | safe. The returned list is empty. */ |
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 */ |
40 | extern psiconv_list psiconv_list_new(int element_size); |
44 | extern psiconv_list psiconv_list_new(size_t element_size); |
41 | |
45 | |
42 | /* This frees the list. If elements contain pointers that need to be freed |
46 | /* This frees the list. If elements contain pointers that need to be freed |
43 | separately, call list_free_el below. */ |
47 | separately, call list_free_el below. */ |
44 | extern void psiconv_list_free(psiconv_list l); |
48 | extern void psiconv_list_free(psiconv_list l); |
45 | |
49 | |
… | |
… | |
47 | Note that you should *not* do 'free(el)' at any time; that is taken care of |
51 | Note that you should *not* do 'free(el)' at any time; that is taken care of |
48 | automatically. */ |
52 | automatically. */ |
49 | extern void psiconv_list_free_el(psiconv_list l, void free_el(void *el)); |
53 | extern void psiconv_list_free_el(psiconv_list l, void free_el(void *el)); |
50 | |
54 | |
51 | /* Return the number of allocated elements */ |
55 | /* Return the number of allocated elements */ |
52 | extern int psiconv_list_length(const psiconv_list l); |
56 | extern psiconv_u32 psiconv_list_length(const psiconv_list l); |
53 | |
57 | |
54 | /* Return 1 if the list is empty, 0 if not */ |
58 | /* Return 1 if the list is empty, 0 if not */ |
55 | extern int psiconv_list_is_empty(const psiconv_list l); |
59 | extern int psiconv_list_is_empty(const psiconv_list l); |
56 | |
60 | |
57 | /* Get an element from the list, and return a pointer to it. Note: you can |
61 | /* Get an element from the list, and return a pointer to it. Note: you can |
58 | directly modify this element, but be careful not to write beyond the |
62 | directly modify this element, but be careful not to write beyond the |
59 | element memory space. */ |
63 | element memory space. |
|
|
64 | If indx is out of range, NULL is returned. */ |
60 | extern void * psiconv_list_get(const psiconv_list l, unsigned int indx); |
65 | extern void * psiconv_list_get(const psiconv_list l, psiconv_u32 indx); |
61 | |
66 | |
62 | /* Add an element at the end of the list. The element is copied from the |
67 | /* Add an element at the end of the list. The element is copied from the |
63 | supplied element. Of course, this does not help if the element contains |
68 | supplied element. Of course, this does not help if the element contains |
64 | pointers. */ |
69 | pointers. |
|
|
70 | As the lists extends itself, it may be necessary to allocate new |
|
|
71 | memory. If this fails, a negative error-code is returned. If everything, |
|
|
72 | succeeds, 0 is returned. */ |
65 | extern void psiconv_list_add(psiconv_list l, void *el); |
73 | extern int psiconv_list_add(psiconv_list l, const void *el); |
66 | |
74 | |
67 | /* Do some action for each element. Note: you can directly modify the |
75 | /* Do some action for each element. Note: you can directly modify the |
68 | elements supplied to action, and they will be changed in the list, |
76 | elements supplied to action, and they will be changed in the list, |
69 | but never try a free(el)! */ |
77 | but never try a free(el)! */ |
70 | extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); |
78 | extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); |
71 | |
79 | |
72 | /* Clone the list, that is, copy it. If elements contain pointers, you |
80 | /* Clone the list, that is, copy it. If elements contain pointers, you |
73 | should call the next routine. */ |
81 | should call the next routine. If not enough memory is available, |
|
|
82 | NULL is returned. */ |
74 | extern psiconv_list psiconv_list_clone(const psiconv_list l); |
83 | extern psiconv_list psiconv_list_clone(const psiconv_list l); |
75 | |
|
|
76 | /* Clone the list. For each element, clone_el is called. The elements which |
|
|
77 | are given to clone_el must be modified in place, as needed. */ |
|
|
78 | extern psiconv_list psiconv_list_clone_el(const psiconv_list l, |
|
|
79 | void clone_el(void *el)); |
|
|
80 | |
84 | |
81 | /* Read upto size_t elements from file f, and put them at the end of list l. |
85 | /* Read upto size_t elements from file f, and put them at the end of list l. |
82 | Returned is the actual number of elements added. This assumes the file |
86 | Returned is the actual number of elements added. This assumes the file |
83 | layout and the memory layout of elements is the same. */ |
87 | layout and the memory layout of elements is the same. Note that if |
|
|
88 | not enough memory could be allocated, 0 is simply returned. */ |
84 | extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); |
89 | extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); |
|
|
90 | |
|
|
91 | /* Read the whole file f to list l. Returns 0 on succes, and an errorcode |
|
|
92 | on failure. */ |
|
|
93 | extern int psiconv_list_fread_all(psiconv_list l, FILE *f); |
|
|
94 | |
|
|
95 | /* Write the whole list l to the opened file f. Returns 0 on succes, and |
|
|
96 | an errorcode on failure. */ |
|
|
97 | extern int psiconv_list_fwrite_all(const psiconv_list l, FILE *f); |
|
|
98 | |
|
|
99 | /* Concatenate two lists. The element sized does not have to be the same, |
|
|
100 | but the result may be quite unexpected if it is not. */ |
|
|
101 | int psiconv_list_concat(psiconv_list l, const psiconv_list extra); |
|
|
102 | |
85 | |
103 | |
86 | #ifdef __cplusplus |
104 | #ifdef __cplusplus |
87 | } |
105 | } |
88 | #endif /* __cplusplus */ |
106 | #endif /* __cplusplus */ |
89 | |
107 | |