formats: clarify setting of reverse_bytes
[sox.git] / src / util.h
blobfa341285615683e83100def080a6cdcc85452df3
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 <inttypes.h>
23 #include "soxconfig.h"
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h> /* For off_t not found in stdio.h */
27 #endif
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h> /* Needs to be included before we redefine off_t. */
31 #endif
33 #include "xmalloc.h"
35 /*---------------------------- Portability stuff -----------------------------*/
37 #ifdef __GNUC__
38 #define NORET __attribute__((noreturn))
39 #define UNUSED __attribute__ ((unused))
40 #else
41 #define NORET
42 #define UNUSED
43 #endif
45 #ifdef _MSC_VER
47 #define __STDC__ 1
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
56 #define close _close
57 #define dup _dup
58 #define fdopen _fdopen
59 #define fileno _fileno
61 #ifdef _fstati64
62 #define fstat _fstati64
63 #else
64 #define fstat _fstat
65 #endif
67 #define ftime _ftime
68 #define inline __inline
69 #define isatty _isatty
70 #define kbhit _kbhit
71 #define mktemp _mktemp
72 #define off_t _off_t
73 #define open _open
74 #define pclose _pclose
75 #define popen _popen
76 #define setmode _setmode
77 #define snprintf _snprintf
79 #ifdef _stati64
80 #define stat _stati64
81 #else
82 #define stat _stat
83 #endif
85 #define strdup _strdup
86 #define timeb _timeb
87 #define unlink _unlink
89 #if defined(HAVE__FSEEKI64) && !defined(HAVE_FSEEKO)
90 #undef off_t
91 #define fseeko _fseeki64
92 #define ftello _ftelli64
93 #define off_t __int64
94 #define HAVE_FSEEKO 1
95 #endif
97 #elif defined(__MINGW32__)
99 #if !defined(HAVE_FSEEKO)
100 #undef off_t
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
107 #endif
109 #endif
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"
116 #else
117 #define LAST_SLASH(path) strrchr(path, '/')
118 #define IS_ABSOLUTE(path) ((path)[0] == '/')
119 #define SET_BINARY_MODE(file)
120 #endif
122 #ifdef WORDS_BIGENDIAN
123 #define MACHINE_IS_BIGENDIAN 1
124 #define MACHINE_IS_LITTLEENDIAN 0
125 #else
126 #define MACHINE_IS_BIGENDIAN 0
127 #define MACHINE_IS_LITTLEENDIAN 1
128 #endif
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 --------------------------------*/
141 #include <math.h>
143 #ifdef min
144 #undef min
145 #endif
146 #define min(a, b) ((a) <= (b) ? (a) : (b))
148 #ifdef max
149 #undef max
150 #endif
151 #define max(a, b) ((a) >= (b) ? (a) : (b))
153 #define range_limit(x, lower, upper) (min(max(x, lower), upper))
155 #ifndef M_PI
156 #define M_PI 3.14159265358979323846
157 #endif
158 #ifndef M_PI_2
159 #define M_PI_2 1.57079632679489661923 /* pi/2 */
160 #endif
161 #ifndef M_LN10
162 #define M_LN10 2.30258509299404568402 /* natural log of 10 */
163 #endif
164 #ifndef M_SQRT2
165 #define M_SQRT2 sqrt(2.)
166 #endif
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))
184 #endif