From a732e75d8a484c9e9a1c281d93124580fad83dd4 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 20 Jan 2011 16:24:48 +0100 Subject: Check all given files before open the first --- image.c | 12 ++++++++---- image.h | 2 +- main.c | 28 ++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/image.c b/image.c index 953b1c2..7b0df5d 100644 --- a/image.c +++ b/image.c @@ -39,22 +39,26 @@ void imlib_destroy() { imlib_free_image(); } -void img_load(img_t *img, const char *filename) { +int img_load(img_t *img, const char *filename) { Imlib_Image *im; if (!img || !filename) - return; + return -1; if (imlib_context_get_image()) imlib_free_image(); - if (!(im = imlib_load_image(filename))) - DIE("could not open image: %s", filename); + if (!(im = imlib_load_image(filename))) { + WARN("could not open image: %s", filename); + return -1; + } imlib_context_set_image(im); img->w = imlib_image_get_width(); img->h = imlib_image_get_height(); + + return 0; } void img_display(img_t *img, win_t *win) { diff --git a/image.h b/image.h index c3e3c10..a889704 100644 --- a/image.h +++ b/image.h @@ -39,7 +39,7 @@ typedef struct img_s { void imlib_init(win_t*); void imlib_destroy(); -void img_load(img_t*, const char*); +int img_load(img_t*, const char*); void img_display(img_t*, win_t*); void img_render(img_t*, win_t*, int, int, int, int); diff --git a/main.c b/main.c index 42cecb5..08189c2 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -38,6 +39,9 @@ static void (*handler[LASTEvent])(XEvent*) = { img_t img; win_t win; + +const char **filenames; +unsigned int filecnt; unsigned int fileidx; void run() { @@ -50,6 +54,8 @@ void run() { } int main(int argc, char **argv) { + int i; + parse_options(argc, argv); if (!options->filecnt) { @@ -57,7 +63,21 @@ int main(int argc, char **argv) { exit(1); } + if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) + DIE("could not allocate memory"); + fileidx = 0; + filecnt = 0; + + for (i = 0; i < options->filecnt; ++i) { + if (!(img_load(&img, options->filenames[i]) < 0)) + filenames[filecnt++] = options->filenames[i]; + } + + if (!filecnt) { + fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); + exit(1); + } img.zoom = 1.0; img.scalemode = SCALE_MODE; @@ -68,7 +88,7 @@ int main(int argc, char **argv) { win_open(&win); imlib_init(&win); - img_load(&img, options->filenames[fileidx]); + img_load(&img, filenames[fileidx]); img_display(&img, &win); run(); @@ -104,15 +124,15 @@ void on_keypress(XEvent *ev) { exit(0); case XK_n: case XK_space: - if (fileidx + 1 < options->filecnt) { - img_load(&img, options->filenames[++fileidx]); + if (fileidx + 1 < filecnt) { + img_load(&img, filenames[++fileidx]); img_display(&img, &win); } break; case XK_p: case XK_BackSpace: if (fileidx > 0) { - img_load(&img, options->filenames[--fileidx]); + img_load(&img, filenames[--fileidx]); img_display(&img, &win); } break; -- cgit v1.2.3-54-g00ecf