| 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 |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, 5th 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 | |
| 26 | #define ABS(a) ((a) < 0 ? (-(a)) : (a)) |
| 27 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 28 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 29 | #define LEN(a) (sizeof(a) / sizeof(a[0])) |
| 30 | |
| 31 | #define TIMEDIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec) * 1000 + \ |
| 32 | ((t1)->tv_usec - (t2)->tv_usec) / 1000) |
| 33 | |
| 34 | #define MSEC_TO_TIMEVAL(t,tv) { \ |
| 35 | (tv)->tv_sec = (t) / 1000; \ |
| 36 | (tv)->tv_usec = (t) % 1000 * 1000; \ |
| 37 | } |
| 38 | |
| 39 | #ifndef TIMESPEC_TO_TIMEVAL |
| 40 | #define TIMESPEC_TO_TIMEVAL(tv,ts) { \ |
| 41 | (tv)->tv_sec = (ts)->tv_sec; \ |
| 42 | (tv)->tv_usec = (ts)->tv_nsec / 1000; \ |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | typedef struct { |
| 47 | DIR *dir; |
| 48 | char *name; |
| 49 | int d; |
| 50 | |
| 51 | char **stack; |
| 52 | int stcap; |
| 53 | int stlen; |
| 54 | } r_dir_t; |
| 55 | |
| 56 | void* s_malloc(size_t); |
| 57 | void* s_realloc(void*, size_t); |
| 58 | char* s_strdup(char*); |
| 59 | |
| 60 | void warn(const char*, ...); |
| 61 | void die(const char*, ...); |
| 62 | |
| 63 | void size_readable(float*, const char**); |
| 64 | |
| 65 | char* absolute_path(const char*); |
| 66 | |
| 67 | int r_opendir(r_dir_t*, const char*); |
| 68 | int r_closedir(r_dir_t*); |
| 69 | char* r_readdir(r_dir_t*); |
| 70 | int r_mkdir(const char *); |
| 71 | |
| 72 | #endif /* UTIL_H */ |