Krolyxon
Commit 850bc78

Branches

Refs →

nsxiv850bc78

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
Author
N-R-K
Date
2021-10-29 02:00 +0600
Committer
GitHub · 2021-10-29 02:00 +0600
Parent
03eb664
Commit
850bc788c3fd4bb96f425ceedfe4237754cabe46

+42 −45

9 files changed

  1. M commands.c +0 −2
  2. M image.c +7 −9
  3. M main.c +12 −10
  4. M nsxiv.h +4 −4
  5. M options.c +0 −1
  6. M thumbs.c +4 −4
  7. M utf8.h +4 −4
  8. M util.c +1 −1
  9. M window.c +10 −10
M commands.c View
@@ -18,8 +18,6 @@
1818 */
1919
2020 #include "nsxiv.h"
21−#define _IMAGE_CONFIG
22−#include "config.h"
2321
2422 #include <stdlib.h>
2523 #include <string.h>
M image.c View
@@ -135,8 +135,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
135135
136136 if (img->multi.cap == 0) {
137137 img->multi.cap = 8;
138− img->multi.frames = (img_frame_t*)
139− emalloc(sizeof(img_frame_t) * img->multi.cap);
138+ img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
140139 }
141140 img->multi.cnt = img->multi.sel = 0;
142141 img->multi.length = 0;
@@ -188,9 +187,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
188187 w = gif->Image.Width;
189188 h = gif->Image.Height;
190189
191− rows = (GifRowType*) emalloc(h * sizeof(GifRowType));
190+ rows = emalloc(h * sizeof(GifRowType));
192191 for (i = 0; i < h; i++)
193− rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType));
192+ rows[i] = emalloc(w * sizeof(GifPixelType));
194193 if (gif->Image.Interlace) {
195194 for (i = 0; i < 4; i++) {
196195 for (j = intoffset[i]; j < h; j += intjump[i])
@@ -201,7 +200,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
201200 DGifGetLine(gif, rows[i], w);
202201 }
203202
204− ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh);
203+ ptr = data = emalloc(sw * sh * sizeof(DATA32));
205204 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
206205 if (bg >= cmap->ColorCount) {
207206 err = true;
@@ -258,8 +257,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
258257
259258 if (img->multi.cnt == img->multi.cap) {
260259 img->multi.cap *= 2;
261− img->multi.frames = (img_frame_t*)
262− erealloc(img->multi.frames,
260+ img->multi.frames = erealloc(img->multi.frames,
263261 img->multi.cap * sizeof(img_frame_t));
264262 }
265263 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)
322320 {
323321 FILE *webp_file;
324322 WebPData data;
325− data.bytes = NULL;
326323
327324 Imlib_Image im = NULL;
328325 struct WebPAnimDecoderOptions opts;
@@ -335,6 +332,7 @@ bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
335332 unsigned long flags;
336333 unsigned int delay;
337334 bool err = false;
335+ data.bytes = NULL;
338336
339337 if ((err = fframe == NULL && img == NULL))
340338 goto fail;
@@ -863,7 +861,7 @@ bool img_change_gamma(img_t *img, int d)
863861
864862 if (img->gamma != gamma) {
865863 imlib_reset_color_modifier();
866− if (gamma != 0) {
864+ if (gamma) {
867865 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
868866 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
869867 }
M main.c View
@@ -169,7 +169,7 @@ void remove_file(int n, bool manual)
169169
170170 void set_timeout(timeout_f handler, int time, bool overwrite)
171171 {
172− int i;
172+ unsigned int i;
173173
174174 for (i = 0; i < ARRLEN(timeouts); i++) {
175175 if (timeouts[i].handler == handler) {
@@ -185,7 +185,7 @@ void set_timeout(timeout_f handler, int time, bool overwrite)
185185
186186 void reset_timeout(timeout_f handler)
187187 {
188− int i;
188+ unsigned int i;
189189
190190 for (i = 0; i < ARRLEN(timeouts); i++) {
191191 if (timeouts[i].handler == handler) {
@@ -234,7 +234,7 @@ void open_info(void)
234234 int pfd[2];
235235 char w[12], h[12];
236236
237− if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
237+ if (info.f.err || info.fd >= 0 || win.bar.h == 0)
238238 return;
239239 win.bar.l.buf[0] = '\0';
240240 if (pipe(pfd) < 0)
@@ -379,7 +379,7 @@ void update_info(void)
379379 else
380380 bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
381381 }
382− if (img.gamma != 0)
382+ if (img.gamma)
383383 bar_put(r, "G%+d" BAR_SEP, img.gamma);
384384 bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
385385 if (img.multi.cnt > 0) {
@@ -423,7 +423,8 @@ void redraw(void)
423423
424424 void reset_cursor(void)
425425 {
426− int c, i;
426+ int c;
427+ unsigned int i;
427428 cursor_t cursor = CURSOR_NONE;
428429
429430 if (mode == MODE_IMAGE) {
@@ -499,7 +500,7 @@ void run_key_handler(const char *key, unsigned int mask)
499500 struct stat *oldst, st;
500501 XEvent dump;
501502
502− if (keyhandler.f.err != 0) {
503+ if (keyhandler.f.err) {
503504 if (!keyhandler.warned) {
504505 error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
505506 keyhandler.warned = true;
@@ -586,7 +587,7 @@ end:
586587
587588 void on_keypress(XKeyEvent *kev)
588589 {
589− int i;
590+ unsigned int i;
590591 unsigned int sh = 0;
591592 KeySym ksym, shksym;
592593 char dummy, key;
@@ -629,7 +630,8 @@ void on_keypress(XKeyEvent *kev)
629630
630631 void on_buttonpress(XButtonEvent *bev)
631632 {
632− int i, sel;
633+ int sel;
634+ unsigned int i;
633635 bool dirty = false;
634636 static Time firstclick;
635637
@@ -855,7 +857,7 @@ int main(int argc, char *argv[])
855857
856858 if (options->clean_cache) {
857859 tns_init(&tns, NULL, NULL, NULL, NULL);
858− tns_clean_cache(&tns);
860+ tns_clean_cache();
859861 exit(EXIT_SUCCESS);
860862 }
861863
@@ -929,7 +931,7 @@ int main(int argc, char *argv[])
929931 const char *s = "/nsxiv/exec/";
930932
931933 for (i = 0; i < ARRLEN(cmd); i++) {
932− n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + strlen(s) + 1;
934+ n = strlen(homedir) + strlen(dsuffix) + strlen(s) + strlen(name[i]) + 1;
933935 cmd[i]->cmd = emalloc(n);
934936 snprintf(cmd[i]->cmd, n, "%s%s%s%s", homedir, dsuffix, s, name[i]);
935937 if (access(cmd[i]->cmd, X_OK) != 0)
M nsxiv.h View
@@ -119,7 +119,7 @@ typedef enum {
119119 typedef enum {
120120 SUFFIX_EMPTY,
121121 SUFFIX_BASENAME,
122− SUFFIX_FULLPATH,
122+ SUFFIX_FULLPATH
123123 } suffixmode_t;
124124
125125 typedef struct {
@@ -198,7 +198,7 @@ typedef struct {
198198 int cnt;
199199 int sel;
200200 bool animate;
201− int framedelay;
201+ unsigned int framedelay;
202202 int length;
203203 } multi_img_t;
204204
@@ -272,7 +272,7 @@ struct opt {
272272 /* window: */
273273 bool fullscreen;
274274 bool hide_bar;
275− long embed;
275+ Window embed; /* unsigned long */
276276 char *geometry;
277277 char *res_name;
278278 const char *title_prefix;
@@ -324,7 +324,7 @@ struct tns {
324324 bool dirty;
325325 };
326326
327−void tns_clean_cache(tns_t*);
327+void tns_clean_cache(void);
328328 void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
329329 CLEANUP void tns_free(tns_t*);
330330 bool tns_load(tns_t*, int, bool, bool);
M options.c View
@@ -18,7 +18,6 @@
1818 */
1919
2020 #include "nsxiv.h"
21−#define _IMAGE_CONFIG
2221 #define _TITLE_CONFIG
2322 #include "config.h"
2423 #include "version.h"
M thumbs.c View
@@ -49,7 +49,7 @@ char* tns_cache_filepath(const char *filepath)
4949 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
5050 /* don't cache images inside the cache directory! */
5151 len = strlen(cache_dir) + strlen(filepath) + 2;
52− cfile = (char*) emalloc(len);
52+ cfile = emalloc(len);
5353 snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
5454 }
5555 return cfile;
@@ -120,7 +120,7 @@ end:
120120 }
121121 }
122122
123−void tns_clean_cache(tns_t *tns)
123+void tns_clean_cache(void)
124124 {
125125 int dirlen;
126126 char *cfile, *filename;
@@ -150,7 +150,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
150150 const char *homedir, *dsuffix = "";
151151
152152 if (cnt != NULL && *cnt > 0) {
153− tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t));
153+ tns->thumbs = emalloc(*cnt * sizeof(thumb_t));
154154 memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
155155 } else {
156156 tns->thumbs = NULL;
@@ -174,7 +174,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, win_t *wi
174174 const char *s = "/nsxiv";
175175 free(cache_dir);
176176 len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1;
177− cache_dir = (char*) emalloc(len);
177+ cache_dir = emalloc(len);
178178 snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s);
179179 } else {
180180 error(0, 0, "Cache directory not found");
M utf8.h View
@@ -53,13 +53,13 @@ utf8_decode(void *buf, uint32_t *c, int *e)
5353 *c >>= shiftc[len];
5454
5555 /* Accumulate the various error conditions. */
56− *e = (*c < mins[len]) << 6; // non-canonical encoding
57− *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
58− *e |= (*c > 0x10FFFF) << 8; // out of range?
56+ *e = (*c < mins[len]) << 6; /* non-canonical encoding */
57+ *e |= ((*c >> 11) == 0x1b) << 7; /* surrogate half? */
58+ *e |= (*c > 0x10FFFF) << 8; /* out of range? */
5959 *e |= (s[1] & 0xc0) >> 2;
6060 *e |= (s[2] & 0xc0) >> 4;
6161 *e |= (s[3] ) >> 6;
62− *e ^= 0x2a; // top two bits of each tail byte correct?
62+ *e ^= 0x2a; /* top two bits of each tail byte correct? */
6363 *e >>= shifte[len];
6464
6565 return next;
M util.c View
@@ -82,7 +82,7 @@ void error(int eval, int err, const char* fmt, ...)
8282 void size_readable(float *size, const char **unit)
8383 {
8484 const char *units[] = { "", "K", "M", "G" };
85− int i;
85+ unsigned int i;
8686
8787 for (i = 0; i < ARRLEN(units) && *size > 1024.0; i++)
8888 *size /= 1024.0;
M window.c View
@@ -172,7 +172,7 @@ void win_init(win_t *win)
172172 void win_open(win_t *win)
173173 {
174174 int c, i, j, n;
175− long parent;
175+ Window parent;
176176 win_env_t *e;
177177 XClassHint classhint;
178178 unsigned long *icon_data;
@@ -188,7 +188,7 @@ void win_open(win_t *win)
188188 XSetWindowAttributes attrs;
189189
190190 e = &win->env;
191− parent = options->embed != 0 ? options->embed : RootWindow(e->dpy, e->scr);
191+ parent = options->embed ? options->embed : RootWindow(e->dpy, e->scr);
192192
193193 sizehints.flags = PWinGravity;
194194 sizehints.win_gravity = NorthWestGravity;
@@ -199,16 +199,16 @@ void win_open(win_t *win)
199199 else
200200 gmask = XParseGeometry(options->geometry, &win->x, &win->y,
201201 &win->w, &win->h);
202− if ((gmask & WidthValue) != 0)
202+ if (gmask & WidthValue)
203203 sizehints.flags |= USSize;
204204 else
205205 win->w = WIN_WIDTH;
206− if ((gmask & HeightValue) != 0)
206+ if (gmask & HeightValue)
207207 sizehints.flags |= USSize;
208208 else
209209 win->h = WIN_HEIGHT;
210− if ((gmask & XValue) != 0) {
211− if ((gmask & XNegative) != 0) {
210+ if (gmask & XValue) {
211+ if (gmask & XNegative) {
212212 win->x += e->scrw - win->w;
213213 sizehints.win_gravity = NorthEastGravity;
214214 }
@@ -216,8 +216,8 @@ void win_open(win_t *win)
216216 } else {
217217 win->x = 0;
218218 }
219− if ((gmask & YValue) != 0) {
220− if ((gmask & YNegative) != 0) {
219+ if (gmask & YValue) {
220+ if (gmask & YNegative) {
221221 win->y += e->scrh - win->h;
222222 sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
223223 ? SouthEastGravity : SouthWestGravity;
@@ -322,7 +322,7 @@ void win_open(win_t *win)
322322
323323 CLEANUP void win_close(win_t *win)
324324 {
325− int i;
325+ unsigned int i;
326326
327327 for (i = 0; i < ARRLEN(cursors); i++)
328328 XFreeCursor(win->env.dpy, cursors[i].icon);
@@ -360,7 +360,7 @@ void win_toggle_fullscreen(win_t *win)
360360 cm->window = win->xwin;
361361 cm->message_type = atoms[ATOM__NET_WM_STATE];
362362 cm->format = 32;
363− cm->data.l[0] = 2; // toggle
363+ cm->data.l[0] = 2; /* toggle */
364364 cm->data.l[1] = atoms[ATOM__NET_WM_STATE_FULLSCREEN];
365365
366366 XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,