idmap.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Mon Mar 01 2004
00003     copyright   : (C) 2004-2010 by Martin Preuss
00004     email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *                                                                         *
00008  *   This library is free software; you can redistribute it and/or         *
00009  *   modify it under the terms of the GNU Lesser General Public            *
00010  *   License as published by the Free Software Foundation; either          *
00011  *   version 2.1 of the License, or (at your option) any later version.    *
00012  *                                                                         *
00013  *   This library is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00016  *   Lesser General Public License for more details.                       *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU Lesser General Public      *
00019  *   License along with this library; if not, write to the Free Software   *
00020  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00021  *   MA  02111-1307  USA                                                   *
00022  *                                                                         *
00023  ***************************************************************************/
00024 
00025 #ifndef GWENHYWFAR_IDMAP_H
00026 #define GWENHYWFAR_IDMAP_H
00027 
00028 
00029 #include <gwenhywfar/types.h>
00030 
00031 #include <stdio.h>
00032 
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036 
00037 
00038 typedef struct GWEN_IDMAP GWEN_IDMAP;
00039 
00040 typedef enum {
00041   GWEN_IdMapResult_Ok=0,
00042   GWEN_IdMapResult_NoFit,
00043   GWEN_IdMapResult_NotFound
00044 } GWEN_IDMAP_RESULT;
00045 
00046 
00047 typedef enum {
00048   GWEN_IdMapAlgo_Unknown=0,
00049   GWEN_IdMapAlgo_Hex4
00050 } GWEN_IDMAP_ALGO;
00051 
00052 
00057 #ifndef GWEN_DUMMY_EMPTY_ARG
00058 
00060 # define GWEN_DUMMY_EMPTY_ARG
00061 #endif
00062 
00063 
00064 #define GWEN_IDMAP_FUNCTION_LIB_DEFS(t, pr, decl) \
00065   typedef GWEN_IDMAP t##_IDMAP;                                       \
00066                                                                       \
00067   decl t##_IDMAP *pr##_IdMap_new(GWEN_IDMAP_ALGO algo);               \
00068   decl void pr##_IdMap_free(t##_IDMAP *l);                            \
00069   decl void pr##_IdMap_freeAll(t##_IDMAP *l);                         \
00070   decl void pr##_IdMap_FreeItems(t##_IDMAP *l);                       \
00071   decl GWEN_IDMAP_RESULT pr##_IdMap_Insert(t##_IDMAP *l,              \
00072                                            uint32_t id,       \
00073                                            t* ptr);                   \
00074   decl GWEN_IDMAP_RESULT pr##_IdMap_Remove(t##_IDMAP *l,              \
00075                                            uint32_t id);      \
00076   decl t* pr##_IdMap_Find(t##_IDMAP *l, uint32_t id);         \
00077   decl GWEN_IDMAP_RESULT pr##_IdMap_GetFirst(const t##_IDMAP *map,    \
00078                                               uint32_t *pid); \
00079   decl GWEN_IDMAP_RESULT pr##_IdMap_GetNext(const t##_IDMAP *map,     \
00080                                              uint32_t *pid);  \
00081   decl uint32_t pr##_IdMap_GetSize(const GWEN_IDMAP *map);    \
00082   decl void pr##_IdMap_Clear(GWEN_IDMAP *l);
00083 
00084 
00085 #define GWEN_IDMAP_FUNCTION_DEFS(t, pr) \
00086   GWEN_IDMAP_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
00087 
00088 
00089 #define GWEN_IDMAP_FUNCTIONS(t, pr) \
00090   t##_IDMAP *pr##_IdMap_new(GWEN_IDMAP_ALGO algo) {                  \
00091     return (t##_IDMAP*)GWEN_IdMap_new(algo);                         \
00092   }                                                                  \
00093                                                                      \
00094   void pr##_IdMap_free(t##_IDMAP *l) {                               \
00095     GWEN_IdMap_free((GWEN_IDMAP*)l);                                 \
00096   }                                                                  \
00097                                                                      \
00098   void pr##_IdMap_freeAll(t##_IDMAP *l) {                            \
00099     GWEN_IDMAP_RESULT res;                                           \
00100     uint32_t id;                                             \
00101                                                                      \
00102     res=pr##_IdMap_GetFirst(l, &id);                                 \
00103     while(res==GWEN_IdMapResult_Ok) {                                \
00104       uint32_t nextId;                                       \
00105       t *ptr;                                                        \
00106                                                                      \
00107       nextId=id;                                                     \
00108       res=pr##_IdMap_GetNext(l, &nextId);                            \
00109       ptr=pr##_IdMap_Find(l, id);                                    \
00110       if (ptr)                                                       \
00111         pr##_free(ptr);                                              \
00112       id=nextId;                                                     \
00113     }                                                                \
00114     pr##_IdMap_free(l);                                              \
00115   }                                                                  \
00116                                                                      \
00117   void pr##_IdMap_FreeItems(t##_IDMAP *l) {                          \
00118     GWEN_IDMAP_RESULT res;                                           \
00119     uint32_t id;                                             \
00120                                                                      \
00121     res=pr##_IdMap_GetFirst(l, &id);                                 \
00122     while(res==GWEN_IdMapResult_Ok) {                                \
00123       uint32_t nextId;                                       \
00124       t *ptr;                                                        \
00125                                                                      \
00126       nextId=id;                                                     \
00127       res=pr##_IdMap_GetNext(l, &nextId);                            \
00128       ptr=pr##_IdMap_Find(l, id);                                    \
00129       if (ptr)                                                       \
00130         pr##_free(ptr);                                              \
00131       pr##_IdMap_Remove(l, id);                                      \
00132       id=nextId;                                                     \
00133     }                                                                \
00134   }                                                                  \
00135                                                                      \
00136   GWEN_IDMAP_RESULT pr##_IdMap_Insert(t##_IDMAP *l,                  \
00137                                       uint32_t id,           \
00138                                       t* ptr) {                      \
00139     return GWEN_IdMap_Insert((GWEN_IDMAP*)l, id, (void*) ptr);       \
00140   }                                                                  \
00141                                                                      \
00142   GWEN_IDMAP_RESULT pr##_IdMap_Remove(t##_IDMAP *l,                  \
00143                                       uint32_t id){          \
00144     return GWEN_IdMap_Remove((GWEN_IDMAP*)l, id);                    \
00145   }                                                                  \
00146 \
00147   t* pr##_IdMap_Find(t##_IDMAP *l, uint32_t id) {            \
00148     return GWEN_IdMap_Find((GWEN_IDMAP*)l, id);                      \
00149   }                                                                  \
00150                                                                      \
00151   GWEN_IDMAP_RESULT pr##_IdMap_GetFirst(const t##_IDMAP *l,          \
00152                                         uint32_t *pid) {     \
00153     return GWEN_IdMap_GetFirst((const GWEN_IDMAP*)l, pid);           \
00154   }                                                                  \
00155                                                                      \
00156   GWEN_IDMAP_RESULT pr##_IdMap_GetNext(const t##_IDMAP *l,           \
00157                                        uint32_t *pid) {      \
00158     return GWEN_IdMap_GetNext((const GWEN_IDMAP*)l, pid);            \
00159   }                                                                  \
00160                                                                      \
00161   uint32_t pr##_IdMap_GetSize(const GWEN_IDMAP *l) {         \
00162     return GWEN_IdMap_GetSize((const GWEN_IDMAP*)l);                 \
00163   }                                                                  \
00164                                                                      \
00165   void pr##_IdMap_Clear(GWEN_IDMAP *l) {                             \
00166     GWEN_IdMap_Clear((GWEN_IDMAP*)l);                                \
00167   }
00168 
00172 GWENHYWFAR_API
00173 GWEN_IDMAP *GWEN_IdMap_new(GWEN_IDMAP_ALGO algo);
00174 
00175 GWENHYWFAR_API
00176 void GWEN_IdMap_free(GWEN_IDMAP *map);
00177 
00178 GWENHYWFAR_API
00179 GWEN_IDMAP_RESULT GWEN_IdMap_Insert(GWEN_IDMAP *map,
00180                                     uint32_t id,
00181                                     void *ptr);
00182 
00183 GWENHYWFAR_API
00184 GWEN_IDMAP_RESULT GWEN_IdMap_Remove(GWEN_IDMAP *map,
00185                                     uint32_t id);
00186 
00187 GWENHYWFAR_API
00188 void *GWEN_IdMap_Find(GWEN_IDMAP *map, uint32_t id);
00189 
00190 
00198 GWENHYWFAR_API
00199 GWEN_IDMAP_RESULT GWEN_IdMap_GetFirst(const GWEN_IDMAP *map,
00200                                       uint32_t *pid);
00201 
00209 GWENHYWFAR_API
00210 GWEN_IDMAP_RESULT GWEN_IdMap_GetNext(const GWEN_IDMAP *map,
00211                                      uint32_t *pid);
00212 
00213 GWENHYWFAR_API
00214 uint32_t GWEN_IdMap_GetSize(const GWEN_IDMAP *map);
00215 
00216 GWENHYWFAR_API
00217 void GWEN_IdMap_Clear(GWEN_IDMAP *map);
00218 
00219 
00220 GWENHYWFAR_API
00221 void GWEN_IdMap_Dump(GWEN_IDMAP *map, FILE *f, int indent);
00222 
00223 
00224 #ifdef __cplusplus
00225 }
00226 #endif
00227 
00228 
00229 #endif
00230