Krolyxon
Commit 8f4af65

Branches

Refs →

nsxiv8f4af65

Refactored, new files util.[ch], C89

Author
Bert
Date
2011-02-03 10:15 +0100
Parent
f5c125b
Commit
8f4af658ae484c3090dbd5b5b9b498943b3eb959

+137 −101

8 files changed

  1. M Makefile +2 −2
  2. M image.c +4 −5
  3. M main.c +22 −38
  4. M options.c +1 −1
  5. D sxiv.h +0 −49
  6. A util.c +68 −0
  7. A util.h +34 −0
  8. M window.c +6 −6
M Makefile View
@@ -1,10 +1,10 @@
11 all: sxiv
22
3−VERSION=git-20110202
3+VERSION=git-20110203
44
55 CC?=gcc
66 PREFIX?=/usr/local
7−CFLAGS+= -std=c99 -Wall -pedantic -DVERSION=\"$(VERSION)\"
7+CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\"
88 LDFLAGS+=
99 LIBS+= -lX11 -lImlib2
1010
M image.c View
@@ -16,13 +16,12 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19−#include <stdlib.h>
20−#include <stdio.h>
21−
2219 #include <Imlib2.h>
2320
24−#include "sxiv.h"
21+#include "config.h"
2522 #include "image.h"
23+#include "options.h"
24+#include "util.h"
2625
2726 int zl_cnt;
2827 float zoom_min;
@@ -59,7 +58,7 @@ int _imlib_load_image(const char *filename) {
5958 return 0;
6059
6160 if (!(im = imlib_load_image(filename))) {
62− WARN("could not open image: %s", filename);
61+ warn("could not open image: %s", filename);
6362 return 0;
6463 }
6564
M main.c View
@@ -27,13 +27,11 @@
2727 #include <X11/Xutil.h>
2828 #include <X11/keysym.h>
2929
30−#include "sxiv.h"
3130 #include "image.h"
31+#include "options.h"
32+#include "util.h"
3233 #include "window.h"
3334
34−void* s_malloc(size_t);
35−void* s_realloc(void*, size_t);
36−
3735 void on_keypress(XEvent*);
3836 void on_buttonpress(XEvent*);
3937 void on_buttonrelease(XEvent*);
@@ -44,13 +42,7 @@ void update_title();
4442 void check_append(const char*);
4543 void read_dir_rec(const char*);
4644
47−static void (*handler[LASTEvent])(XEvent*) = {
48− [KeyPress] = on_keypress,
49− [ButtonPress] = on_buttonpress,
50− [ButtonRelease] = on_buttonrelease,
51− [MotionNotify] = on_motionnotify,
52− [ConfigureNotify] = on_configurenotify
53−};
45+static void (*handler[LASTEvent])(XEvent*);
5446
5547 img_t img;
5648 win_t win;
@@ -68,12 +60,27 @@ int moy;
6860 #define TITLE_LEN 256
6961 char win_title[TITLE_LEN];
7062
63+void cleanup() {
64+ static int in = 0;
65+
66+ if (!in++) {
67+ img_free(&img);
68+ win_close(&win);
69+ }
70+}
71+
7172 void run() {
7273 int xfd;
7374 fd_set fds;
7475 struct timeval t;
7576 XEvent ev;
7677
78+ handler[KeyPress] = on_keypress;
79+ handler[ButtonPress] = on_buttonpress;
80+ handler[ButtonRelease] = on_buttonrelease;
81+ handler[MotionNotify] = on_motionnotify;
82+ handler[ConfigureNotify] = on_configurenotify;
83+
7784 timeout = 0;
7885
7986 while (1) {
@@ -118,12 +125,12 @@ int main(int argc, char **argv) {
118125 for (i = 0; i < options->filecnt; ++i) {
119126 filename = options->filenames[i];
120127 if (stat(filename, &fstats)) {
121− WARN("could not stat file: %s", filename);
128+ warn("could not stat file: %s", filename);
122129 } else if (S_ISDIR(fstats.st_mode)) {
123130 if (options->recursive)
124131 read_dir_rec(filename);
125132 else
126− WARN("ignoring directory: %s", filename);
133+ warn("ignoring directory: %s", filename);
127134 } else {
128135 check_append(filename);
129136 }
@@ -151,15 +158,6 @@ int main(int argc, char **argv) {
151158 return 0;
152159 }
153160
154−void cleanup() {
155− static int in = 0;
156−
157− if (!in++) {
158− img_free(&img);
159− win_close(&win);
160− }
161−}
162−
163161 void on_keypress(XEvent *ev) {
164162 char key;
165163 KeySym ksym;
@@ -417,7 +415,7 @@ void read_dir_rec(const char *dirname) {
417415 while (diridx > 0) {
418416 dirname = dirnames[--diridx];
419417 if (!(dir = opendir(dirname)))
420− DIE("could not open directory: %s", dirname);
418+ die("could not open directory: %s", dirname);
421419 while ((dentry = readdir(dir))) {
422420 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
423421 continue;
@@ -425,7 +423,7 @@ void read_dir_rec(const char *dirname) {
425423 filename = (char*) s_malloc(len * sizeof(char));
426424 snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
427425 if (stat(filename, &fstats)) {
428− WARN("could not stat file: %s", filename);
426+ warn("could not stat file: %s", filename);
429427 free(filename);
430428 } else if (S_ISDIR(fstats.st_mode)) {
431429 if (diridx == dircnt) {
@@ -447,17 +445,3 @@ void read_dir_rec(const char *dirname) {
447445
448446 free(dirnames);
449447 }
450−
451−void* s_malloc(size_t size) {
452− void *ptr;
453−
454− if (!(ptr = malloc(size)))
455− DIE("could not allocate memory");
456− return ptr;
457−}
458−
459−void* s_realloc(void *ptr, size_t size) {
460− if (!(ptr = realloc(ptr, size)))
461− DIE("could not allocate memory");
462− return ptr;
463−}
M options.c View
@@ -22,7 +22,7 @@
2222 #include <stdio.h>
2323 #include <unistd.h>
2424
25−#include "sxiv.h"
25+#include "config.h"
2626 #include "options.h"
2727
2828 options_t _options;
D sxiv.h View
@@ -1,49 +0,0 @@
1−/* sxiv: sxiv.h
2− * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3− *
4− * This program is free software; you can redistribute it and/or modify
5− * it under the terms of the GNU General Public License as published by
6− * the Free Software Foundation; either version 2 of the License, or
7− * (at your option) any later version.
8− *
9− * This program is distributed in the hope that it will be useful,
10− * but WITHOUT ANY WARRANTY; without even the implied warranty of
11− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12− * GNU General Public License for more details.
13− *
14− * You should have received a copy of the GNU General Public License
15− * along with this program; if not, write to the Free Software
16− * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17− */
18−
19−#ifndef SXIV_H
20−#define SXIV_H
21−
22−#include "config.h"
23−#include "options.h"
24−
25−#define ABS(a) ((a) < 0 ? (-(a)) : (a))
26−#define MIN(a,b) ((a) < (b) ? (a) : (b))
27−#define MAX(a,b) ((a) > (b) ? (a) : (b))
28−
29−#define WARN(...) \
30− do { \
31− if (!options->quiet) { \
32− fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
33− fprintf(stderr, __VA_ARGS__); \
34− fprintf(stderr, "\n"); \
35− } \
36− } while (0)
37−
38−#define DIE(...) \
39− do { \
40− fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
41− fprintf(stderr, __VA_ARGS__); \
42− fprintf(stderr, "\n"); \
43− cleanup(); \
44− exit(1); \
45− } while (0)
46−
47−void cleanup();
48−
49−#endif /* SXIV_H */
A util.c View
@@ -0,0 +1,68 @@
1+/* sxiv: util.c
2+ * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, write to the Free Software
16+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+ */
18+
19+#include <stdlib.h>
20+#include <stdio.h>
21+
22+#include "options.h"
23+#include "util.h"
24+
25+void cleanup();
26+
27+void* s_malloc(size_t size) {
28+ void *ptr;
29+
30+ if (!(ptr = malloc(size)))
31+ die("could not allocate memory");
32+ return ptr;
33+}
34+
35+void* s_realloc(void *ptr, size_t size) {
36+ if (!(ptr = realloc(ptr, size)))
37+ die("could not allocate memory");
38+ return ptr;
39+}
40+
41+void warn(const char* fmt, ...) {
42+ va_list args;
43+
44+ if (!fmt || options->quiet)
45+ return;
46+
47+ va_start(args, fmt);
48+ fprintf(stderr, "sxiv: warning: ");
49+ fprintf(stderr, fmt, args);
50+ fprintf(stderr, "\n");
51+ va_end(args);
52+}
53+
54+void die(const char* fmt, ...) {
55+ va_list args;
56+
57+ if (!fmt)
58+ return;
59+
60+ va_start(args, fmt);
61+ fprintf(stderr, "sxiv: error: ");
62+ fprintf(stderr, fmt, args);
63+ fprintf(stderr, "\n");
64+ va_end(args);
65+
66+ cleanup();
67+ exit(1);
68+}
A util.h View
@@ -0,0 +1,34 @@
1+/* sxiv: util.h
2+ * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, write to the Free Software
16+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+ */
18+
19+#ifndef UTIL_H
20+#define UTIL_H
21+
22+#include <stdarg.h>
23+
24+#define ABS(a) ((a) < 0 ? (-(a)) : (a))
25+#define MIN(a,b) ((a) < (b) ? (a) : (b))
26+#define MAX(a,b) ((a) > (b) ? (a) : (b))
27+
28+void* s_malloc(size_t);
29+void* s_realloc(void*, size_t);
30+
31+void warn(const char*, ...);
32+void die(const char*, ...);
33+
34+#endif /* UTIL_H */
M window.c View
@@ -16,14 +16,14 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19−#include <stdlib.h>
20−#include <stdio.h>
2119 #include <string.h>
2220
2321 #include <X11/Xutil.h>
2422 #include <X11/cursorfont.h>
2523
26−#include "sxiv.h"
24+#include "config.h"
25+#include "options.h"
26+#include "util.h"
2727 #include "window.h"
2828
2929 static Cursor arrow;
@@ -43,7 +43,7 @@ void win_open(win_t *win) {
4343
4444 e = &win->env;
4545 if (!(e->dpy = XOpenDisplay(NULL)))
46− DIE("could not open display");
46+ die("could not open display");
4747
4848 e->scr = DefaultScreen(e->dpy);
4949 e->scrw = DisplayWidth(e->dpy, e->scr);
@@ -54,7 +54,7 @@ void win_open(win_t *win) {
5454
5555 if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
5656 &bgcol, &bgcol))
57− DIE("could not allocate color: %s", BG_COLOR);
57+ die("could not allocate color: %s", BG_COLOR);
5858
5959 win->bgcol = bgcol.pixel;
6060 win->pm = 0;
@@ -88,7 +88,7 @@ void win_open(win_t *win) {
8888 win->x, win->y, win->w, win->h, 0,
8989 e->depth, InputOutput, e->vis, 0, None);
9090 if (win->xwin == None)
91− DIE("could not create window");
91+ die("could not create window");
9292
9393 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
9494 ButtonPressMask | ButtonReleaseMask | Button2MotionMask);