Updating osxbuild to work with OS X 10.7+/XCode 4.x.
[sox.git] / src / util.h
blobf10b676268f66d85a2b4bf143206de44f35b8b45
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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "soxconfig.h"
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h> /* For off_t not found in stdio.h */
26 #endif
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h> /* Needs to be included before we redefine off_t. */
30 #endif
32 #include "xmalloc.h"
34 /*---------------------------- Portability stuff -----------------------------*/
36 #if defined(HAVE_INTTYPES_H)
37 #include <inttypes.h>
38 #elif defined(HAVE_STDINT_H)
39 #include <stdint.h>
40 #else
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;
49 #endif
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. */
55 #define PRId64 "I64d"
56 #elif LONG_MAX==9223372036854775807
57 #define PRId64 "ld"
58 #else
59 #define PRId64 "lld"
60 #endif
61 #endif /* PRId64 */
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. */
66 #define PRIu64 "I64u"
67 #elif ULONG_MAX==0xffffffffffffffff
68 #define PRIu64 "lu"
69 #else
70 #define PRIu64 "llu"
71 #endif
72 #endif /* PRIu64 */
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. */
78 #define PRIuPTR "Iu"
79 #else
80 #define PRIuPTR "zu"
81 #endif
82 #endif /* PRIuPTR */
84 #ifdef __GNUC__
85 #define NORET __attribute__((noreturn))
86 #define UNUSED __attribute__ ((unused))
87 #else
88 #define NORET
89 #define UNUSED
90 #endif
92 #ifdef _MSC_VER
94 #define __STDC__ 1
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
103 #define close _close
104 #define dup _dup
105 #define fdopen _fdopen
106 #define fileno _fileno
108 #ifdef _fstati64
109 #define fstat _fstati64
110 #else
111 #define fstat _fstat
112 #endif
114 #define ftime _ftime
115 #define inline __inline
116 #define isatty _isatty
117 #define kbhit _kbhit
118 #define mktemp _mktemp
119 #define off_t _off_t
120 #define open _open
121 #define pclose _pclose
122 #define popen _popen
123 #define POPEN_MODE "rb"
124 #define setmode _setmode
125 #define snprintf _snprintf
127 #ifdef _stati64
128 #define stat _stati64
129 #else
130 #define stat _stat
131 #endif
133 #define strdup _strdup
134 #define timeb _timeb
135 #define unlink _unlink
137 #if defined(HAVE__FSEEKI64) && !defined(HAVE_FSEEKO)
138 #undef off_t
139 #define fseeko _fseeki64
140 #define ftello _ftelli64
141 #define off_t __int64
142 #define HAVE_FSEEKO 1
143 #endif
145 #elif defined(__MINGW32__)
147 #if !defined(HAVE_FSEEKO)
148 #undef off_t
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
155 #endif
157 #endif
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)
163 #else
164 #define LAST_SLASH(path) strrchr(path, '/')
165 #define IS_ABSOLUTE(path) ((path)[0] == '/')
166 #define SET_BINARY_MODE(file)
167 #endif
169 #ifdef WORDS_BIGENDIAN
170 #define MACHINE_IS_BIGENDIAN 1
171 #define MACHINE_IS_LITTLEENDIAN 0
172 #else
173 #define MACHINE_IS_BIGENDIAN 0
174 #define MACHINE_IS_LITTLEENDIAN 1
175 #endif
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 --------------------------------*/
188 #include <math.h>
190 #ifdef min
191 #undef min
192 #endif
193 #define min(a, b) ((a) <= (b) ? (a) : (b))
195 #ifdef max
196 #undef max
197 #endif
198 #define max(a, b) ((a) >= (b) ? (a) : (b))
200 #define range_limit(x, lower, upper) (min(max(x, lower), upper))
202 #ifndef M_PI
203 #define M_PI 3.14159265358979323846
204 #endif
205 #ifndef M_PI_2
206 #define M_PI_2 1.57079632679489661923 /* pi/2 */
207 #endif
208 #ifndef M_LN10
209 #define M_LN10 2.30258509299404568402 /* natural log of 10 */
210 #endif
211 #ifndef M_SQRT2
212 #define M_SQRT2 sqrt(2.)
213 #endif
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))
231 #endif