Krolyxon
Commit 9b9294b

Branches

Refs →

nsxiv9b9294b

Use bit-field for boolean flags in fileinfo struct

Author
Bert Münnich
Date
2015-01-04 15:38 +0100
Committer
Bert Münnich · 2015-01-04 21:24 +0100
Parent
0cb1d11
Commit
9b9294bae67da4e0388e7c31d0063f4e114aa1f8

+34 −27

6 files changed

  1. M Makefile +1 −1
  2. M commands.c +8 −8
  3. M image.c +2 −2
  4. M main.c +14 −11
  5. M thumbs.c +3 −3
  6. M types.h +6 −2
M Makefile View
@@ -1,4 +1,4 @@
1−VERSION := git-20141222
1+VERSION := git-20150104
22
33 PREFIX := /usr/local
44 MANPREFIX := $(PREFIX)/share/man
M commands.c View
@@ -61,7 +61,7 @@ bool cg_quit(arg_t a)
6161
6262 if (options->to_stdout && markcnt > 0) {
6363 for (i = 0; i < filecnt; i++) {
64− if (files[i].marked)
64+ if (files[i].flags & FF_MARK)
6565 printf("%s\n", files[i].name);
6666 }
6767 }
@@ -200,10 +200,10 @@ bool cg_zoom(arg_t a)
200200
201201 bool cg_toggle_image_mark(arg_t a)
202202 {
203− files[fileidx].marked = !files[fileidx].marked;
204− markcnt += files[fileidx].marked ? 1 : -1;
203+ files[fileidx].flags ^= FF_MARK;
204+ markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
205205 if (mode == MODE_THUMB)
206− tns_mark(&tns, fileidx, files[fileidx].marked);
206+ tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
207207 return true;
208208 }
209209
@@ -212,8 +212,8 @@ bool cg_reverse_marks(arg_t a)
212212 int i;
213213
214214 for (i = 0; i < filecnt; i++) {
215− files[i].marked = !files[i].marked;
216− markcnt += files[i].marked ? 1 : -1;
215+ files[i].flags ^= FF_MARK;
216+ markcnt += files[i].flags & FF_MARK ? 1 : -1;
217217 }
218218 if (mode == MODE_THUMB)
219219 tns.dirty = true;
@@ -225,7 +225,7 @@ bool cg_unmark_all(arg_t a)
225225 int i;
226226
227227 for (i = 0; i < filecnt; i++)
228− files[i].marked = false;
228+ files[i].flags &= ~FF_MARK;
229229 markcnt = 0;
230230 if (mode == MODE_THUMB)
231231 tns.dirty = true;
@@ -242,7 +242,7 @@ bool cg_navigate_marked(arg_t a)
242242 n *= prefix;
243243 d = n > 0 ? 1 : -1;
244244 for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
245− if (files[i].marked) {
245+ if (files[i].flags & FF_MARK) {
246246 n -= d;
247247 new = i;
248248 }
M image.c View
@@ -292,7 +292,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
292292 DGifCloseFile(gif);
293293 #endif
294294
295− if (err && file->warn)
295+ if (err && (file->flags & FF_WARN))
296296 warn("corrupted gif file: %s", file->name);
297297
298298 if (img->multi.cnt > 1) {
@@ -321,7 +321,7 @@ bool img_load(img_t *img, const fileinfo_t *file)
321321 if (access(file->path, R_OK) < 0 ||
322322 (img->im = imlib_load_image(file->path)) == NULL)
323323 {
324− if (file->warn)
324+ if (file->flags & FF_WARN)
325325 warn("could not open image: %s", file->name);
326326 return false;
327327 }
M main.c View
@@ -128,7 +128,8 @@ void check_add_file(char *filename, bool given)
128128
129129 if (fileidx == filecnt) {
130130 filecnt *= 2;
131− files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
131+ files = s_realloc(files, filecnt * sizeof(*files));
132+ memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
132133 }
133134
134135 #if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
@@ -149,7 +150,6 @@ void check_add_file(char *filename, bool given)
149150 }
150151 #endif
151152
152− files[fileidx].warn = given;
153153 files[fileidx].name = s_strdup(filename);
154154 if (files[fileidx].path == NULL)
155155 files[fileidx].path = files[fileidx].name;
@@ -157,6 +157,8 @@ void check_add_file(char *filename, bool given)
157157 files[fileidx].base = ++bn;
158158 else
159159 files[fileidx].base = files[fileidx].name;
160+ if (given)
161+ files[fileidx].flags |= FF_WARN;
160162 fileidx++;
161163 }
162164
@@ -171,7 +173,7 @@ void remove_file(int n, bool manual)
171173 cleanup();
172174 exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
173175 }
174− if (files[n].marked)
176+ if (files[n].flags & FF_MARK)
175177 markcnt--;
176178
177179 if (files[n].path != files[n].name)
@@ -335,7 +337,7 @@ void load_image(int new)
335337 else if (new > 0 && new < fileidx)
336338 new--;
337339 }
338− files[new].warn = false;
340+ files[new].flags &= ~FF_WARN;
339341 fileidx = current = new;
340342
341343 info.open = false;
@@ -378,7 +380,7 @@ void update_info(void)
378380 if (win.bar.h == 0)
379381 return;
380382 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
381− mark = files[fileidx].marked ? "* " : "";
383+ mark = files[fileidx].flags & FF_MARK ? "* " : "";
382384 l->p = l->buf;
383385 r->p = r->buf;
384386 if (mode == MODE_THUMB) {
@@ -535,7 +537,7 @@ void run_key_handler(const char *key, unsigned int mask)
535537 }
536538
537539 for (f = i = 0; f < fcnt; i++) {
538− if ((marked && files[i].marked) || (!marked && i == fileidx)) {
540+ if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
539541 stat(files[i].path, &oldst[f]);
540542 fprintf(pfs, "%s\n", files[i].name);
541543 f++;
@@ -548,7 +550,7 @@ void run_key_handler(const char *key, unsigned int mask)
548550 warn("key handler exited with non-zero return value: %d", retval);
549551
550552 for (f = i = 0; f < fcnt; i++) {
551− if ((marked && files[i].marked) || (!marked && i == fileidx)) {
553+ if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
552554 if (stat(files[i].path, &st) != 0 ||
553555 memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
554556 {
@@ -670,9 +672,9 @@ void on_buttonpress(XButtonEvent *bev)
670672 break;
671673 case Button3:
672674 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
673− files[sel].marked = !files[sel].marked;
674− markcnt += files[sel].marked ? 1 : -1;
675− tns_mark(&tns, sel, files[sel].marked);
675+ files[sel].flags ^= FF_MARK;
676+ markcnt += files[sel].flags & FF_MARK ? 1 : -1;
677+ tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
676678 redraw();
677679 }
678680 break;
@@ -818,7 +820,8 @@ int main(int argc, char **argv)
818820 else
819821 filecnt = options->filecnt;
820822
821− files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
823+ files = s_malloc(filecnt * sizeof(*files));
824+ memset(files, 0, filecnt * sizeof(*files));
822825 fileidx = 0;
823826
824827 if (options->from_stdin) {
M thumbs.c View
@@ -341,7 +341,7 @@ bool tns_load(tns_t *tns, int n, bool force)
341341 if (im == NULL && (access(file->path, R_OK) < 0 ||
342342 (im = imlib_load_image(file->path)) == NULL))
343343 {
344− if (file->warn)
344+ if (file->flags & FF_WARN)
345345 warn("could not open image: %s", file->name);
346346 return false;
347347 }
@@ -461,7 +461,7 @@ void tns_render(tns_t *tns)
461461 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
462462 imlib_context_set_image(t->im);
463463 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
464− if (tns->files[i].marked)
464+ if (tns->files[i].flags & FF_MARK)
465465 tns_mark(tns, i, true);
466466 } else {
467467 tns->loadnext = MIN(tns->loadnext, i);
@@ -520,7 +520,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
520520 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
521521 false, tns->bw, col);
522522
523− if (tns->files[n].marked)
523+ if (tns->files[n].flags & FF_MARK)
524524 tns_mark(tns, n, true);
525525 }
526526 }
M types.h View
@@ -64,12 +64,16 @@ typedef enum {
6464 CURSOR_WATCH
6565 } cursor_t;
6666
67+typedef enum {
68+ FF_WARN = 1,
69+ FF_MARK = 2
70+} fileflags_t;
71+
6772 typedef struct {
6873 const char *name; /* as given by user */
6974 const char *path; /* always absolute */
7075 const char *base;
71− bool warn;
72− bool marked;
76+ fileflags_t flags;
7377 } fileinfo_t;
7478
7579 /* timeouts in milliseconds: */