Use bit-field for boolean flags in fileinfo struct

This commit is contained in:
Bert Münnich 2015-01-04 15:38:49 +01:00
parent 0cb1d11305
commit 9b9294bae6
6 changed files with 34 additions and 27 deletions

View file

@ -1,4 +1,4 @@
VERSION := git-20141222 VERSION := git-20150104
PREFIX := /usr/local PREFIX := /usr/local
MANPREFIX := $(PREFIX)/share/man MANPREFIX := $(PREFIX)/share/man

View file

@ -61,7 +61,7 @@ bool cg_quit(arg_t a)
if (options->to_stdout && markcnt > 0) { if (options->to_stdout && markcnt > 0) {
for (i = 0; i < filecnt; i++) { for (i = 0; i < filecnt; i++) {
if (files[i].marked) if (files[i].flags & FF_MARK)
printf("%s\n", files[i].name); printf("%s\n", files[i].name);
} }
} }
@ -200,10 +200,10 @@ bool cg_zoom(arg_t a)
bool cg_toggle_image_mark(arg_t a) bool cg_toggle_image_mark(arg_t a)
{ {
files[fileidx].marked = !files[fileidx].marked; files[fileidx].flags ^= FF_MARK;
markcnt += files[fileidx].marked ? 1 : -1; markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
if (mode == MODE_THUMB) if (mode == MODE_THUMB)
tns_mark(&tns, fileidx, files[fileidx].marked); tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
return true; return true;
} }
@ -212,8 +212,8 @@ bool cg_reverse_marks(arg_t a)
int i; int i;
for (i = 0; i < filecnt; i++) { for (i = 0; i < filecnt; i++) {
files[i].marked = !files[i].marked; files[i].flags ^= FF_MARK;
markcnt += files[i].marked ? 1 : -1; markcnt += files[i].flags & FF_MARK ? 1 : -1;
} }
if (mode == MODE_THUMB) if (mode == MODE_THUMB)
tns.dirty = true; tns.dirty = true;
@ -225,7 +225,7 @@ bool cg_unmark_all(arg_t a)
int i; int i;
for (i = 0; i < filecnt; i++) for (i = 0; i < filecnt; i++)
files[i].marked = false; files[i].flags &= ~FF_MARK;
markcnt = 0; markcnt = 0;
if (mode == MODE_THUMB) if (mode == MODE_THUMB)
tns.dirty = true; tns.dirty = true;
@ -242,7 +242,7 @@ bool cg_navigate_marked(arg_t a)
n *= prefix; n *= prefix;
d = n > 0 ? 1 : -1; d = n > 0 ? 1 : -1;
for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
if (files[i].marked) { if (files[i].flags & FF_MARK) {
n -= d; n -= d;
new = i; new = i;
} }

View file

@ -292,7 +292,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
DGifCloseFile(gif); DGifCloseFile(gif);
#endif #endif
if (err && file->warn) if (err && (file->flags & FF_WARN))
warn("corrupted gif file: %s", file->name); warn("corrupted gif file: %s", file->name);
if (img->multi.cnt > 1) { 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 || if (access(file->path, R_OK) < 0 ||
(img->im = imlib_load_image(file->path)) == NULL) (img->im = imlib_load_image(file->path)) == NULL)
{ {
if (file->warn) if (file->flags & FF_WARN)
warn("could not open image: %s", file->name); warn("could not open image: %s", file->name);
return false; return false;
} }

25
main.c
View file

