core: move iterator definitions to a separate file
authorBartosz Golaszewski <bartekgola@gmail.com>
Sat, 24 Jun 2017 11:05:34 +0000 (13:05 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 6 Jul 2017 09:11:36 +0000 (11:11 +0200)
Move all iterator-related code to iter.c.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
src/lib/Makefile.am
src/lib/core.c
src/lib/iter.c [new file with mode: 0644]

index dbcd8a431c538710d809ac78e0e1e3ef775a38b9..9128f64b5769ffc665f99fa86aea4ed0e4522625 100644 (file)
@@ -7,7 +7,7 @@
 #
 
 lib_LTLIBRARIES = libgpiod.la
-libgpiod_la_SOURCES = core.c simple.c
+libgpiod_la_SOURCES = core.c iter.c simple.c
 libgpiod_la_CFLAGS = -Wall -Wextra -g
 libgpiod_la_CFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/
 libgpiod_la_CFLAGS += -include $(top_builddir)/config.h
index 3864f944926c6f9119bb21894e94b3b59694ccce..59c525cdec0c3d2b710dcc535752b1346b7083d0 100644 (file)
@@ -51,19 +51,6 @@ struct gpiod_line {
        };
 };
 
-enum {
-       CHIP_ITER_INIT = 0,
-       CHIP_ITER_DONE,
-       CHIP_ITER_ERR,
-};
-
-struct gpiod_chip_iter {
-       DIR *dir;
-       struct gpiod_chip *current;
-       int state;
-       char *failed_chip;
-};
-
 static const char dev_dir[] = "/dev/";
 static const char cdev_prefix[] = "gpiochip";
 
@@ -899,118 +886,6 @@ struct gpiod_chip * gpiod_line_get_chip(struct gpiod_line *line)
        return line->chip;
 }
 
