summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src
diff options
context:
space:
mode:
authorOndrej Oprala2014-03-17 11:46:17 +0100
committerKarel Zak2014-04-03 12:29:15 +0200
commit0b0ab9ff96f078e2d952206093c554849741dffa (patch)
treed0a84560975245b867dd9adae5b214d27a265711 /libsmartcols/src
parentlibsmartcols: add basic files (diff)
downloadkernel-qcow2-util-linux-0b0ab9ff96f078e2d952206093c554849741dffa.tar.gz
kernel-qcow2-util-linux-0b0ab9ff96f078e2d952206093c554849741dffa.tar.xz
kernel-qcow2-util-linux-0b0ab9ff96f078e2d952206093c554849741dffa.zip
libsmartcols: add iterator
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src')
-rw-r--r--libsmartcols/src/Makemodule.am1
-rw-r--r--libsmartcols/src/iter.c73
-rw-r--r--libsmartcols/src/libsmartcols.h.in14
-rw-r--r--libsmartcols/src/smartcolsP.h9
4 files changed, 97 insertions, 0 deletions
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 <kzak@redhat.com>
+ *
+ * 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#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 */