From 9b9294bae67da4e0388e7c31d0063f4e114aa1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20M=C3=BCnnich?= Date: Sun, 4 Jan 2015 15:38:49 +0100 Subject: Use bit-field for boolean flags in fileinfo struct --- Makefile | 2 +- commands.c | 16 ++++++++-------- image.c | 4 ++-- main.c | 25 ++++++++++++++----------- thumbs.c | 6 +++--- types.h | 8 ++++++-- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index d4e9b65..f0b22d3 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION := git-20141222 +VERSION := git-20150104 PREFIX := /usr/local MANPREFIX := $(PREFIX)/share/man diff --git a/commands.c b/commands.c index 770f780..f77add3 100644 --- a/commands.c +++ b/commands.c @@ -61,7 +61,7 @@ bool cg_quit(arg_t a) if (options->to_stdout && markcnt > 0) { for (i = 0; i < filecnt; i++) { - if (files[i].marked) + if (files[i].flags & FF_MARK) printf("%s\n", files[i].name); } } @@ -200,10 +200,10 @@ bool cg_zoom(arg_t a) bool cg_toggle_image_mark(arg_t a) { - files[fileidx].marked = !files[fileidx].marked; - markcnt += files[fileidx].marked ? 1 : -1; + files[fileidx].flags ^= FF_MARK; + markcnt += files[fileidx].flags & FF_MARK ? 1 : -1; if (mode == MODE_THUMB) - tns_mark(&tns, fileidx, files[fileidx].marked); + tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK)); return true; } @@ -212,8 +212,8 @@ bool cg_reverse_marks(arg_t a) int i; for (i = 0; i < filecnt; i++) { - files[i].marked = !files[i].marked; - markcnt += files[i].marked ? 1 : -1; + files[i].flags ^= FF_MARK; + markcnt += files[i].flags & FF_MARK ? 1 : -1; } if (mode == MODE_THUMB) tns.dirty = true; @@ -225,7 +225,7 @@ bool cg_unmark_all(arg_t a) int i; for (i = 0; i < filecnt; i++) - files[i].marked = false; + files[i].flags &= ~FF_MARK; markcnt = 0; if (mode == MODE_THUMB) tns.dirty = true; @@ -242,7 +242,7 @@ bool cg_navigate_marked(arg_t a) n *= prefix; d = n > 0 ? 1 : -1; for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { - if (files[i].marked) { + if (files[i].flags & FF_MARK) { n -= d; new = i; } diff --git a/image.c b/image.c index ee9c30d..240054f 100644 --- a/image.c +++ b/image.c @@ -292,7 +292,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) DGifCloseFile(gif); #endif - if (err && file->warn) + if (err && (file->flags & FF_WARN)) warn("corrupted gif file: %s", file->name); if (img->multi.cnt > 1) { @@ -321,7 +321,7 @@ bool img_load(img_t *img, const fileinfo_t *file) if (access(file->path, R_OK) < 0 || (img->im = imlib_load_image(file->path)) == NULL) { - if (file->warn) + if (file->flags & FF_WARN) warn("could not open image: %s", file->name); return false; } diff --git a/main.c b/main.c index 904a567..32c0888 100644 --- a/main.c +++ b/main.c @@ -128,7 +128,8 @@ void check_add_file(char *filename, bool given) if (fileidx == filecnt) { filecnt *= 2; - files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t)); + files = s_realloc(files, filecnt * sizeof(*files)); + memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files)); } #if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \ @@ -149,7 +150,6 @@ void check_add_file(char *filename, bool given) } #endif - files[fileidx].warn = given; files[fileidx].name = s_strdup(filename); if (files[fileidx].path == NULL) files[fileidx].path = files[fileidx].name; @@ -157,6 +157,8 @@ void check_add_file(char *filename, bool given) files[fileidx].base = ++bn; else files[fileidx].base = files[fileidx].name; + if (given) + files[fileidx].flags |= FF_WARN; fileidx++; } @@ -171,7 +173,7 @@ void remove_file(int n, bool manual) cleanup(); exit(manual ? EXIT_SUCCESS : EXIT_FAILURE); } - if (files[n].marked) + if (files[n].flags & FF_MARK) markcnt--; if (files[n].path != files[n].name) @@ -335,7 +337,7 @@ void load_image(int new) else if (new > 0 && new < fileidx) new--; } - files[new].warn = false; + files[new].flags &= ~FF_WARN; fileidx = current = new; info.open = false; @@ -378,7 +380,7 @@ void update_info(void) if (win.bar.h == 0) return; for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); - mark = files[fileidx].marked ? "* " : ""; + mark = files[fileidx].flags & FF_MARK ? "* " : ""; l->p = l->buf; r->p = r->buf; if (mode == MODE_THUMB) { @@ -535,7 +537,7 @@ void run_key_handler(const char *key, unsigned int mask) } for (f = i = 0; f < fcnt; i++) { - if ((marked && files[i].marked) || (!marked && i == fileidx)) { + if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { stat(files[i].path, &oldst[f]); fprintf(pfs, "%s\n", files[i].name); f++; @@ -548,7 +550,7 @@ void run_key_handler(const char *key, unsigned int mask) warn("key handler exited with non-zero return value: %d", retval); for (f = i = 0; f < fcnt; i++) { - if ((marked && files[i].marked) || (!marked && i == fileidx)) { + if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { if (stat(files[i].path, &st) != 0 || memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) { @@ -670,9 +672,9 @@ void on_buttonpress(XButtonEvent *bev) break; case Button3: if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { - files[sel].marked = !files[sel].marked; - markcnt += files[sel].marked ? 1 : -1; - tns_mark(&tns, sel, files[sel].marked); + files[sel].flags ^= FF_MARK; + markcnt += files[sel].flags & FF_MARK ? 1 : -1; + tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK)); redraw(); } break; @@ -818,7 +820,8 @@ int main(int argc, char **argv) else filecnt = options->filecnt; - files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); + files = s_malloc(filecnt * sizeof(*files)); + memset(files, 0, filecnt * sizeof(*files)); fileidx = 0; if (options->from_stdin) { diff --git a/thumbs.c b/thumbs.c index 8051dd8..8ec7a30 100644 --- a/thumbs.c +++ b/thumbs.c @@ -341,7 +341,7 @@ bool tns_load(tns_t *tns, int n, bool force) if (im == NULL && (access(file->path, R_OK) < 0 || (im = imlib_load_image(file->path)) == NULL)) { - if (file->warn) + if (file->flags & FF_WARN) warn("could not open image: %s", file->name); return false; } @@ -461,7 +461,7 @@ void tns_render(tns_t *tns) t->y = y + (thumb_sizes[tns->zl] - t->h) / 2; imlib_context_set_image(t->im); imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h); - if (tns->files[i].marked) + if (tns->files[i].flags & FF_MARK) tns_mark(tns, i, true); } else { tns->loadnext = MIN(tns->loadnext, i); @@ -520,7 +520,7 @@ void tns_highlight(tns_t *tns, int n, bool hl) win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh, false, tns->bw, col); - if (tns->files[n].marked) + if (tns->files[n].flags & FF_MARK) tns_mark(tns, n, true); } } diff --git a/types.h b/types.h index 78022bd..c573d13 100644 --- a/types.h +++ b/types.h @@ -64,12 +64,16 @@ typedef enum { CURSOR_WATCH } cursor_t; +typedef enum { + FF_WARN = 1, + FF_MARK = 2 +} fileflags_t; + typedef struct { const char *name; /* as given by user */ const char *path; /* always absolute */ const char *base; - bool warn; - bool marked; + fileflags_t flags; } fileinfo_t; /* timeouts in milliseconds: */ -- cgit v1.2.3