list1.h
Go to the documentation of this file.
/***************************************************************************
begin : Sat Jun 28 2003
copyright : (C) 2018 by Martin Preuss
email : martin@libchipcard.de
***************************************************************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
* MA 02111-1307 USA *
* *
***************************************************************************/
#include <gwenhywfar/gwenhywfarapi.h>
#include <gwenhywfar/types.h>
#include <assert.h>
#ifndef GWEN_DUMMY_EMPTY_ARG
# define GWEN_DUMMY_EMPTY_ARG
#endif
#ifndef GWEN_LIST1_H
#define GWEN_LIST1_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct GWEN_LIST1 GWEN_LIST1;
typedef struct GWEN_LIST1_ELEMENT GWEN_LIST1_ELEMENT;
typedef int GWENHYWFAR_CB(*GWEN_LIST1_SORT_FN)(const void *a, const void *b, int ascending);
GWENHYWFAR_API
GWEN_LIST1 *GWEN_List1_new(void);
GWENHYWFAR_API
void GWEN_List1_free(GWEN_LIST1 *l);
GWENHYWFAR_API
int GWEN_List1_GetCount(const GWEN_LIST1 *l);
GWENHYWFAR_API
int GWEN_List1_Add(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
int GWEN_List1_Insert(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
int GWEN_List1_Del(GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
int GWEN_List1_AddList(GWEN_LIST1 *dest, GWEN_LIST1 *l);
GWENHYWFAR_API
void *GWEN_List1_GetFirst(const GWEN_LIST1 *l);
GWENHYWFAR_API
void *GWEN_List1_GetLast(const GWEN_LIST1 *l);
GWENHYWFAR_API
GWEN_LIST1_SORT_FN GWEN_List1_SetSortFn(GWEN_LIST1 *l, GWEN_LIST1_SORT_FN fn);
GWENHYWFAR_API
void GWEN_List1_Sort(GWEN_LIST1 *l, int ascending);
GWENHYWFAR_API
GWEN_LIST1_ELEMENT *GWEN_List1Element_new(void *d);
GWENHYWFAR_API
void GWEN_List1Element_free(GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_List1Element_GetData(const GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_List1Element_GetPrevious(const GWEN_LIST1_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_List1Element_GetNext(const GWEN_LIST1_ELEMENT *el);
#define GWEN_LIST_ELEMENT(t) \
GWEN_LIST1_ELEMENT *_list1_element;
#define GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
/*typedef GWEN_LIST1 t##_LIST;*/ \
typedef struct t##_LIST t##_LIST; \
typedef int GWENHYWFAR_CB (*t##_LIST_SORT_FN)(const t *a, const t *b, int ascending); \
typedef t* (t##_LIST_FOREACH)(t *element, void *user_data); \
\
\
decl t* pr##_List_First(const t##_LIST *l); \
decl t* pr##_List_Last(const t##_LIST *l); \
decl t* pr##_List_Next(const t *element); \
decl t* pr##_List_Previous(const t *element); \
decl uint32_t pr##_List_GetCount(const t##_LIST *l); \
decl int pr##_List_HasElement(const t##_LIST *l, const t *element); \
decl t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn); \
decl void pr##_List_Sort(t##_LIST *l, int ascending); \
decl t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data);
#define GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
typedef GWEN_LIST1_ELEMENT t##_LIST_ELEMENT; \
\
decl void pr##_List_Clear(t##_LIST *l); \
decl t##_LIST* pr##_List_new(); \
decl void pr##_List_free(t##_LIST *l); \
decl int pr##_List_AddList(t##_LIST *dst, t##_LIST *l); \
decl int pr##_List_Add(t *element, t##_LIST *list); \
decl int pr##_List_Insert(t *element, t##_LIST *list); \
decl int pr##_List_Del(t *element);
#define GWEN_LIST_FUNCTION_DEFS_CONST(t, pr) \
GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_LIST_FUNCTION_DEFS_NOCONST(t, pr) \
GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, decl) \
GWEN_LIST_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
GWEN_LIST_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
#define GWEN_LIST_FUNCTION_DEFS(t, pr) \
GWEN_LIST_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_LIST_FUNCTIONS(t, pr) \
\
int pr##_List_Add(t *element, t##_LIST *l) { \
return GWEN_List1_Add((GWEN_LIST1*)l, element->_list1_element); \
} \
\
int pr##_List_AddList(t##_LIST *dst, t##_LIST *l) { \
return GWEN_List1_AddList((GWEN_LIST1*)dst, (GWEN_LIST1*)l); \
} \
\
int pr##_List_Insert(t *element, t##_LIST *l) { \
return GWEN_List1_Insert((GWEN_LIST1*)l, element->_list1_element); \
} \
\
int pr##_List_Del(t *element){ \
return GWEN_List1_Del(element->_list1_element); \
}\
\
t* pr##_List_First(const t##_LIST *l) { \
if (l) return (t*)GWEN_List1_GetFirst((GWEN_LIST1*)l);\
else return 0; \
} \
\
t* pr##_List_Last(const t##_LIST *l) { \
if (l) return (t*) GWEN_List1_GetLast((GWEN_LIST1*)l);\
else return 0; \
} \
\
void pr##_List_Clear(t##_LIST *l) { \
t* el; \
while( (el=(t*) GWEN_List1_GetFirst((GWEN_LIST1*)l)) ) {\
pr##_List_Del(el);\
pr##_free(el);\
} /* while */ \
} \
\
int pr##_List_HasElement(const t##_LIST *l, const t *element) { \
const t* el; \
el=(t*)GWEN_List1_GetFirst((GWEN_LIST1*)l); \
while(el) {\
if (el==element) \
return 1; \
el=(const t*)GWEN_List1Element_GetNext(el->_list1_element); \
} /* while */ \
return 0; \
} \
\
t##_LIST* pr##_List_new(){\
return (t##_LIST*)GWEN_List1_new(); \
}\
\
void pr##_List_free(t##_LIST *l) {\
if (l) { \
pr##_List_Clear(l);\
GWEN_List1_free((GWEN_LIST1*)l); \
}\
} \
\
t* pr##_List_Next(const t *element) { \
return (t*)GWEN_List1Element_GetNext(element->_list1_element);\
} \
\
t* pr##_List_Previous(const t *element) { \
return (t*)GWEN_List1Element_GetPrevious(element->_list1_element);\
} \
\
uint32_t pr##_List_GetCount(const t##_LIST *l){\
return GWEN_List1_GetCount((GWEN_LIST1*)l);\
} \
\
t##_LIST_SORT_FN pr##_List_SetSortFn(t##_LIST *l, t##_LIST_SORT_FN fn) { \
return (t##_LIST_SORT_FN) GWEN_List1_SetSortFn((GWEN_LIST1*)l, (GWEN_LIST1_SORT_FN) fn); \
} \
\
void pr##_List_Sort(t##_LIST *l, int ascending){\
GWEN_List1_Sort((GWEN_LIST1*)l, ascending);\
}\
\
t* pr##_List_ForEach(t##_LIST *l, t##_LIST_FOREACH fn, void *user_data){ \
t *el; \
if (!l) return 0; \
\
el=pr##_List_First(l); \
while(el) { \
t *elReturned; \
elReturned=fn(el, user_data); \
if (elReturned) { \
return elReturned; \
} \
el=pr##_List_Next(el); \
} \
return 0; \
}
#define GWEN_LIST_INIT(t, element) \
element->_list1_element=GWEN_List1Element_new(element);
#define GWEN_LIST_FINI(t, element) \
if (element && element->_list1_element) { \
GWEN_List1Element_free(element->_list1_element); \
element->_list1_element=0; \
}
/* defgroup */
#ifdef __cplusplus
}
#endif
#endif
gwenhywfarapi.h
GWENHYWFAR_API
#define GWENHYWFAR_API
Definition gwenhywfarapi.h:67
GWENHYWFAR_CB
#define GWENHYWFAR_CB
Definition gwenhywfarapi.h:89
GWEN_List1_free
GWENHYWFAR_API void GWEN_List1_free(GWEN_LIST1 *l)
GWEN_List1_new
GWENHYWFAR_API GWEN_LIST1 * GWEN_List1_new(void)
GWEN_List1_Insert
GWENHYWFAR_API int GWEN_List1_Insert(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el)
GWEN_List1_AddList
GWENHYWFAR_API int GWEN_List1_AddList(GWEN_LIST1 *dest, GWEN_LIST1 *l)
GWEN_List1_GetFirst
GWENHYWFAR_API void * GWEN_List1_GetFirst(const GWEN_LIST1 *l)
GWEN_List1_Del
GWENHYWFAR_API int GWEN_List1_Del(GWEN_LIST1_ELEMENT *el)
GWEN_List1_GetLast
GWENHYWFAR_API void * GWEN_List1_GetLast(const GWEN_LIST1 *l)
GWEN_LIST1_SORT_FN
int GWENHYWFAR_CB(* GWEN_LIST1_SORT_FN)(const void *a, const void *b, int ascending)
Definition list1.h:158
GWEN_List1_GetCount
GWENHYWFAR_API int GWEN_List1_GetCount(const GWEN_LIST1 *l)
GWEN_List1Element_new
GWENHYWFAR_API GWEN_LIST1_ELEMENT * GWEN_List1Element_new(void *d)
GWEN_LIST1
struct GWEN_LIST1 GWEN_LIST1
Definition list1.h:155
GWEN_List1_Add
GWENHYWFAR_API int GWEN_List1_Add(GWEN_LIST1 *l, GWEN_LIST1_ELEMENT *el)
GWEN_List1Element_GetData
GWENHYWFAR_API void * GWEN_List1Element_GetData(const GWEN_LIST1_ELEMENT *el)
GWEN_List1Element_GetPrevious
GWENHYWFAR_API void * GWEN_List1Element_GetPrevious(const GWEN_LIST1_ELEMENT *el)
GWEN_List1_SetSortFn
GWENHYWFAR_API GWEN_LIST1_SORT_FN GWEN_List1_SetSortFn(GWEN_LIST1 *l, GWEN_LIST1_SORT_FN fn)
GWEN_List1_Sort
GWENHYWFAR_API void GWEN_List1_Sort(GWEN_LIST1 *l, int ascending)
GWEN_List1Element_GetNext
GWENHYWFAR_API void * GWEN_List1Element_GetNext(const GWEN_LIST1_ELEMENT *el)
GWEN_List1Element_free
GWENHYWFAR_API void GWEN_List1Element_free(GWEN_LIST1_ELEMENT *el)
GWEN_LIST1_ELEMENT
struct GWEN_LIST1_ELEMENT GWEN_LIST1_ELEMENT
Definition list1.h:156
types.h