/[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 2 Revision 80
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
30#ifdef __cplusplus
31extern "C" {
32#endif /* __cplusplus */
33
29/* Always use psiconv_list, never struct psiconv_list */ 34/* Always use psiconv_list, never struct psiconv_list */
30/* No need to export the actual internal format */ 35/* No need to export the actual internal format */
31typedef struct psiconv_list *psiconv_list; 36typedef struct psiconv_list_s *psiconv_list;
32 37
33/* 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
34 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
35 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 */
36extern psiconv_list psiconv_list_new(int element_size); 44extern psiconv_list psiconv_list_new(size_t element_size);
37 45
38/* 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
39 separately, call list_free_el below. */ 47 separately, call list_free_el below. */
40extern void psiconv_list_free(psiconv_list l); 48extern void psiconv_list_free(psiconv_list l);
41 49
43 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
44 automatically. */ 52 automatically. */
45extern 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));
46 54
47/* Return the number of allocated elements */ 55/* Return the number of allocated elements */
48extern int psiconv_list_length(const psiconv_list l); 56extern psiconv_u32 psiconv_list_length(const psiconv_list l);
49 57
50/* Return 1 if the list is empty, 0 if not */ 58/* Return 1 if the list is empty, 0 if not */
51extern int psiconv_list_is_empty(const psiconv_list l); 59extern int psiconv_list_is_empty(const psiconv_list l);
52 60
61/* Empty a list. Note this does not reclaim any memory space! */
62extern void psiconv_list_empty(psiconv_list l);
63
53/* 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
54 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
55 element memory space. */ 66 element memory space.
67 If indx is out of range, NULL is returned. */
56extern void * psiconv_list_get(const psiconv_list l, unsigned int indx); 68extern void * psiconv_list_get(const psiconv_list l, psiconv_u32 indx);
57 69
58/* 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
59 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
60 pointers. */ 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. */
61extern void 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);
62 83
63/* 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
64 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,
65 but never try a free(el)! */ 86 but never try a free(el)! */
66extern 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));
67 88
68/* Clone the list, that is, copy it. If elements contain pointers, you 89/* Clone the list, that is, copy it. If elements contain pointers, you
69 should call the next routine. */ 90 should call the next routine. If not enough memory is available,
91 NULL is returned. */
70extern psiconv_list psiconv_list_clone(const psiconv_list l); 92extern 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. */
74extern psiconv_list psiconv_list_clone_el(const psiconv_list l,
75 void clone_el(void *el));
76 93
77/* Read upto size_t elements from file f, and put them at the end of list l. 94/* 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 95 Returned is the actual number of elements added. This assumes the file
79 layout and the memory layout of elements is the same. */ 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. */
80extern 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);
81 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
112
113#ifdef __cplusplus
114}
115#endif /* __cplusplus */
116
82#endif 117#endif

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

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