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
22 #include "soxconfig.h"
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h> /* For off_t not found in stdio.h */
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h> /* Needs to be included before we redefine off_t. */
34 /*---------------------------- Portability stuff -----------------------------*/
36 #if defined(HAVE_INTTYPES_H)
38 #elif defined(HAVE_STDINT_H)
41 typedef sox_int8_t
int8_t;
42 typedef sox_uint8_t
uint8_t;
43 typedef sox_int16_t
int16_t;
44 typedef sox_uint16_t
uint16_t;
45 typedef sox_int32_t
int32_t;
46 typedef sox_uint32_t
uint32_t;
47 typedef sox_int64_t
int64_t;
48 typedef sox_uint64_t
uint64_t;
51 /* Define the format specifier to use for int64_t values.
52 * Example: printf("You may have already won $ %" PRId64 " !!!", n64); */
53 #ifndef PRId64 /* Maybe <inttypes.h> already defined this. */
54 #if defined(_MSC_VER) || defined(__MINGW32__) /* Older versions of msvcrt.dll don't recognize %lld. */
56 #elif LONG_MAX==9223372036854775807
63 /* Define the format specifier to use for uint64_t values. */
64 #ifndef PRIu64 /* Maybe <inttypes.h> already defined this. */
65 #if defined(_MSC_VER) || defined(__MINGW32__) /* Older versions of msvcrt.dll don't recognize %llu. */
67 #elif ULONG_MAX==0xffffffffffffffff
74 /* Define the format specifier to use for size_t values.
75 * Example: printf("Sizeof(x) = %" PRIuPTR " bytes", sizeof(x)); */
76 #ifndef PRIuPTR /* Maybe <inttypes.h> already defined this. */
77 #if defined(_MSC_VER) || defined(__MINGW32__) /* Older versions of msvcrt.dll don't recognize %zu. */
85 #define NORET __attribute__((noreturn))
86 #define UNUSED __attribute__ ((unused))
95 #define O_BINARY _O_BINARY
96 #define O_CREAT _O_CREAT
97 #define O_RDWR _O_RDWR
98 #define O_TRUNC _O_TRUNC
99 #define S_IFMT _S_IFMT
100 #define S_IFREG _S_IFREG
101 #define S_IREAD _S_IREAD
102 #define S_IWRITE _S_IWRITE
105 #define fdopen _fdopen
106 #define fileno _fileno
109 #define fstat _fstati64
115 #define inline __inline
116 #define isatty _isatty
118 #define mktemp _mktemp
121 #define pclose _pclose
123 #define POPEN_MODE "rb"
124 #define setmode _setmode
125 #define snprintf _snprintf
128 #define stat _stati64
133 #define strdup _strdup
135 #define unlink _unlink
137 #if defined(HAVE__FSEEKI64) && !defined(HAVE_FSEEKO)
139 #define fseeko _fseeki64
140 #define ftello _ftelli64
141 #define off_t __int64
142 #define HAVE_FSEEKO 1
145 #elif defined(__MINGW32__)
147 #if !defined(HAVE_FSEEKO)
149 #define fseeko fseeko64
150 #define fstat _fstati64
151 #define ftello ftello64
152 #define off_t off64_t
153 #define stat _stati64
154 #define HAVE_FSEEKO 1
159 #if defined(DOS) || defined(WIN32) || defined(__NT__) || defined(__DJGPP__) || defined(__OS2__)
160 #define LAST_SLASH(path) max(strrchr(path, '/'), strrchr(path, '\\'))
161 #define IS_ABSOLUTE(path) ((path)[0] == '/' || (path)[0] == '\\' || (path)[1] == ':')
162 #define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
164 #define LAST_SLASH(path) strrchr(path, '/')
165 #define IS_ABSOLUTE(path) ((path)[0] == '/')
166 #define SET_BINARY_MODE(file)
169 #ifdef WORDS_BIGENDIAN
170 #define MACHINE_IS_BIGENDIAN 1
171 #define MACHINE_IS_LITTLEENDIAN 0
173 #define MACHINE_IS_BIGENDIAN 0
174 #define MACHINE_IS_LITTLEENDIAN 1
177 /*--------------------------- Language extensions ----------------------------*/
179 /* Compile-time ("static") assertion */
180 /* e.g. assert_static(sizeof(int) >= 4, int_type_too_small) */
181 #define assert_static(e,f) enum {assert_static__##f = 1/(e)}
182 #define array_length(a) (sizeof(a)/sizeof(a[0]))
183 #define field_offset(type, field) ((size_t)&(((type *)0)->field))
184 #define unless(x) if (!(x))
186 /*------------------------------- Maths stuff --------------------------------*/
193 #define min(a, b) ((a) <= (b) ? (a) : (b))
198 #define max(a, b) ((a) >= (b) ? (a) : (b))
200 #define range_limit(x, lower, upper) (min(max(x, lower), upper))
203 #define M_PI 3.14159265358979323846
206 #define M_PI_2 1.57079632679489661923 /* pi/2 */
209 #define M_LN10 2.30258509299404568402 /* natural log of 10 */
212 #define M_SQRT2 sqrt(2.)
215 #define sqr(a) ((a) * (a))
216 #define sign(x) ((x) < 0? -1 : 1)
218 /* Numerical Recipes in C, p. 284 */
219 #define ranqd1(x) ((x) = 1664525L * (x) + 1013904223L) /* int32_t x */
220 #define dranqd1(x) (ranqd1(x) * (1. / (65536. * 32768.))) /* [-1,1) */
222 #define dB_to_linear(x) exp((x) * M_LN10 * 0.05)
223 #define linear_to_dB(x) (log10(x) * 20)
225 extern int lsx_strcasecmp(const char *s1
, const char *st
);
226 extern int lsx_strncasecmp(char const *s1
, char const *s2
, size_t n
);
228 #ifndef HAVE_STRCASECMP
229 #define strcasecmp(s1, s2) lsx_strcasecmp((s1), (s2))
230 #define strncasecmp(s1, s2, n) lsx_strncasecmp((s1), (s2), (n))