aac: Collect all interesting ID3 frames
[cmus.git] / utils.h
blobc5c84582493666113728bfca97e608b2d369a6cb
1 /*
2 * Copyright 2004 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #ifndef _UTILS_H
21 #define _UTILS_H
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <time.h>
30 static inline int min(int a, int b)
32 return a < b ? a : b;
35 static inline int max(int a, int b)
37 return a > b ? a : b;
40 static inline int clamp(int val, int minval, int maxval)
42 if (val < minval)
43 return minval;
44 if (val > maxval)
45 return maxval;
46 return val;
49 static inline int scale_from_percentage(int val, int max_val)
51 if (val < 0)
52 return (val * max_val - 50) / 100;
53 return (val * max_val + 50) / 100;
56 static inline int scale_to_percentage(int val, int max_val)
58 int half = max_val / 2;
60 if (max_val <= 0)
61 return 100;
63 if (val < 0)
64 return (val * 100 - half) / max_val;
65 return (val * 100 + half) / max_val;
68 static inline int str_to_int(const char *str, long int *val)
70 char *end;
72 *val = strtol(str, &end, 10);
73 if (*str == 0 || *end != 0)
74 return -1;
75 return 0;
78 static inline time_t file_get_mtime(const char *filename)
80 struct stat s;
82 /* stat follows symlinks, lstat does not */
83 if (stat(filename, &s) == -1)
84 return -1;
85 return s.st_mtime;
88 static inline void ns_sleep(int ns)
90 struct timespec req;
92 req.tv_sec = 0;
93 req.tv_nsec = ns;
94 nanosleep(&req, NULL);
97 static inline void us_sleep(int us)
99 ns_sleep(us * 1e3);
102 static inline void ms_sleep(int ms)
104 ns_sleep(ms * 1e6);
107 static inline int is_url(const char *name)
109 return strncmp(name, "http://", 7) == 0;
112 #endif