From 0b0ab9ff96f078e2d952206093c554849741dffa Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 17 Mar 2014 11:46:17 +0100 Subject: libsmartcols: add iterator Signed-off-by: Karel Zak --- libsmartcols/src/Makemodule.am | 1 + libsmartcols/src/iter.c | 73 ++++++++++++++++++++++++++++++++++++++ libsmartcols/src/libsmartcols.h.in | 14 ++++++++ libsmartcols/src/smartcolsP.h | 9 +++++ 4 files changed, 97 insertions(+) create mode 100644 libsmartcols/src/iter.c (limited to 'libsmartcols/src') diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am index f942b6789..3dbac9ca1 100644 --- a/libsmartcols/src/Makemodule.am +++ b/libsmartcols/src/Makemodule.am @@ -9,6 +9,7 @@ libsmartcols_la_SOURCES= \ include/list.h \ \ libsmartcols/src/smartcolsP.h \ + libsmartcols/src/iter.c \ $(smartcolsinc_HEADERS) nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h diff --git a/libsmartcols/src/iter.c b/libsmartcols/src/iter.c new file mode 100644 index 000000000..d9b33da7c --- /dev/null +++ b/libsmartcols/src/iter.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2009-2014 Karel Zak + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + */ + +/** + * SECTION: iter + * @title: Iterator + * @short_description: unified iterator + * + * The iterator keeps the direction and the last position + * for access to the internal library tables/lists. + */ +#include +#include +#include + +#include "smartcolsP.h" + +/** + * scols_new_iter: + * @direction: SCOLS_INTER_{FOR,BACK}WARD direction + * + * Returns: newly allocated generic libmount iterator. + */ +struct libscols_iter *scols_new_iter(int direction) +{ + struct libscols_iter *itr = calloc(1, sizeof(*itr)); + if (!itr) + return NULL; + itr->direction = direction; + return itr; +} + +/** + * scols_free_iter: + * @itr: iterator pointer + * + * Deallocates the iterator. + */ +void scols_free_iter(struct libscols_iter *itr) +{ + free(itr); +} + +/** + * scols_reset_iter: + * @itr: iterator pointer + * @direction: SCOLS_INTER_{FOR,BACK}WARD or -1 to keep the direction unchanged + * + * Resets the iterator. + */ +void scols_reset_iter(struct libscols_iter *itr, int direction) +{ + if (direction == -1) + direction = itr->direction; + + memset(itr, 0, sizeof(*itr)); + itr->direction = direction; +} + +/** + * scols_iter_get_direction: + * @itr: iterator pointer + * + * Returns: SCOLS_INTER_{FOR,BACK}WARD + */ +int scols_iter_get_direction(struct libscols_iter *itr) +{ + return itr->direction; +} diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 84bbb825f..46a06a0ce 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -19,6 +19,20 @@ extern "C" { #define LIBSMARTCOLS_VERSION "@LIBSMARTCOLS_VERSION@" +struct libscols_iter; + + +/* iter.c */ +enum { + + SCOLS_ITER_FORWARD = 0, + SCOLS_ITER_BACKWARD +}; + +extern struct libscols_iter *scols_new_iter(int direction); +extern void scols_free_iter(struct libscols_iter *itr); +extern void scols_reset_iter(struct libscols_iter *itr, int direction); +extern int scols_iter_get_direction(struct libscols_iter *itr); #ifdef __cplusplus } diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index fecf9e8c0..c42457580 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -24,4 +24,13 @@ # define assert(x) #endif +/* + * Generic iterator + */ +struct libscols_iter { + struct list_head *p; /* current position */ + struct list_head *head; /* start position */ + int direction; /* SCOLS_ITER_{FOR,BACK}WARD */ +}; + #endif /* _LIBSMARTCOLS_PRIVATE_H */ -- cgit v1.2.3-55-g7522