]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add iterator
authorOndrej Oprala <ooprala@redhat.com>
Mon, 17 Mar 2014 10:46:17 +0000 (11:46 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Apr 2014 10:29:15 +0000 (12:29 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/Makemodule.am
libsmartcols/src/iter.c [new file with mode: 0644]
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/smartcolsP.h

index f942b67895a48a42fca634ecece4a39640b09000..3dbac9ca1e1fa7f1117d77be982590ed5032062f 100644 (file)
@@ -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 (file)
index 0000000..d9b33da
--- /dev/null
@@ -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;
+}
index 84bbb825fc075f0b943bd2e13bcc0ebf35eaae38..46a06a0ce635e4b3e99d001e67d5b3aef19cecea 100644 (file)
@@ -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
 }
index fecf9e8c0ec6157243edfd2ab3ba3d1a1741d400..c424575805cab4270145916c69e0cdbee4c3ac8d 100644 (file)
 # 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 */