@ -128,7 +128,8 @@ void check_add_file(char *filename, bool given)
if (fileidx == filecnt) { if (fileidx == filecnt) {
filecnt *= 2; 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 && \ #if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
@ -149,7 +150,6 @@ void check_add_file(char *filename, bool given)
} }
#endif #endif
files[fileidx].warn = given;
files[fileidx].name = s_strdup(filename); files[fileidx].name = s_strdup(filename);
if (files[fileidx].path == NULL) if (files[fileidx].path == NULL)
files[fileidx].path = files[fileidx].name; files[fileidx].path = files[fileidx].name;
@ -157,6 +157,8 @@ void check_add_file(char *filename, bool given)
files[fileidx].base = ++bn; files[fileidx].base = ++bn;
else else
files[fileidx].base = files[fileidx].name; files[fileidx].base = files[fileidx].name;
if (given)
files[fileidx].flags |= FF_WARN;
fileidx++; fileidx++;
} }
@ -171,7 +173,7 @@ void remove_file(int n, bool manual)
cleanup(); cleanup();
exit(manual ? EXIT_SUCCESS : EXIT_FAILURE); exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
} }
if (files[n].marked) if (files[n].flags & FF_MARK)
markcnt--; markcnt--;
if (files[n].path != files[n].name) if (files[n].path != files[n].name)
@ -335,7 +337,7 @@ void load_image(int new)
else if (new > 0 && new < fileidx) else if (new > 0 && new < fileidx)
new--; new--;
} }
files[new].warn = false; files[new].flags &= ~FF_WARN;
fileidx = current = new; fileidx = current = new;
info.open = false; info.open = false;
@ -378,7 +380,7 @@ void update_info(void)
if (win.bar.h == 0) if (win.bar.h == 0)
return; return;
for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
mark = files[fileidx].marked ? "* " : ""; mark = files[fileidx].flags & FF_MARK ? "* " : "";
l->p = l->buf; l->p = l->buf;
r->p = r->buf; r->p = r->buf;
if (mode == MODE_THUMB) { 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++) { 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]); stat(files[i].path, &oldst[f]);
fprintf(pfs, "%s\n", files[i].name); fprintf(pfs, "%s\n", files[i].name);
f++; 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); warn("key handler exited with non-zero return value: %d", retval);
for (f = i = 0; f < fcnt; i++) { 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 || if (stat(files[i].path, &st) != 0 ||
memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
{ {
@ -670,9 +672,9 @@ void on_buttonpress(XButtonEvent *bev)
break; break;
case Button3: case Button3:
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
files[sel].marked = !files[sel].marked; files[sel].flags ^= FF_MARK;
markcnt += files[sel].marked ? 1 : -1; markcnt += files[sel].flags & FF_MARK ? 1 : -1;
tns_mark(&tns, sel, files[sel].marked); tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
redraw(); redraw();
} }
break; break;
@ -818,7 +820,8 @@ int main(int argc, char **argv)
else else
filecnt = options->filecnt; 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; fileidx = 0;
if (options->from_stdin) { if (options->from_stdin) {

View file

@ -341,7 +341,7 @@ bool tns_load(tns_t *tns, int n, bool force)
if (im == NULL && (access(file->path, R_OK) < 0 || if (im == NULL && (access(file->path, R_OK) < 0 ||
(im = imlib_load_image(file->path)) == NULL)) (im = imlib_load_image(file->path)) == NULL))
{ {
if (file->warn) if (file->flags & FF_WARN)
warn("could not open image: %s", file->name); warn("could not open image: %s", file->name);
return false; return false;
} }
@ -461,7 +461,7 @@ void tns_render(tns_t *tns)
t->y = y + (thumb_sizes[tns->zl] - t->h) / 2; t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
imlib_context_set_image(t->im); imlib_context_set_image(t->im);
imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h); 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); tns_mark(tns, i, true);
} else { } else {
tns->loadnext = MIN(tns->loadnext, i); 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, win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
false, tns->bw, col); false, tns->bw, col);
if (tns->files[n].marked) if (tns->files[n].flags & FF_MARK)
tns_mark(tns, n, true); tns_mark(tns, n, true);
} }
} }

View file

@ -64,12 +64,16 @@ typedef enum {
CURSOR_WATCH CURSOR_WATCH
} cursor_t; } cursor_t;
typedef enum {
FF_WARN = 1,
FF_MARK = 2
} fileflags_t;
typedef struct { typedef struct {
const char *name; /* as given by user */ const char *name; /* as given by user */
const char *path; /* always absolute */ const char *path; /* always absolute */
const char *base; const char *base;
bool warn; fileflags_t flags;
bool marked;
} fileinfo_t; } fileinfo_t;
/* timeouts in milliseconds: */ /* timeouts in milliseconds: */