pcsc-lite 1.7.2
|
00001 /* 00002 * Copyright (c) 2007,2008 Mij <mij@bitchx.it> 00003 * 00004 * Permission to use, copy, modify, and distribute this software for any 00005 * purpose with or without fee is hereby granted, provided that the above 00006 * copyright notice and this permission notice appear in all copies. 00007 * 00008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 00009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 00011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 */ 00016 00017 00018 /* 00019 * SimCList library. See http://mij.oltrelinux.com/devel/simclist 00020 */ 00021 00022 00023 #ifndef SIMCLIST_H 00024 #define SIMCLIST_H 00025 00026 #ifdef __cplusplus 00027 extern "C" { 00028 #endif 00029 00030 #include <inttypes.h> 00031 #include <errno.h> 00032 #include <sys/types.h> 00033 00034 /* Be friend of both C90 and C99 compilers */ 00035 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 00036 /* "inline" and "restrict" are keywords */ 00037 #else 00038 # define inline /* inline */ 00039 # define restrict /* restrict */ 00040 #endif 00041 00042 00048 typedef int32_t list_hash_t; 00049 00050 #ifndef SIMCLIST_NO_DUMPRESTORE 00051 typedef struct { 00052 uint16_t version; /* dump version */ 00053 int64_t timestamp; /* when the list has been dumped, microseconds from UNIX epoch */ 00054 uint32_t list_size; 00055 uint32_t list_numels; 00056 list_hash_t list_hash; /* hash of the list when dumped, or 0 if invalid */ 00057 uint32_t dumpsize; 00058 int consistent; /* 1 if the dump is verified complete/consistent; 0 otherwise */ 00059 } list_dump_info_t; 00060 #endif 00061 00071 typedef int (*element_comparator)(const void *a, const void *b); 00072 00084 typedef int (*element_seeker)(const void *el, const void *indicator); 00085 00095 typedef size_t (*element_meter)(const void *el); 00096 00106 typedef list_hash_t (*element_hash_computer)(const void *el); 00107 00126 typedef void *(*element_serializer)(const void *restrict el, uint32_t *restrict serializ_len); 00127 00143 typedef void *(*element_unserializer)(const void *restrict data, uint32_t *restrict data_len); 00144 00145 /* [private-use] list entry -- olds actual user datum */ 00146 struct list_entry_s { 00147 void *data; 00148 00149 /* doubly-linked list service references */ 00150 struct list_entry_s *next; 00151 struct list_entry_s *prev; 00152 }; 00153 00154 /* [private-use] list attributes */ 00155 struct list_attributes_s { 00156 /* user-set routine for comparing list elements */ 00157 element_comparator comparator; 00158 /* user-set routing for seeking elements */ 00159 element_seeker seeker; 00160 /* user-set routine for determining the length of an element */ 00161 element_meter meter; 00162 int copy_data; 00163 /* user-set routine for computing the hash of an element */ 00164 element_hash_computer hasher; 00165 /* user-set routine for serializing an element */ 00166 element_serializer serializer; 00167 /* user-set routine for unserializing an element */ 00168 element_unserializer unserializer; 00169 }; 00170 00172 typedef struct { 00173 struct list_entry_s *head_sentinel; 00174 struct list_entry_s *tail_sentinel; 00175 struct list_entry_s *mid; 00176 00177 unsigned int numels; 00178 00179 /* array of spare elements */ 00180 struct list_entry_s **spareels; 00181 unsigned int spareelsnum; 00182 00183 #ifdef SIMCLIST_WITH_THREADS 00184 /* how many threads are currently running */ 00185 unsigned int threadcount; 00186 #endif 00187 00188 /* service variables for list iteration */ 00189 int iter_active; 00190 unsigned int iter_pos; 00191 struct list_entry_s *iter_curentry; 00192 00193 /* list attributes */ 00194 struct list_attributes_s attrs; 00195 } list_t; 00196 00203 int list_init(list_t *restrict l); 00204 00214 void list_destroy(list_t *restrict l); 00215 00228 int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun); 00229 00242 int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun); 00243 00274 int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data); 00275 00294 int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun); 00295 00315 int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun); 00316 00337 int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun); 00338 00349 int list_append(list_t *restrict l, const void *data); 00350 00361 int list_prepend(list_t *restrict l, const void *restrict data); 00362 00371 void *list_fetch(list_t *restrict l); 00372 00380 void *list_get_at(const list_t *restrict l, unsigned int pos); 00381 00394 void *list_get_max(const list_t *restrict l); 00395 00408 void *list_get_min(const list_t *restrict l); 00409 00417 void *list_extract_at(list_t *restrict l, unsigned int pos); 00418 00427 int list_insert_at(list_t *restrict l, const void *data, unsigned int pos); 00428 00444 int list_delete(list_t *restrict l, const void *data); 00445 00453 int list_delete_at(list_t *restrict l, unsigned int pos); 00454 00463 int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend); 00464 00476 int list_clear(list_t *restrict l); 00477 00484 unsigned int list_size(const list_t *restrict l); 00485 00494 int list_empty(const list_t *restrict l); 00495 00513 int list_locate(const list_t *restrict l, const void *data); 00514 00528 void *list_seek(list_t *restrict l, const void *indicator); 00529 00549 int list_contains(const list_t *restrict l, const void *data); 00550 00568 int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest); 00569 00585 int list_sort(list_t *restrict l, int versus); 00586 00597 int list_iterator_start(list_t *restrict l); 00598 00605 void *list_iterator_next(list_t *restrict l); 00606 00613 int list_iterator_hasnext(const list_t *restrict l); 00614 00621 int list_iterator_stop(list_t *restrict l); 00622 00631 int list_hash(const list_t *restrict l, list_hash_t *restrict hash); 00632 00633 #ifndef SIMCLIST_NO_DUMPRESTORE 00634 00649 int list_dump_getinfo_filedescriptor(int fd, list_dump_info_t *restrict info); 00650 00664 int list_dump_getinfo_file(const char *restrict filename, list_dump_info_t *restrict info); 00665 00700 int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len); 00701 00723 int list_dump_file(const list_t *restrict l, const char *restrict filename, size_t *restrict len); 00724 00743 int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len); 00744 00761 int list_restore_file(list_t *restrict l, const char *restrict filename, size_t *len); 00762 #endif 00763 00764 /* ready-made comparators, meters and hash computers */ 00765 /* comparator functions */ 00770 int list_comparator_int8_t(const void *a, const void *b); 00771 00776 int list_comparator_int16_t(const void *a, const void *b); 00777 00782 int list_comparator_int32_t(const void *a, const void *b); 00783 00788 int list_comparator_int64_t(const void *a, const void *b); 00789 00794 int list_comparator_uint8_t(const void *a, const void *b); 00795 00800 int list_comparator_uint16_t(const void *a, const void *b); 00801 00806 int list_comparator_uint32_t(const void *a, const void *b); 00807 00812 int list_comparator_uint64_t(const void *a, const void *b); 00813 00818 int list_comparator_float(const void *a, const void *b); 00819 00824 int list_comparator_double(const void *a, const void *b); 00825 00830 int list_comparator_string(const void *a, const void *b); 00831 00832 /* metric functions */ 00837 size_t list_meter_int8_t(const void *el); 00838 00843 size_t list_meter_int16_t(const void *el); 00844 00849 size_t list_meter_int32_t(const void *el); 00850 00855 size_t list_meter_int64_t(const void *el); 00856 00861 size_t list_meter_uint8_t(const void *el); 00862 00867 size_t list_meter_uint16_t(const void *el); 00868 00873 size_t list_meter_uint32_t(const void *el); 00874 00879 size_t list_meter_uint64_t(const void *el); 00880 00885 size_t list_meter_float(const void *el); 00886 00891 size_t list_meter_double(const void *el); 00892 00897 size_t list_meter_string(const void *el); 00898 00899 /* hash functions */ 00904 list_hash_t list_hashcomputer_int8_t(const void *el); 00905 00910 list_hash_t list_hashcomputer_int16_t(const void *el); 00911 00916 list_hash_t list_hashcomputer_int32_t(const void *el); 00917 00922 list_hash_t list_hashcomputer_int64_t(const void *el); 00923 00928 list_hash_t list_hashcomputer_uint8_t(const void *el); 00929 00934 list_hash_t list_hashcomputer_uint16_t(const void *el); 00935 00940 list_hash_t list_hashcomputer_uint32_t(const void *el); 00941 00946 list_hash_t list_hashcomputer_uint64_t(const void *el); 00947 00952 list_hash_t list_hashcomputer_float(const void *el); 00953 00958 list_hash_t list_hashcomputer_double(const void *el); 00959 00964 list_hash_t list_hashcomputer_string(const void *el); 00965 00966 #ifdef __cplusplus 00967 } 00968 #endif 00969 00970 #endif 00971