This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via c7b776f3628256d94d62b176ccf72743f832fa45 (commit) from b91ac3947efd9ef282fb01e5152b3190f5f1352e (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/c7b776f3628256d94d62b176ccf72743f832f...
commit c7b776f3628256d94d62b176ccf72743f832fa45 Author: Cyril Hrubis metan@ucw.cz Date: Thu Dec 20 17:30:00 2012 +0100
spiv: Add directory traversal code.
diff --git a/demos/spiv/image_list.c b/demos/spiv/image_list.c index 390275f..8ed726a 100644 --- a/demos/spiv/image_list.c +++ b/demos/spiv/image_list.c @@ -20,7 +20,13 @@ * * *****************************************************************************/
+#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> #include <stdlib.h> +#include <errno.h> +#include <string.h>
#include <core/GP_Debug.h>
@@ -35,29 +41,129 @@ struct image_list { /* path to the currently loaded image */ char path[1024]; int path_loaded:1; + + /* directory handling */ + int in_dir:1; + + int cur_file; + int max_file; + struct dirent **dir_files; };
+static int dir_cmp(const struct dirent **a, const struct dirent **b) +{ + return strcasecmp((*a)->d_name, (*b)->d_name); +} + +static int dir_filter(const struct dirent *d) +{ + /* Ignore some filenames */ + if (!strcmp(d->d_name, ".")) + return 0; + + if (!strcmp(d->d_name, "..")) + return 0; + + GP_DEBUG(4, "Adding file '%s'", d->d_name); + + + return 1; +} + +static void try_load_dir(struct image_list *self) +{ + struct stat sb; + const char *path = self->args[self->cur_arg]; + + if (stat(path, &sb)) { + GP_WARN("Failed to stat '%s': %s", path, strerror(errno)); + return; + } + + if (!(sb.st_mode & S_IFDIR)) + return; + + GP_DEBUG(1, "Loading directory '%s' content.", path); + + int ret = scandir(path, &self->dir_files, dir_filter, dir_cmp); + + if (self->max_file == -1) { + GP_WARN("Failed to scandir '%s': %s", path, strerror(errno)); + return; + } + + self->max_file = ret; + self->cur_file = 0; + self->in_dir = 1; +} + +static void exit_dir(struct image_list *self) +{ + int i; + + GP_DEBUG(1, "Freeing directory '%s' content.", + self->args[self->cur_arg]); + + for (i = 0; i < self->max_file; i++) + free(self->dir_files[i]); + + free(self->dir_files); + + self->in_dir = 0; +} + static void next_img(struct image_list *self) { + if (self->in_dir) { + if (++self->cur_file == self->max_file) { + exit_dir(self); + } else { + self->path_loaded = 0; + return; + } + } + if (++self->cur_arg == self->max_arg) self->cur_arg = 0; + + try_load_dir(self);
self->path_loaded = 0; }
static void prev_img(struct image_list *self) { + if (self->in_dir) { + if (self->cur_file-- == 0) { + exit_dir(self); + } else { + self->path_loaded = 0; + return; + } + } + if (self->cur_arg == 0) self->cur_arg = self->max_arg - 1; else self->cur_arg--;
+ try_load_dir(self); + self->path_loaded = 0; }
static void load_path(struct image_list *self) { - snprintf(self->path, sizeof(self->path), "%s", self->args[self->cur_arg]); + if (self->in_dir) { + //TODO: eliminate double / + snprintf(self->path, sizeof(self->path), "%s/%s", + self->args[self->cur_arg], + self->dir_files[self->cur_file]->d_name); + + } else { + snprintf(self->path, sizeof(self->path), "%s", + self->args[self->cur_arg]); + }
self->path_loaded = 1; } @@ -92,10 +198,15 @@ struct image_list *image_list_create(const char *args[]) self->cur_arg = 0; self->path_loaded = 0; + + self->dir_files = 0; + self->in_dir = 0;
self->max_arg = 0; while (args[++self->max_arg] != NULL);
+ try_load_dir(self); + return self; }
-----------------------------------------------------------------------
Summary of changes: demos/spiv/image_list.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 112 insertions(+), 1 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.