From 850bc788c3fd4bb96f425ceedfe4237754cabe46 Mon Sep 17 00:00:00 2001 From: N-R-K <79544946+N-R-K@users.noreply.github.com> Date: Fri, 29 Oct 2021 02:00:53 +0600 Subject: [PATCH] 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 Co-authored-by: Arthur Williams --- commands.c | 2 -- image.c | 16 +++++++--------- main.c | 22 ++++++++++++---------- nsxiv.h | 8 ++++---- options.c | 1 - thumbs.c | 8 ++++---- utf8.h | 8 ++++---- util.c | 2 +- window.c | 20 ++++++++++---------- 9 files changed, 42 insertions(+), 45 deletions(-) diff --git a/commands.c b/commands.c index 32b4a8b..3bfb28f 100644 --- a/commands.c +++ b/commands.c @@ -18,8 +18,6 @@ */ #include "nsxiv.h" -#define _IMAGE_CONFIG -#include "config.h" #include #include diff --git a/image.c b/image.c index 776840d..2926a1f 100644 --- a/image.c +++ b/image.c @@ -135,8 +135,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) if (img->multi.cap == 0) { img->multi.cap = 8; - img->multi.frames = (img_frame_t*) - emalloc(sizeof(img_frame_t) * img->multi.cap); + img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); } img->multi.cnt = img->multi.sel = 0; img->multi.length = 0; @@ -188,9 +187,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) w = gif->Image.Width; h = gif->Image.Height; - rows = (GifRowType*) emalloc(h * sizeof(GifRowType)); + rows = emalloc(h * sizeof(GifRowType)); for (i = 0; i < h; i++) - rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType)); + rows[i] = emalloc(w * sizeof(GifPixelType)); if (gif->Image.Interlace) { for (i = 0; i < 4; 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); } - ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh); + ptr = data = emalloc(sw * sh * sizeof(DATA32)); cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; if (bg >= cmap->ColorCount) { err = true; @@ -258,8 +257,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) if (img->multi.cnt == img->multi.cap) { img->multi.cap *= 2; - img->multi.frames = (img_frame_t*) - erealloc(img->multi.frames, + img->multi.frames = erealloc(img->multi.frames, img->multi.cap * sizeof(img_frame_t)); } 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; WebPData data; - data.bytes = NULL; Imlib_Image im = NULL; 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 int delay; bool err = false; + data.bytes = NULL; if ((err = fframe == NULL && img == NULL)) goto fail; @@ -863,7 +861,7 @@ bool img_change_gamma(img_t *img, int d) if (img->gamma != gamma) { imlib_reset_color_modifier(); - if (gamma != 0) { + if (gamma) { range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0; imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE)); } diff --git a/main.c b/main.c index a5b381a..e16f5ac 100644 --- a/main.c +++ b/main.c @@ -169,7 +169,7 @@ void remove_file(int n, bool manual) void set_timeout(timeout_f handler, int time, bool overwrite) { - int i; + unsigned int i; for (i = 0; i < ARRLEN(timeouts); i++) { 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) { - int i; + unsigned int i; for (i = 0; i < ARRLEN(timeouts); i++) { if (timeouts[i].handler == handler) { @@ -234,7 +234,7 @@ void open_info(void) int pfd[2]; 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; win.bar.l.buf[0] = '\0'; if (pipe(pfd) < 0) @@ -379,7 +379,7 @@ void update_info(void) else 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, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0)); if (img.multi.cnt > 0) { @@ -423,7 +423,8 @@ void redraw(void) void reset_cursor(void) { - int c, i; + int c; + unsigned int i; cursor_t cursor = CURSOR_NONE; if (mode == MODE_IMAGE) { @@ -499,7 +500,7 @@ void run_key_handler(const char *key, unsigned int mask) struct stat *oldst, st; XEvent dump; - if (keyhandler.f.err != 0) { + if (keyhandler.f.err) { if (!keyhandler.warned) { error(0, keyhandler.f.err, "%s", keyhandler.f.cmd); keyhandler.warned = true; @@ -586,7 +587,7 @@ end: void on_keypress(XKeyEvent *kev) { - int i; + unsigned int i; unsigned int sh = 0; KeySym ksym, shksym; char dummy, key; @@ -629,7 +630,8 @@ void on_keypress(XKeyEvent *kev) void on_buttonpress(XButtonEvent *bev) { - int i, sel; + int sel; + unsigned int i; bool dirty = false; static Time firstclick; @@ -855,7 +857,7 @@ int main(int argc, char *argv[]) if (options->clean_cache) { tns_init(&tns, NULL, NULL, NULL, NULL); - tns_clean_cache(&tns); + tns_clean_cache(); exit(EXIT_SUCCESS); } @@ -929,7 +931,7 @@ int main(int argc, char *argv[]) const char *s = "/nsxiv/exec/"; 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); snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]); if (access(cmd[i]->cmd, X_OK) != 0) diff --git a/nsxiv.h b/nsxiv.h index d49e176..06f5300 100644 --- a/nsxiv.h +++ b/nsxiv.h @@ -119,7 +119,7 @@ typedef enum { typedef enum { SUFFIX_EMPTY, SUFFIX_BASENAME, - SUFFIX_FULLPATH, + SUFFIX_FULLPATH } suffixmode_t; typedef struct { @@ -198,7 +198,7 @@ typedef struct { int cnt; int sel; bool animate; - int framedelay; + unsigned int framedelay; int length; } multi_img_t; @@ -272,7 +272,7 @@ struct opt { /* window: */ bool fullscreen; bool hide_bar; - long embed; + Window embed; /* unsigned long */ char *geometry; char *res_name; const char *title_prefix; @@ -324,7 +324,7 @@ struct tns { 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*); CLEANUP void tns_free(tns_t*); bool tns_load(tns_t*, int, bool, bool); diff --git a/options.c b/options.c index 13af0c7..d9eb59d 100644 --- a/options.c +++ b/options.c @@ -18,7 +18,6 @@ */ #include "nsxiv.h" -#define _IMAGE_CONFIG #define _TITLE_CONFIG #include "config.h" #include "version.h" diff --git a/thumbs.c b/thumbs.c index 22d589f..f518ecb 100644 --- a/thumbs.c +++ b/thumbs.c @@ -49,7 +49,7 @@ char* tns_cache_filepath(const char *filepath) if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { /* don't cache images inside the cache directory! */ len = strlen(cache_dir) + strlen(filepath) + 2; - cfile = (char*) emalloc(len); + cfile = emalloc(len); snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); } return cfile; @@ -120,7 +120,7 @@ end: } } -void tns_clean_cache(tns_t *tns) +void tns_clean_cache(void) { int dirlen; 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 = ""; 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)); } else { 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"; free(cache_dir); 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); } else { error(0, 0, "Cache directory not found"); diff --git a/utf8.h b/utf8.h index 8c6a7a0..a9be2d4 100644 --- a/utf8.h +++ b/utf8.h @@ -53,13 +53,13 @@ utf8_decode(void *buf, uint32_t *c, int *e) *c >>= shiftc[len]; /* Accumulate the various error conditions. */ - *e = (*c < mins[len]) << 6; // non-canonical encoding - *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? - *e |= (*c > 0x10FFFF) << 8; // out of range? + *e = (*c < mins[len]) << 6; /* non-canonical encoding */ + *e |= ((*c >> 11) == 0x1b) << 7; /* surrogate half? */ + *e |= (*c > 0x10FFFF) << 8; /* out of range? */ *e |= (s[1] & 0xc0) >> 2; *e |= (s[2] & 0xc0) >> 4; *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]; return next; diff --git a/util.c b/util.c index 37b5d10..84297f0 100644 --- a/util.c +++ b/util.c @@ -82,7 +82,7 @@ void error(int eval, int err, const char* fmt, ...) void size_readable(float *size, const char **unit) { const char *units[] = { "", "K", "M", "G" }; - int i; + unsigned int i; for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++) *size /= 1024.0; diff --git a/window.c b/window.c index 77eb9a0..97e424f 100644 --- a/window.c +++ b/window.c @@ -172,7 +172,7 @@ void win_init(win_t *win) void win_open(win_t *win) { int c, i, j, n; - long parent; + Window parent; win_env_t *e; XClassHint classhint; unsigned long *icon_data; @@ -188,7 +188,7 @@ void win_open(win_t *win) XSetWindowAttributes attrs; 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.win_gravity = NorthWestGravity; @@ -199,16 +199,16 @@ void win_open(win_t *win) else gmask = XParseGeometry(options->geometry, &win->x, &win->y, &win->w, &win->h); - if ((gmask & WidthValue) != 0) + if (gmask & WidthValue) sizehints.flags |= USSize; else win->w = WIN_WIDTH; - if ((gmask & HeightValue) != 0) + if (gmask & HeightValue) sizehints.flags |= USSize; else win->h = WIN_HEIGHT; - if ((gmask & XValue) != 0) { - if ((gmask & XNegative) != 0) { + if (gmask & XValue) { + if (gmask & XNegative) { win->x += e->scrw - win->w; sizehints.win_gravity = NorthEastGravity; } @@ -216,8 +216,8 @@ void win_open(win_t *win) } else { win->x = 0; } - if ((gmask & YValue) != 0) { - if ((gmask & YNegative) != 0) { + if (gmask & YValue) { + if (gmask & YNegative) { win->y += e->scrh - win->h; sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity ? SouthEastGravity : SouthWestGravity; @@ -322,7 +322,7 @@ void win_open(win_t *win) CLEANUP void win_close(win_t *win) { - int i; + unsigned int i; for (i = 0; i < ARRLEN(cursors); i++) XFreeCursor(win->env.dpy, cursors[i].icon); @@ -360,7 +360,7 @@ void win_toggle_fullscreen(win_t *win) cm->window = win->xwin; cm->message_type = atoms[ATOM__NET_WM_STATE]; 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]; XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,