Refactored, new files util.[ch], C89

This commit is contained in:
Bert 2011-02-03 10:15:01 +01:00
parent f5c125b3ee
commit 8f4af658ae
8 changed files with 137 additions and 101 deletions

View File

@ -1,10 +1,10 @@
all: sxiv
VERSION=git-20110202
VERSION=git-20110203
CC?=gcc
PREFIX?=/usr/local
CFLAGS+= -std=c99 -Wall -pedantic -DVERSION=\"$(VERSION)\"
CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\"
LDFLAGS+=
LIBS+= -lX11 -lImlib2

View File

@ -16,13 +16,12 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <Imlib2.h>
#include "sxiv.h"
#include "config.h"
#include "image.h"
#include "options.h"
#include "util.h"
int zl_cnt;
float zoom_min;
@ -59,7 +58,7 @@ int _imlib_load_image(const char *filename) {
return 0;
if (!(im = imlib_load_image(filename))) {
WARN("could not open image: %s", filename);
warn("could not open image: %s", filename);
return 0;
}

60
main.c
View File

@ -27,13 +27,11 @@
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include "sxiv.h"
#include "image.h"
#include "options.h"
#include "util.h"
#include "window.h"
void* s_malloc(size_t);
void* s_realloc(void*, size_t);
void on_keypress(XEvent*);
void on_buttonpress(XEvent*);
void on_buttonrelease(XEvent*);
@ -44,13 +42,7 @@ void update_title();
void check_append(const char*);
void read_dir_rec(const char*);
static void (*handler[LASTEvent])(XEvent*) = {
[KeyPress] = on_keypress,
[ButtonPress] = on_buttonpress,
[ButtonRelease] = on_buttonrelease,
[MotionNotify] = on_motionnotify,
[ConfigureNotify] = on_configurenotify
};
static void (*handler[LASTEvent])(XEvent*);
img_t img;
win_t win;
@ -68,12 +60,27 @@ int moy;
#define TITLE_LEN 256
char win_title[TITLE_LEN];
void cleanup() {
static int in = 0;
if (!in++) {
img_free(&img);
win_close(&win);
}
}
void run() {
int xfd;
fd_set fds;
struct timeval t;
XEvent ev;
handler[KeyPress] = on_keypress;
handler[ButtonPress] = on_buttonpress;
handler[ButtonRelease] = on_buttonrelease;
handler[MotionNotify] = on_motionnotify;
handler[ConfigureNotify] = on_configurenotify;
timeout = 0;
while (1) {
@ -118,12 +125,12 @@ int main(int argc, char **argv) {
for (i = 0; i < options->filecnt; ++i) {
filename = options->filenames[i];
if (stat(filename, &fstats)) {
WARN("could not stat file: %s", filename);
warn("could not stat file: %s", filename);
} else if (S_ISDIR(fstats.st_mode)) {
if (options->recursive)
read_dir_rec(filename);
else
WARN("ignoring directory: %s", filename);
warn("ignoring directory: %s", filename);
} else {
check_append(filename);
}
@ -151,15 +158,6 @@ int main(int argc, char **argv) {
return 0;
}
void cleanup() {
static int in = 0;
if (!in++) {
img_free(&img);
win_close(&win);
}
}
void on_keypress(XEvent *ev) {
char key;
KeySym ksym;
@ -417,7 +415,7 @@ void read_dir_rec(const char *dirname) {
while (diridx > 0) {
dirname = dirnames[--diridx];
if (!(dir = opendir(dirname)))
DIE("could not open directory: %s", dirname);
die("could not open directory: %s", dirname);
while ((dentry = readdir(dir))) {
if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
continue;
@ -425,7 +423,7 @@ void read_dir_rec(const char *dirname) {
filename = (char*) s_malloc(len * sizeof(char));
snprintf(filename, len, "%s/%s", dirname, dentry->d_name);
if (stat(filename, &fstats)) {
WARN("could not stat file: %s", filename);
warn("could not stat file: %s", filename);
free(filename);
} else if (S_ISDIR(fstats.st_mode)) {
if (diridx == dircnt) {
@ -447,17 +445,3 @@ void read_dir_rec(const char *dirname) {
free(dirnames);
}
void* s_malloc(size_t size) {
void *ptr;
if (!(ptr = malloc(size)))
DIE("could not allocate memory");
return ptr;
}
void* s_realloc(void *ptr, size_t size) {
if (!(ptr = realloc(ptr, size)))
DIE("could not allocate memory");
return ptr;
}

View File

@ -22,7 +22,7 @@
#include <stdio.h>
#include <unistd.h>
#include "sxiv.h"
#include "config.h"
#include "options.h"
options_t _options;

49
sxiv.h
View File

@ -1,49 +0,0 @@
/* sxiv: sxiv.h
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef SXIV_H
#define SXIV_H
#include "config.h"
#include "options.h"
#define ABS(a) ((a) < 0 ? (-(a)) : (a))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define WARN(...) \
do { \
if (!options->quiet) { \
fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} \
} while (0)
#define DIE(...) \
do { \
fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
cleanup(); \
exit(1); \
} while (0)
void cleanup();
#endif /* SXIV_H */

68
util.c Normal file
View File

@ -0,0 +1,68 @@
/* sxiv: util.c
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include "options.h"
#include "util.h"
void cleanup();
void* s_malloc(size_t size) {
void *ptr;
if (!(ptr = malloc(size)))
die("could not allocate memory");
return ptr;
}
void* s_realloc(void *ptr, size_t size) {
if (!(ptr = realloc(ptr, size)))
die("could not allocate memory");
return ptr;
}
void warn(const char* fmt, ...) {
va_list args;
if (!fmt || options->quiet)
return;
va_start(args, fmt);
fprintf(stderr, "sxiv: warning: ");
fprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
void die(const char* fmt, ...) {
va_list args;
if (!fmt)
return;
va_start(args, fmt);
fprintf(stderr, "sxiv: error: ");
fprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
cleanup();
exit(1);
}

34
util.h Normal file
View File

@ -0,0 +1,34 @@
/* sxiv: util.h
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef UTIL_H
#define UTIL_H
#include <stdarg.h>
#define ABS(a) ((a) < 0 ? (-(a)) : (a))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
void* s_malloc(size_t);
void* s_realloc(void*, size_t);
void warn(const char*, ...);
void die(const char*, ...);
#endif /* UTIL_H */

View File

@ -16,14 +16,14 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include "sxiv.h"
#include "config.h"
#include "options.h"
#include "util.h"
#include "window.h"
static Cursor arrow;
@ -43,7 +43,7 @@ void win_open(win_t *win) {
e = &win->env;
if (!(e->dpy = XOpenDisplay(NULL)))
DIE("could not open display");
die("could not open display");
e->scr = DefaultScreen(e->dpy);
e->scrw = DisplayWidth(e->dpy, e->scr);
@ -54,7 +54,7 @@ void win_open(win_t *win) {
if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
&bgcol, &bgcol))
DIE("could not allocate color: %s", BG_COLOR);
die("could not allocate color: %s", BG_COLOR);
win->bgcol = bgcol.pixel;
win->pm = 0;
@ -88,7 +88,7 @@ void win_open(win_t *win) {
win->x, win->y, win->w, win->h, 0,
e->depth, InputOutput, e->vis, 0, None);
if (win->xwin == None)
DIE("could not create window");
die("could not create window");
XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
ButtonPressMask | ButtonReleaseMask | Button2MotionMask);