code-style: general cleanups (#137)

* tns_clean_cache: remove unused function arg

* remove malloc casting

* improve consistency

use sizeof(T) at the end

* avoid comparing integers of different signedness

* use Window type for embed and parent

* remove unnecessary comparisons

* remove cpp style comments

* improve consistency: remove comma from the end of enumerator list

* Removed useless _IMAGE_CONFIG defines

* consistency: use the same order as snprintf

* Resolve c89 warnings


Co-authored-by: uidops <uidops@protonmail.com>
Co-authored-by: Arthur Williams <taaparthur@gmail.com>
This commit is contained in:
N-R-K 2021-10-29 02:00:53 +06:00 committed by GitHub
parent 03eb664e89
commit 850bc788c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 42 additions and 45 deletions

View File

@ -18,8 +18,6 @@
*/ */
#include "nsxiv.h" #include "nsxiv.h"
#define _IMAGE_CONFIG
#include "config.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

16
image.c
View File

@ -135,8 +135,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
if (img->multi.cap == 0) { if (img->multi.cap == 0) {
img->multi.cap = 8; img->multi.cap = 8;
img->multi.frames = (img_frame_t*) img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
emalloc(sizeof(img_frame_t) * img->multi.cap);
} }
img->multi.cnt = img->multi.sel = 0; img->multi.cnt = img->multi.sel = 0;
img->multi.length = 0; img->multi.length = 0;
@ -188,9 +187,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
w = gif->Image.Width; w = gif->Image.Width;
h = gif->Image.Height; h = gif->Image.Height;
rows = (GifRowType*) emalloc(h * sizeof(GifRowType)); rows = emalloc(h * sizeof(GifRowType));
for (i = 0; i < h; i++) for (i = 0; i < h; i++)
rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType)); rows[i] = emalloc(w * sizeof(GifPixelType));
if (gif->Image.Interlace) { if (gif->Image.Interlace) {
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
for (j = intoffset[i]; j < h; j += intjump[i]) for (j = intoffset[i]; j < h; j += intjump[i])
@ -201,7 +200,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
DGifGetLine(gif, rows[i], w); DGifGetLine(gif, rows[i], w);
} }
ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh); ptr = data = emalloc(sw * sh * sizeof(DATA32));
cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
if (bg >= cmap->ColorCount) { if (bg >= cmap->ColorCount) {
err = true; err = true;
@ -258,8 +257,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
if (img->multi.cnt == img->multi.cap) { if (img->multi.cnt == img->multi.cap) {
img->multi.cap *= 2; img->multi.cap *= 2;
img->multi.frames = (img_frame_t*) img->multi.frames = erealloc(img->multi.frames,
erealloc(img->multi.frames,
img->multi.cap * sizeof(img_frame_t)); img->multi.cap * sizeof(img_frame_t));
} }
img->multi.frames[img->multi.cnt].im = im; img->multi.frames[img->multi.cnt].im = im;
@ -322,7 +320,6 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
{ {
FILE *webp_file; FILE *webp_file;
WebPData data; WebPData data;
data.bytes = NULL;
Imlib_Image im = NULL; Imlib_Image im = NULL;
struct WebPAnimDecoderOptions opts; struct WebPAnimDecoderOptions opts;
@ -335,6 +332,7 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
unsigned long flags; unsigned long flags;
unsigned int delay; unsigned int delay;
bool err = false; bool err = false;
data.bytes = NULL;
if ((err = fframe == NULL && img == NULL)) if ((err = fframe == NULL && img == NULL))
goto fail; goto fail;
@ -863,7 +861,7 @@ bool img_change_gamma(img_t *img, int d)
if (img->gamma != gamma) { if (img->gamma != gamma) {
imlib_reset_color_modifier(); imlib_reset_color_modifier();
if (gamma != 0) { if (gamma) {
range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0; range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE)); imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
} }

22
main.c
View File

@ -169,7 +169,7 @@ void remove_file(int n, bool manual)
void set_timeout(timeout_f handler, int time, bool overwrite) void set_timeout(timeout_f handler, int time, bool overwrite)
{ {
int i; unsigned int i;
for (i = 0; i < ARRLEN(timeouts); i++) { for (i = 0; i < ARRLEN(timeouts); i++) {
if (timeouts[i].handler == handler) { if (timeouts[i].handler == handler) {
@ -185,7 +185,7 @@ void set_timeout(timeout_f handler, int time, bool overwrite)
void reset_timeout(timeout_f handler) void reset_timeout(timeout_f handler)
{ {
int i; unsigned int i;
for (i = 0; i < ARRLEN(timeouts); i++) { for (i = 0; i < ARRLEN(timeouts); i++) {
if (timeouts[i].handler == handler) { if (timeouts[i].handler == handler) {
@ -234,7 +234,7 @@ void open_info(void)
int pfd[2]; int pfd[2];
char w[12], h[12]; char w[12], h[12];
if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0) if (info.f.err || info.fd >= 0 || win.bar.h == 0)
return; return;
win.bar.l.buf[0] = '\0'; win.bar.l.buf[0] = '\0';
if (pipe(pfd) < 0) if (pipe(pfd) < 0)
@ -379,7 +379,7 @@ void update_info(void)
else else
bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10); bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
} }
if (img.gamma != 0) if (img.gamma)
bar_put(r, "G%+d" BAR_SEP, img.gamma); bar_put(r, "G%+d" BAR_SEP, img.gamma);
bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0)); bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
if (img.multi.cnt > 0) { if (img.multi.cnt > 0) {
@ -423,7 +423,8 @@ void redraw(void)
void reset_cursor(void) void reset_cursor(void)
{ {
int c, i; int c;
unsigned int i;
cursor_t cursor = CURSOR_NONE; cursor_t cursor = CURSOR_NONE;
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
@ -499,7 +500,7 @@ void run_key_handler(const char *key, unsigned int mask)
struct stat *oldst, st; struct stat *oldst, st;
XEvent dump; XEvent dump;
if (keyhandler.f.err != 0) { if (keyhandler.f.err) {
if (!keyhandler.warned) { if (!keyhandler.warned) {
error(0, keyhandler.f.err, "%s", keyhandler.f.cmd); error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
keyhandler.warned = true; keyhandler.warned = true;
@ -586,7 +587,7 @@ end:
void on_keypress(XKeyEvent *kev) void on_keypress(XKeyEvent *kev)
{ {
int i; unsigned int i;
unsigned int sh = 0; unsigned int sh = 0;
KeySym ksym, shksym; KeySym ksym, shksym;
char dummy, key; char dummy, key;
@ -629,7 +630,8 @@ void on_keypress(XKeyEvent *kev)
void on_buttonpress(XButtonEvent *bev) void on_buttonpress(XButtonEvent *bev)
{ {
int i, sel; int sel;
unsigned int i;
bool dirty = false; bool dirty = false;
static Time firstclick; static Time firstclick;
@ -855,7 +857,7 @@ int main(int argc, char *argv[])
if (options->clean_cache) { if (options->clean_cache) {
tns_init(&tns, NULL, NULL, NULL, NULL); tns_init(&tns, NULL, NULL, NULL, NULL);
tns_clean_cache(&tns); tns_clean_cache();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -929,7 +931,7 @@ int main(int argc, char *argv[])
const char *s = "/nsxiv/exec/"; const char *s = "/nsxiv/exec/";
for (i = 0; i < ARRLEN(cmd); i++) { for (i = 0; i < ARRLEN(cmd); i++) {
n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + strlen(s) + 1; n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1;
cmd[i]->cmd = emalloc(n); cmd[i]->cmd = emalloc(n);
snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]); snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);
if (access(cmd[i]->cmd, X_OK) != 0) if (access(cmd[i]->cmd, X_OK) != 0)

View File

@ -119,7 +119,7 @@ typedef enum {
typedef enum { typedef enum {
SUFFIX_EMPTY, SUFFIX_EMPTY,
SUFFIX_BASENAME, SUFFIX_BASENAME,
SUFFIX_FULLPATH, SUFFIX_FULLPATH
} suffixmode_t; } suffixmode_t;
typedef struct { typedef struct {
@ -198,7 +198,7 @@ typedef struct {
int cnt; int cnt;
int sel; int sel;
bool animate; bool animate;
int framedelay; unsigned int framedelay;
int length; int length;
} multi_img_t; } multi_img_t;
@ -272,7 +272,7 @@ struct opt {
/* window: */ /* window: */
bool fullscreen; bool fullscreen;
bool hide_bar; bool hide_bar;
long embed; Window embed; /* unsigned long */
char *geometry; char *geometry;
char *res_name; char *res_name;
const char *title_prefix; const char *title_prefix;
@ -324,7 +324,7 @@ struct tns {
bool dirty; bool dirty;
}; };
void tns_clean_cache(tns_t*); void tns_clean_cache(void);
void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*); void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
CLEANUP void tns_free(tns_t*); CLEANUP void tns_free(tns_t*);
bool tns_load(tns_t*, int, bool, bool); bool tns_load(tns_t*, int, bool, bool);

View File

@ -18,7 +18,6 @@
*/ */
#include "nsxiv.h" #include "nsxiv.h"
#define _IMAGE_CONFIG
#define _TITLE_CONFIG #define _TITLE_CONFIG
#include "config.h" #include "config.h"
#include "version.h" #include "version.h"

View File

@ -49,7 +49,7 @@ char* tns_cache_filepath(const char *filepath)
if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
/* don't cache images inside the cache directory! */ /* don't cache images inside the cache directory! */
len = strlen(cache_dir) + strlen(filepath) + 2; len = strlen(cache_dir) + strlen(filepath) + 2;
cfile = (char*) emalloc(len); cfile = emalloc(len);
snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
} }
return cfile; return cfile;
@ -120,7 +120,7 @@ end:
} }
} }
void tns_clean_cache(tns_t *tns) void tns_clean_cache(void)
{ {
int dirlen; int dirlen;
char *cfile, *filename; char *cfile, *filename;
@ -150,7 +150,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
const char *homedir, *dsuffix = ""; const char *homedir, *dsuffix = "";
if (cnt != NULL && *cnt > 0) { if (cnt != NULL && *cnt > 0) {
tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t)); tns->thumbs = emalloc(*cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
} else { } else {
tns->thumbs = NULL; tns->thumbs = NULL;
@ -174,7 +174,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
const char *s = "/nsxiv"; const char *s = "/nsxiv";
free(cache_dir); free(cache_dir);
len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1; len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1;
cache_dir = (char*) emalloc(len); cache_dir = emalloc(len);
snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s); snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s);
} else { } else {
error(0, 0, "Cache directory not found"); error(0, 0, "Cache directory not found");

8
utf8.h
View File

@ -53,13 +53,13 @@ utf8_decode(void *buf, uint32_t *c, int *e)
*c >>= shiftc[len]; *c >>= shiftc[len];
/* Accumulate the various error conditions. */ /* Accumulate the various error conditions. */
*e = (*c < mins[len]) << 6; // non-canonical encoding *e = (*c < mins[len]) << 6; /* non-canonical encoding */
*e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? *e |= ((*c >> 11) == 0x1b) << 7; /* surrogate half? */
*e |= (*c > 0x10FFFF) << 8; // out of range? *e |= (*c > 0x10FFFF) << 8; /* out of range? */
*e |= (s[1] & 0xc0) >> 2; *e |= (s[1] & 0xc0) >> 2;
*e |= (s[2] & 0xc0) >> 4; *e |= (s[2] & 0xc0) >> 4;
*e |= (s[3] ) >> 6; *e |= (s[3] ) >> 6;
*e ^= 0x2a; // top two bits of each tail byte correct? *e ^= 0x2a; /* top two bits of each tail byte correct? */
*e >>= shifte[len]; *e >>= shifte[len];
return next; return next;

2
util.c
View File

@ -82,7 +82,7 @@ void error(int eval, int err, const char* fmt, ...)
void size_readable(float *size, const char **unit) void size_readable(float *size, const char **unit)
{ {
const char *units[] = { "", "K", "M", "G" }; const char *units[] = { "", "K", "M", "G" };
int i; unsigned int i;
for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++) for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
*size /= 1024.0; *size /= 1024.0;

View File

@ -172,7 +172,7 @@ void win_init(win_t *win)
void win_open(win_t *win) void win_open(win_t *win)
{ {
int c, i, j, n; int c, i, j, n;
long parent; Window parent;
win_env_t *e; win_env_t *e;
XClassHint classhint; XClassHint classhint;
unsigned long *icon_data; unsigned long *icon_data;
@ -188,7 +188,7 @@ void win_open(win_t *win)
XSetWindowAttributes attrs; XSetWindowAttributes attrs;
e = &win->env; e = &win->env;
parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr); parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr);
sizehints.flags = PWinGravity; sizehints.flags = PWinGravity;
sizehints.win_gravity = NorthWestGravity; sizehints.win_gravity = NorthWestGravity;
@ -199,16 +199,16 @@ void win_open(win_t *win)
else else
gmask = XParseGeometry(options->geometry, &win->x, &win->y, gmask = XParseGeometry(options->geometry, &win->x, &win->y,
&win->w, &win->h); &win->w, &win->h);
if ((gmask & WidthValue) != 0) if (gmask & WidthValue)
sizehints.flags |= USSize; sizehints.flags |= USSize;
else else
win->w = WIN_WIDTH; win->w = WIN_WIDTH;
if ((gmask & HeightValue) != 0) if (gmask & HeightValue)
sizehints.flags |= USSize; sizehints.flags |= USSize;
else else
win->h = WIN_HEIGHT; win->h = WIN_HEIGHT;
if ((gmask & XValue) != 0) { if (gmask & XValue) {
if ((gmask & XNegative) != 0) { if (gmask & XNegative) {
win->x += e->scrw - win->w; win->x += e->scrw - win->w;
sizehints.win_gravity = NorthEastGravity; sizehints.win_gravity = NorthEastGravity;
} }
@ -216,8 +216,8 @@ void win_open(win_t *win)
} else { } else {
win->x = 0; win->x = 0;
} }
if ((gmask & YValue) != 0) { if (gmask & YValue) {
if ((gmask & YNegative) != 0) { if (gmask & YNegative) {
win->y += e->scrh - win->h; win->y += e->scrh - win->h;
sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
? SouthEastGravity : SouthWestGravity; ? SouthEastGravity : SouthWestGravity;
@ -322,7 +322,7 @@ void win_open(win_t *win)
CLEANUP void win_close(win_t *win) CLEANUP void win_close(win_t *win)
{ {
int i; unsigned int i;
for (i = 0; i < ARRLEN(cursors); i++) for (i = 0; i < ARRLEN(cursors); i++)
XFreeCursor(win->env.dpy, cursors[i].icon); XFreeCursor(win->env.dpy, cursors[i].icon);
@ -360,7 +360,7 @@ void win_toggle_fullscreen(win_t *win)
cm->window = win->xwin; cm->window = win->xwin;
cm->message_type = atoms[ATOM__NET_WM_STATE]; cm->message_type = atoms[ATOM__NET_WM_STATE];
cm->format = 32; cm->format = 32;
cm->data.l[0] = 2; // toggle cm->data.l[0] = 2; /* toggle */
cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN]; cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False, XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,