/[public]/psiconv/trunk/lib/psiconv/list.h
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/list.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 63 Revision 80
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
30extern "C" { 31extern "C" {
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.
40 If there is not enough memory available, NULL is returned. You should 41 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 always test for this explicitely, because the other functions do not
42 like a psiconv_list argument that is equal to NULL */ 43 like a psiconv_list argument that is equal to NULL */
43extern psiconv_list psiconv_list_new(int element_size); 44extern psiconv_list psiconv_list_new(size_t element_size);
44 45
45/* 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
46 separately, call list_free_el below. */ 47 separately, call list_free_el below. */
47extern void psiconv_list_free(psiconv_list l); 48extern void psiconv_list_free(psiconv_list l);
48 49
50 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
51 automatically. */ 52 automatically. */
52extern void psiconv_list_free_el(psiconv_list l, void free_el(void *el)); 53extern void psiconv_list_free_el(psiconv_list l, void free_el(void *el));
53 54
54/* Return the number of allocated elements */ 55/* Return the number of allocated elements */
55extern int psiconv_list_length(const psiconv_list l); 56extern psiconv_u32 psiconv_list_length(const psiconv_list l);
56 57
57/* Return 1 if the list is empty, 0 if not */ 58/* Return 1 if the list is empty, 0 if not */
58extern int psiconv_list_is_empty(const psiconv_list l); 59extern int psiconv_list_is_empty(const psiconv_list l);
60
61/* Empty a list. Note this does not reclaim any memory space! */
62extern void psiconv_list_empty(psiconv_list l);
59 63
60/* Get an element from the list, and return a pointer to it. Note: you can 64/* Get an element from the list, and return a pointer to it. Note: you can
61 directly modify this element, but be careful not to write beyond the 65 directly modify this element, but be careful not to write beyond the
62 element memory space. 66 element memory space.
63 If indx is out of range, NULL is returned. */ 67 If indx is out of range, NULL is returned. */
64extern void * psiconv_list_get(const psiconv_list l, unsigned int indx); 68extern void * psiconv_list_get(const psiconv_list l, psiconv_u32 indx);
65 69
66/* Add an element at the end of the list. The element is copied from the 70/* Add an element at the end of the list. The element is copied from the
67 supplied element. Of course, this does not help if the element contains 71 supplied element. Of course, this does not help if the element contains
68 pointers. 72 pointers.
69 As the lists extends itself, it may be necessary to allocate new 73 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, 74 memory. If this fails, a negative error-code is returned. If everything,
71 succeeds, 0 is returned. */ 75 succeeds, 0 is returned. */
72extern int psiconv_list_add(psiconv_list l, void *el); 76extern 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. */
81extern int psiconv_list_replace(psiconv_list l, psiconv_u32 indx,
82 const void *el);
73 83
74/* Do some action for each element. Note: you can directly modify the 84/* Do some action for each element. Note: you can directly modify the
75 elements supplied to action, and they will be changed in the list, 85 elements supplied to action, and they will be changed in the list,
76 but never try a free(el)! */ 86 but never try a free(el)! */
77extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el)); 87extern void psiconv_list_foreach_el(psiconv_list l, void action(void *el));
85 Returned is the actual number of elements added. This assumes the file 95 Returned is the actual number of elements added. This assumes the file
86 layout and the memory layout of elements is the same. Note that if 96 layout and the memory layout of elements is the same. Note that if
87 not enough memory could be allocated, 0 is simply returned. */ 97 not enough memory could be allocated, 0 is simply returned. */
88extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f); 98extern size_t psiconv_list_fread(psiconv_list l,size_t size, FILE *f);
89 99
100/* Read the whole file f to list l. Returns 0 on succes, and an errorcode
101 on failure. */
102extern 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. */
106extern 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. */
110int psiconv_list_concat(psiconv_list l, const psiconv_list extra);
111
90 112
91#ifdef __cplusplus 113#ifdef __cplusplus
92} 114}
93#endif /* __cplusplus */ 115#endif /* __cplusplus */
94 116

Legend:
Removed from v.63  
changed lines
  Added in v.80

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