-struct gpiod_chip_iter * gpiod_chip_iter_new(void)
-{
-       struct gpiod_chip_iter *new;
-
-       new = malloc(sizeof(*new));
-       if (!new)
-               return NULL;
-
-       memset(new, 0, sizeof(*new));
-
-       new->dir = opendir(dev_dir);
-       if (!new->dir)
-               return NULL;
-
-       new->state = CHIP_ITER_INIT;
-
-       return new;
-}
-
-void gpiod_chip_iter_free(struct gpiod_chip_iter *iter)
-{
-       if (iter->current)
-               gpiod_chip_close(iter->current);
-
-       gpiod_chip_iter_free_noclose(iter);
-}
-
-void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter)
-{
-       closedir(iter->dir);
-       if (iter->failed_chip)
-               free(iter->failed_chip);
-       free(iter);
-}
-
-struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter)
-{
-       if (iter->current) {
-               gpiod_chip_close(iter->current);
-               iter->current = NULL;
-       }
-
-       return gpiod_chip_iter_next_noclose(iter);
-}
-
-struct gpiod_chip * gpiod_chip_iter_next_noclose(struct gpiod_chip_iter *iter)
-{
-       struct gpiod_chip *chip;
-       struct dirent *dentry;
-       int status;
-
-       for (dentry = readdir(iter->dir);
-            dentry;
-            dentry = readdir(iter->dir)) {
-               status = strncmp(dentry->d_name, cdev_prefix,
-                                sizeof(cdev_prefix) - 1);
-               if (status == 0) {
-                       iter->state = CHIP_ITER_INIT;
-                       if (iter->failed_chip) {
-                               free(iter->failed_chip);
-                               iter->failed_chip = NULL;
-                       }
-
-                       chip = gpiod_chip_open_by_name(dentry->d_name);
-                       if (!chip) {
-                               iter->state = CHIP_ITER_ERR;
-                               iter->failed_chip = strdup(dentry->d_name);
-                               /* No point in an error check here. */
-                       }
-
-                       iter->current = chip;
-                       return iter->current;
-               }
-       }
-
-       iter->state = CHIP_ITER_DONE;
-       return NULL;
-}
-
-bool gpiod_chip_iter_done(struct gpiod_chip_iter *iter)
-{
-       return iter->state == CHIP_ITER_DONE;
-}
-
-bool gpiod_chip_iter_err(struct gpiod_chip_iter *iter)
-{
-       return iter->state == CHIP_ITER_ERR;
-}
-
-const char *
-gpiod_chip_iter_failed_chip(struct gpiod_chip_iter *iter)
-{
-       return iter->failed_chip;
-}
-
-struct gpiod_line * gpiod_line_iter_next(struct gpiod_line_iter *iter)
-{
-       struct gpiod_line *line;
-
-       if (iter->offset >= gpiod_chip_num_lines(iter->chip)) {
-               iter->state = GPIOD_LINE_ITER_DONE;
-               return NULL;
-       }
-
-       iter->state = GPIOD_LINE_ITER_INIT;
-       line = gpiod_chip_get_line(iter->chip, iter->offset++);
-       if (!line)
-               iter->state = GPIOD_LINE_ITER_ERR;
-
-       return line;
-}
-
 const char * gpiod_version_string(void)
 {
        return GPIOD_VERSION_STR;
diff --git a/src/lib/iter.c b/src/lib/iter.c
new file mode 100644 (file)
index 0000000..e81bf71
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * GPIO chip and line iterators for libgpiod.
+ *
+ * Copyright (C) 2017 Bartosz Golaszewski <bartekgola@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ */
+
+#include <gpiod.h>
+
+#include <string.h>
+#include <dirent.h>
+
+enum {
+       CHIP_ITER_INIT = 0,
+       CHIP_ITER_DONE,
+       CHIP_ITER_ERR,
+};
+
+struct gpiod_chip_iter {
+       DIR *dir;
+       struct gpiod_chip *current;
+       int state;
+       char *failed_chip;
+};
+
+struct gpiod_chip_iter * gpiod_chip_iter_new(void)
+{
+       struct gpiod_chip_iter *new;
+
+       new = malloc(sizeof(*new));
+       if (!new)
+               return NULL;
+
+       memset(new, 0, sizeof(*new));
+
+       new->dir = opendir("/dev");
+       if (!new->dir)
+               return NULL;
+
+       new->state = CHIP_ITER_INIT;
+
+       return new;
+}
+
+void gpiod_chip_iter_free(struct gpiod_chip_iter *iter)
+{
+       if (iter->current)
+               gpiod_chip_close(iter->current);
+
+       gpiod_chip_iter_free_noclose(iter);
+}
+
+void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter)
+{
+       closedir(iter->dir);
+       if (iter->failed_chip)
+               free(iter->failed_chip);
+       free(iter);
+}
+
+struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter)
+{
+       if (iter->current) {
+               gpiod_chip_close(iter->current);
+               iter->current = NULL;
+       }
+
+       return gpiod_chip_iter_next_noclose(iter);
+}
+
+struct gpiod_chip * gpiod_chip_iter_next_noclose(struct gpiod_chip_iter *iter)
+{
+       struct gpiod_chip *chip;
+       struct dirent *dentry;
+       int status;
+
+       for (dentry = readdir(iter->dir);
+            dentry;
+            dentry = readdir(iter->dir)) {
+               status = strncmp(dentry->d_name, "gpiochip", 8);
+               if (status == 0) {
+                       iter->state = CHIP_ITER_INIT;
+                       if (iter->failed_chip) {
+                               free(iter->failed_chip);
+                               iter->failed_chip = NULL;
+                       }
+
+                       chip = gpiod_chip_open_by_name(dentry->d_name);
+                       if (!chip) {
+                               iter->state = CHIP_ITER_ERR;
+                               iter->failed_chip = strdup(dentry->d_name);
+                               /* No point in an error check here. */
+                       }
+
+                       iter->current = chip;
+                       return iter->current;
+               }
+       }
+
+       iter->state = CHIP_ITER_DONE;
+       return NULL;
+}
+
+bool gpiod_chip_iter_done(struct gpiod_chip_iter *iter)
+{
+       return iter->state == CHIP_ITER_DONE;
+}
+
+bool gpiod_chip_iter_err(struct gpiod_chip_iter *iter)
+{
+       return iter->state == CHIP_ITER_ERR;
+}
+
+const char *
+gpiod_chip_iter_failed_chip(struct gpiod_chip_iter *iter)
+{
+       return iter->failed_chip;
+}
+
+struct gpiod_line * gpiod_line_iter_next(struct gpiod_line_iter *iter)
+{
+       struct gpiod_line *line;
+
+       if (iter->offset >= gpiod_chip_num_lines(iter->chip)) {
+               iter->state = GPIOD_LINE_ITER_DONE;
+               return NULL;
+       }
+
+       iter->state = GPIOD_LINE_ITER_INIT;
+       line = gpiod_chip_get_line(iter->chip, iter->offset++);
+       if (!line)
+               iter->state = GPIOD_LINE_ITER_ERR;
+
+       return line;
+}