Krolyxon
Commit 22d4e99

Branches

Refs →

nsxivutil.h

91 lines · 2.1 KB · Objective-C

1/* sxiv: util.h
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#ifndef UTIL_H
20#define UTIL_H
21
22#include <stdio.h>
23#include <stdarg.h>
24#include <dirent.h>
25#include <sys/time.h>
26#include <sys/types.h>
27
28#include "types.h"
29
30#ifndef MIN
31#define MIN(a,b) ((a) < (b) ? (a) : (b))
32#endif
33#ifndef MAX
34#define MAX(a,b) ((a) > (b) ? (a) : (b))
35#endif
36
37#define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
38
39typedef struct {
40 DIR *dir;
41 char *name;
42 int d;
43
44 char **stack;
45 int stcap;
46 int stlen;
47} r_dir_t;
48
49static inline
50bool streq(const char *a, const char *b) {
51 return strcmp(a, b) == 0;
52}
53
54static inline
55long tv_diff(const struct timeval *t1, const struct timeval *t2) {
56 return (t1->tv_sec - t2->tv_sec) * 1000 +
57 (t1->tv_usec - t2->tv_usec) / 1000;
58}
59
60static inline
61void tv_set_msec(struct timeval *t, int msec) {
62 t->tv_sec = msec / 1000;
63 t->tv_usec = msec % 1000 * 1000;
64}
65
66static inline
67void tv_add_msec(struct timeval *t, int msec) {
68 t->tv_sec += msec / 1000;
69 t->tv_usec += msec % 1000 * 1000;
70}
71
72void* s_malloc(size_t);
73void* s_realloc(void*, size_t);
74char* s_strdup(char*);
75
76void warn(const char*, ...);
77void die(const char*, ...);
78
79ssize_t get_line(char**, size_t*, FILE*);
80
81void size_readable(float*, const char**);
82void time_readable(float*, const char**);
83
84char* absolute_path(const char*);
85
86int r_opendir(r_dir_t*, const char*);
87int r_closedir(r_dir_t*);
88char* r_readdir(r_dir_t*);
89int r_mkdir(const char *);
90
91#endif /* UTIL_H */