Krolyxon
Commit a732e75

Branches

Refs →

nsxiva732e75

Check all given files before open the first

Author
Bert
Date
2011-01-20 16:24 +0100
Parent
004f297
Commit
a732e75d8a484c9e9a1c281d93124580fad83dd4

+33 −9

3 files changed

  1. M image.c +8 −4
  2. M image.h +1 −1
  3. M main.c +24 −4
M image.c View
@@ -39,22 +39,26 @@ void imlib_destroy() {
3939 imlib_free_image();
4040 }
4141
42−void img_load(img_t *img, const char *filename) {
42+int img_load(img_t *img, const char *filename) {
4343 Imlib_Image *im;
4444
4545 if (!img || !filename)
46− return;
46+ return -1;
4747
4848 if (imlib_context_get_image())
4949 imlib_free_image();
5050
51− if (!(im = imlib_load_image(filename)))
52− DIE("could not open image: %s", filename);
51+ if (!(im = imlib_load_image(filename))) {
52+ WARN("could not open image: %s", filename);
53+ return -1;
54+ }
5355
5456 imlib_context_set_image(im);
5557
5658 img->w = imlib_image_get_width();
5759 img->h = imlib_image_get_height();
60+
61+ return 0;
5862 }
5963
6064 void img_display(img_t *img, win_t *win) {
M image.h View
@@ -39,7 +39,7 @@ typedef struct img_s {
3939 void imlib_init(win_t*);
4040 void imlib_destroy();
4141
42−void img_load(img_t*, const char*);
42+int img_load(img_t*, const char*);
4343 void img_display(img_t*, win_t*);
4444 void img_render(img_t*, win_t*, int, int, int, int);
4545
M main.c View
@@ -17,6 +17,7 @@
1717 */
1818
1919 #include <stdlib.h>
20+#include <stdio.h>
2021
2122 #include <X11/Xlib.h>
2223 #include <X11/keysym.h>
@@ -38,6 +39,9 @@ static void (*handler[LASTEvent])(XEvent*) = {
3839
3940 img_t img;
4041 win_t win;
42+
43+const char **filenames;
44+unsigned int filecnt;
4145 unsigned int fileidx;
4246
4347 void run() {
@@ -50,6 +54,8 @@ void run() {
5054 }
5155
5256 int main(int argc, char **argv) {
57+ int i;
58+
5359 parse_options(argc, argv);
5460
5561 if (!options->filecnt) {
@@ -57,7 +63,21 @@ int main(int argc, char **argv) {
5763 exit(1);
5864 }
5965
66+ if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
67+ DIE("could not allocate memory");
68+
6069 fileidx = 0;
70+ filecnt = 0;
71+
72+ for (i = 0; i < options->filecnt; ++i) {
73+ if (!(img_load(&img, options->filenames[i]) < 0))
74+ filenames[filecnt++] = options->filenames[i];
75+ }
76+
77+ if (!filecnt) {
78+ fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
79+ exit(1);
80+ }
6181
6282 img.zoom = 1.0;
6383 img.scalemode = SCALE_MODE;
@@ -68,7 +88,7 @@ int main(int argc, char **argv) {
6888 win_open(&win);
6989 imlib_init(&win);
7090
71− img_load(&img, options->filenames[fileidx]);
91+ img_load(&img, filenames[fileidx]);
7292 img_display(&img, &win);
7393
7494 run();
@@ -104,15 +124,15 @@ void on_keypress(XEvent *ev) {
104124 exit(0);
105125 case XK_n:
106126 case XK_space:
107− if (fileidx + 1 < options->filecnt) {
108− img_load(&img, options->filenames[++fileidx]);
127+ if (fileidx + 1 < filecnt) {
128+ img_load(&img, filenames[++fileidx]);
109129 img_display(&img, &win);
110130 }
111131 break;
112132 case XK_p:
113133 case XK_BackSpace:
114134 if (fileidx > 0) {
115− img_load(&img, options->filenames[--fileidx]);
135+ img_load(&img, filenames[--fileidx]);
116136 img_display(&img, &win);
117137 }
118138 break;