… | |
… | |
24 | #define PSICONV_LIST_H |
24 | #define PSICONV_LIST_H |
25 | |
25 | |
26 | #include <stddef.h> |
26 | #include <stddef.h> |
27 | #include <stdio.h> |
27 | #include <stdio.h> |
28 | |
28 | |
|
|
29 | #ifdef __cplusplus |
|
|
30 | extern "C" { |
|
|
31 | #endif /* __cplusplus */ |
|
|
32 | |
29 | /* Always use psiconv_list, never struct psiconv_list */ |
33 | /* Always use psiconv_list, never struct psiconv_list */ |
30 | /* No need to export the actual internal format */ |
34 | /* No need to export the actual internal format */ |
31 | typedef struct psiconv_list *psiconv_list; |
35 | typedef struct psiconv_list_s *psiconv_list; |
32 | |
36 | |
33 | /* Before using a list, call list_new. It takes the size of a single element |
37 | /* Before using a list, call list_new. It takes the size of a single element |
34 | as its argument. Always compute it with a sizeof() expression, just to be |
38 | as its argument. Always compute it with a sizeof() expression, just to be |
35 | safe. The returned list is empty. */ |
39 | safe. The returned list is empty. |
|
|
40 | If there is not enough memory available, NULL is returned. You should |
|
|
41 | always test for this explicitely, because the other functions do not |
|
|
42 | like a psiconv_list argument that is equal to NULL */ |
36 | extern psiconv_list psiconv_list_new(int element_size); |
43 | extern psiconv_list psiconv_list_new(int element_size); |
37 | |
44 | |
38 | /* This frees the list. If elements contain pointers that need to be freed |
45 | /* This frees the list. If elements contain pointers that need to be freed |
39 | separately, call list_free_el below. */ |
46 | separately, call list_free_el below. */ |
40 | extern void psiconv_list_free(psiconv_list l); |
47 | extern void psiconv_list_free(psiconv_list l); |
… | |
… | |
50 | /* Return 1 if the list is empty, 0 if not */ |
57 | /* Return 1 if the list is empty, 0 if not */ |
51 | extern int psiconv_list_is_empty(const psiconv_list l); |
58 | extern int psiconv_list_is_empty(const psiconv_list l); |
52 | |
59 | |
53 | /* Get an element from the list, and return a pointer to it. Note: you can |
60 | /* Get an element from the list, and return a pointer to it. Note: you can |
54 | directly modify this element, but be careful not to write beyond the |
61 | directly modify this element, but be careful not to write beyond the |
55 | element memory space. */ |
62 | element memory space. |
|
|
63 | If indx is out of range, NULL is returned. */ |
56 | extern void * psiconv_list_get(const psiconv_list l, unsigned int indx); |
64 | extern void * psiconv_list_get(const psiconv_list l, unsigned int indx); |
57 | |
65 | |
58 | /* Add an element at the end of the list. The element is copied from the |
66 | /* Add an element at the end of the list. The element is copied from the |
59 | supplied element. Of course, this does not help if the element contains |
67 | supplied element. Of course, this does not help if the element contains |
60 | pointers. */ |
68 | pointers. |
|
|
69 | As the lists extends itself, it may be necessary to allocate new |
|
|
70 | memory. If this fails, a negative error-code is returned. If everything, |
|
|
71 | succeeds, 0 is returned. */ |
61 | extern void psiconv_list_add(psiconv_list l, void *el); |
72 | extern int psiconv_list_add(psiconv_list l, void *el); |
62 | |
73 | |
63 | /* Do some action for each element. Note: you can directly modify the |
74 | /* Do some action for each element. Note: you can directly modify the |
64 | elements supplied to action, and they will be changed in the list, |
75 | elements supplied to action, and they will be changed in the list, |
65 | but never try a free(el)! */ |
76 | but never try a free(el)! */ |
66 | extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); |
77 | extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); |
67 | |
78 | |
68 | /* Clone the list, that is, copy it. If elements contain pointers, you |
79 | /* Clone the list, that is, copy it. If elements contain pointers, you |
69 | should call the next routine. */ |
80 | should call the next routine. If not enough memory is available, |
|
|
81 | NULL is returned. */ |
70 | extern psiconv_list psiconv_list_clone(const psiconv_list l); |
82 | extern psiconv_list psiconv_list_clone(const psiconv_list l); |
71 | |
|
|
72 | /* Clone the list. For each element, clone_el is called. The elements which |
|
|
73 | are given to clone_el must be modified in place, as needed. */ |
|
|
74 | extern psiconv_list psiconv_list_clone_el(const psiconv_list l, |
|
|
75 | void clone_el(void *el)); |
|
|
76 | |
83 | |
77 | /* Read upto size_t elements from file f, and put them at the end of list l. |
84 | /* Read upto size_t elements from file f, and put them at the end of list l. |
78 | Returned is the actual number of elements added. This assumes the file |
85 | Returned is the actual number of elements added. This assumes the file |
79 | layout and the memory layout of elements is the same. */ |
86 | layout and the memory layout of elements is the same. Note that if |
|
|
87 | not enough memory could be allocated, 0 is simply returned. */ |
80 | extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); |
88 | extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); |
81 | |
89 | |
|
|
90 | |
|
|
91 | #ifdef __cplusplus |
|
|
92 | } |
|
|
93 | #endif /* __cplusplus */ |
|
|
94 | |
82 | #endif |
95 | #endif |