1 /* General purpose, i.e. non SoX specific, utility functions and macros.
3 * (c) 2006-8 Chris Bagwell and SoX contributors
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or (at
8 * your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "soxconfig.h"
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h> /* For off_t not found in stdio.h */
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h> /* Needs to be included before we redefine off_t. */
35 /*---------------------------- Portability stuff -----------------------------*/
38 #define NORET __attribute__((noreturn))
39 #define UNUSED __attribute__ ((unused))
48 #define O_BINARY _O_BINARY
49 #define O_CREAT _O_CREAT
50 #define O_RDWR _O_RDWR
51 #define O_TRUNC _O_TRUNC
52 #define S_IFMT _S_IFMT
53 #define S_IFREG _S_IFREG
54 #define S_IREAD _S_IREAD
55 #define S_IWRITE _S_IWRITE
58 #define fdopen _fdopen
59 #define fileno _fileno
62 #define fstat _fstati64
68 #define inline __inline
69 #define isatty _isatty
71 #define mktemp _mktemp
74 #define pclose _pclose
76 #define setmode _setmode
77 #define snprintf _snprintf
85 #define strdup _strdup
87 #define unlink _unlink
89 #if defined(HAVE__FSEEKI64) && !defined(HAVE_FSEEKO)
91 #define fseeko _fseeki64
92 #define ftello _ftelli64
97 #elif defined(__MINGW32__)
99 #if !defined(HAVE_FSEEKO)
101 #define fseeko fseeko64
102 #define fstat _fstati64
103 #define ftello ftello64
104 #define off_t off64_t
105 #define stat _stati64
106 #define HAVE_FSEEKO 1
111 #if defined(DOS) || defined(WIN32) || defined(__NT__) || defined(__DJGPP__) || defined(__OS2__)
112 #define LAST_SLASH(path) max(strrchr(path, '/'), strrchr(path, '\\'))
113 #define IS_ABSOLUTE(path) ((path)[0] == '/' || (path)[0] == '\\' || (path)[1] == ':')
114 #define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
115 #define POPEN_MODE "rb"
117 #define LAST_SLASH(path) strrchr(path, '/')
118 #define IS_ABSOLUTE(path) ((path)[0] == '/')
119 #define SET_BINARY_MODE(file)
122 #ifdef WORDS_BIGENDIAN
123 #define MACHINE_IS_BIGENDIAN 1
124 #define MACHINE_IS_LITTLEENDIAN 0
126 #define MACHINE_IS_BIGENDIAN 0
127 #define MACHINE_IS_LITTLEENDIAN 1
130 /*--------------------------- Language extensions ----------------------------*/
132 /* Compile-time ("static") assertion */
133 /* e.g. assert_static(sizeof(int) >= 4, int_type_too_small) */
134 #define assert_static(e,f) enum {assert_static__##f = 1/(e)}
135 #define array_length(a) (sizeof(a)/sizeof(a[0]))
136 #define field_offset(type, field) ((size_t)&(((type *)0)->field))
137 #define unless(x) if (!(x))
139 /*------------------------------- Maths stuff --------------------------------*/
146 #define min(a, b) ((a) <= (b) ? (a) : (b))
151 #define max(a, b) ((a) >= (b) ? (a) : (b))
153 #define range_limit(x, lower, upper) (min(max(x, lower), upper))
156 #define M_PI 3.14159265358979323846
159 #define M_PI_2 1.57079632679489661923 /* pi/2 */
162 #define M_LN10 2.30258509299404568402 /* natural log of 10 */
165 #define M_SQRT2 sqrt(2.)
168 #define sqr(a) ((a) * (a))
169 #define sign(x) ((x) < 0? -1 : 1)
171 /* Numerical Recipes in C, p. 284 */
172 #define ranqd1(x) ((x) = 1664525L * (x) + 1013904223L) /* int32_t x */
173 #define dranqd1(x) (ranqd1(x) * (1. / (65536. * 32768.))) /* [-1,1) */
175 #define dB_to_linear(x) exp((x) * M_LN10 * 0.05)
176 #define linear_to_dB(x) (log10(x) * 20)
178 extern int lsx_strcasecmp(const char *s1
, const char *st
);
179 extern int lsx_strncasecmp(char const *s1
, char const *s2
, size_t n
);
181 #ifndef HAVE_STRCASECMP
182 #define strcasecmp(s1, s2) lsx_strcasecmp((s1), (s2))
183 #define strncasecmp(s1, s2, n) lsx_strncasecmp((s1), (s2), (n))