pcsc-lite  1.8.20
simclist.h
1 /*
2  * Copyright (c) 2007,2008 Mij <mij@bitchx.it>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 
18 /*
19  * SimCList library. See http://mij.oltrelinux.com/devel/simclist
20  */
21 
22 
23 #ifndef SIMCLIST_H
24 #define SIMCLIST_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <inttypes.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 
34 #ifndef SIMCLIST_NO_DUMPRESTORE
35 # ifndef _WIN32
36 # include <sys/time.h> /* list_dump_info_t's struct timeval */
37 # else
38 # include <time.h>
39 # endif
40 #endif
41 
42 
43 /* Be friend of both C90 and C99 compilers */
44 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
45  /* "inline" and "restrict" are keywords */
46 #else
47 # define inline /* inline */
48 # define restrict /* restrict */
49 #endif
50 
51 
57 typedef int32_t list_hash_t;
58 
59 #ifndef SIMCLIST_NO_DUMPRESTORE
60 typedef struct {
61  uint16_t version; /* dump version */
62  struct timeval timestamp; /* when the list has been dumped, seconds since UNIX epoch */
63  uint32_t list_size;
64  uint32_t list_numels;
65  list_hash_t list_hash; /* hash of the list when dumped, or 0 if invalid */
66  uint32_t dumpsize;
67  int consistent; /* 1 if the dump is verified complete/consistent; 0 otherwise */
69 #endif
70 
80 typedef int (*element_comparator)(const void *a, const void *b);
81 
93 typedef int (*element_seeker)(const void *el, const void *indicator);
94 
104 typedef size_t (*element_meter)(const void *el);
105 
115 typedef list_hash_t (*element_hash_computer)(const void *el);
116 
135 typedef void *(*element_serializer)(const void *restrict el, uint32_t *restrict serializ_len);
136 
152 typedef void *(*element_unserializer)(const void *restrict data, uint32_t *restrict data_len);
153 
154 /* [private-use] list entry -- olds actual user datum */
155 struct list_entry_s {
156  void *data;
157 
158  /* doubly-linked list service references */
159  struct list_entry_s *next;
160  struct list_entry_s *prev;
161 };
162 
163 /* [private-use] list attributes */
165  /* user-set routine for comparing list elements */
166  element_comparator comparator;
167  /* user-set routing for seeking elements */
168  element_seeker seeker;
169  /* user-set routine for determining the length of an element */
170  element_meter meter;
171  int copy_data;
172  /* user-set routine for computing the hash of an element */
173  element_hash_computer hasher;
174  /* user-set routine for serializing an element */
175  element_serializer serializer;
176  /* user-set routine for unserializing an element */
177  element_unserializer unserializer;
178 };
179 
181 typedef struct {
182  struct list_entry_s *head_sentinel;
183  struct list_entry_s *tail_sentinel;
184  struct list_entry_s *mid;
185 
186  unsigned int numels;
187 
188  /* array of spare elements */
189  struct list_entry_s **spareels;
190  unsigned int spareelsnum;
191 
192 #ifdef SIMCLIST_WITH_THREADS
193  /* how many threads are currently running */
194  unsigned int threadcount;
195 #endif
196 
197  /* service variables for list iteration */
198  int iter_active;
199  unsigned int iter_pos;
200  struct list_entry_s *iter_curentry;
201 
202  /* list attributes */
203  struct list_attributes_s attrs;
204 } list_t;
205 
212 int list_init(list_t *restrict l);
213 
223 void list_destroy(list_t *restrict l);
224 
237 int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun);
238 
251 int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun);
252 
283 int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data);
284 
303 int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun);
304 
324 int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun);
325 
346 int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun);
347 
358 int list_append(list_t *restrict l, const void *data);
359 
370 int list_prepend(list_t *restrict l, const void *restrict data);
371 
380 void *list_fetch(list_t *restrict l);
381 
389 void *list_get_at(const list_t *restrict l, unsigned int pos);
390 
403 void *list_get_max(const list_t *restrict l);
404 
417 void *list_get_min(const list_t *restrict l);
418 
426 void *list_extract_at(list_t *restrict l, unsigned int pos);
427 
436 int list_insert_at(list_t *restrict l, const void *data, unsigned int pos);
437 
453 int list_delete(list_t *restrict l, const void *data);
454 
462 int list_delete_at(list_t *restrict l, unsigned int pos);
463 
472 int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend);
473 
485 int list_clear(list_t *restrict l);
486 
493 unsigned int list_size(const list_t *restrict l);
494 
503 int list_empty(const list_t *restrict l);
504 
522 int list_locate(const list_t *restrict l, const void *data);
523 
537 void *list_seek(list_t *restrict l, const void *indicator);
538 
558 int list_contains(const list_t *restrict l, const void *data);
559 
577 int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest);
578 
594 int list_sort(list_t *restrict l, int versus);
595 
606 int list_iterator_start(list_t *restrict l);
607 
614 void *list_iterator_next(list_t *restrict l);
615 
622 int list_iterator_hasnext(const list_t *restrict l);
623 
630 int list_iterator_stop(list_t *restrict l);
631 
640 int list_hash(const list_t *restrict l, list_hash_t *restrict hash);
641 
642 #ifndef SIMCLIST_NO_DUMPRESTORE
643 
658 int list_dump_getinfo_filedescriptor(int fd, list_dump_info_t *restrict info);
659 
673 int list_dump_getinfo_file(const char *restrict filename, list_dump_info_t *restrict info);
674 
709 int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len);
710 
732 int list_dump_file(const list_t *restrict l, const char *restrict filename, size_t *restrict len);
733 
752 int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len);
753 
770 int list_restore_file(list_t *restrict l, const char *restrict filename, size_t *len);
771 #endif
772 
773 /* ready-made comparators, meters and hash computers */
774  /* comparator functions */
779 int list_comparator_int8_t(const void *a, const void *b);
780 
785 int list_comparator_int16_t(const void *a, const void *b);
786 
791 int list_comparator_int32_t(const void *a, const void *b);
792 
797 int list_comparator_int64_t(const void *a, const void *b);
798 
803 int list_comparator_uint8_t(const void *a, const void *b);
804 
809 int list_comparator_uint16_t(const void *a, const void *b);
810 
815 int list_comparator_uint32_t(const void *a, const void *b);
816 
821 int list_comparator_uint64_t(const void *a, const void *b);
822 
827 int list_comparator_float(const void *a, const void *b);
828 
833 int list_comparator_double(const void *a, const void *b);
834 
839 int list_comparator_string(const void *a, const void *b);
840 
841  /* metric functions */
846 size_t list_meter_int8_t(const void *el);
847 
852 size_t list_meter_int16_t(const void *el);
853 
858 size_t list_meter_int32_t(const void *el);
859 
864 size_t list_meter_int64_t(const void *el);
865 
870 size_t list_meter_uint8_t(const void *el);
871 
876 size_t list_meter_uint16_t(const void *el);
877 
882 size_t list_meter_uint32_t(const void *el);
883 
888 size_t list_meter_uint64_t(const void *el);
889 
894 size_t list_meter_float(const void *el);
895 
900 size_t list_meter_double(const void *el);
901 
906 size_t list_meter_string(const void *el);
907 
908  /* hash functions */
913 list_hash_t list_hashcomputer_int8_t(const void *el);
914 
919 list_hash_t list_hashcomputer_int16_t(const void *el);
920 
925 list_hash_t list_hashcomputer_int32_t(const void *el);
926 
931 list_hash_t list_hashcomputer_int64_t(const void *el);
932 
937 list_hash_t list_hashcomputer_uint8_t(const void *el);
938 
943 list_hash_t list_hashcomputer_uint16_t(const void *el);
944 
949 list_hash_t list_hashcomputer_uint32_t(const void *el);
950 
955 list_hash_t list_hashcomputer_uint64_t(const void *el);
956 
961 list_hash_t list_hashcomputer_float(const void *el);
962 
967 list_hash_t list_hashcomputer_double(const void *el);
968 
973 list_hash_t list_hashcomputer_string(const void *el);
974 
975 #ifdef __cplusplus
976 }
977 #endif
978 
979 #endif
980 
list object
Definition: simclist.h:181
Definition: simclist.h:155