Try to eliminate osdepend.{c,h} again
[survex.git] / src / useful.h
blob216d5bed4b3d83f79701bdcb5e2c3ed6d491e294
1 /* useful.h
2 * Lots of oddments that come in handy generally
3 * Copyright (C) 1993-2003,2004,2010,2011,2014 Olly Betts
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* only include once */
21 #ifndef USEFUL_H
22 #define USEFUL_H
24 #ifndef PACKAGE
25 # error config.h must be included first in each C/C++ source file
26 #endif
28 #include <stdint.h>
29 #include <stdlib.h> /* for Borland C which #defines max() & min() there */
30 #include <stdio.h>
31 #include <math.h>
33 /* Macro to allow easy building of macros contain multiple statements, such
34 * that the likes of “if (x == y) macro1(x); else x = 2;” works properly */
35 #define BLK(X) do {X} while(0)
37 /* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */
38 #define NOP (void)0
40 /* In C++ code, #include<algorithm> and use std::max and std::min instead. */
41 #ifndef __cplusplus
42 /* Return max/min of two numbers. */
43 /* May be defined already (e.g. by Borland C in stdlib.h) */
44 /* NB Bad news if X or Y has side-effects... */
45 # ifndef max
46 # define max(X, Y) ((X) > (Y) ? (X) : (Y))
47 # endif
48 # ifndef min
49 # define min(X, Y) ((X) < (Y) ? (X) : (Y))
50 # endif
51 #endif
53 /* M_PI, etc may be defined in math.h */
54 #ifndef M_PI
55 # ifdef PI /* MSVC defines PI IIRC */
56 # define M_PI PI
57 # else
58 # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459
59 # endif
60 #endif
61 #ifndef M_PI_2
62 # define M_PI_2 (M_PI / 2.0)
63 #endif
64 #ifndef M_PI_4
65 # define M_PI_4 (M_PI / 4.0)
66 #endif
68 #define MM_PER_INCH 25.4 /* exact value */
69 #define METRES_PER_FOOT 0.3048 /* exact value */
71 #define putnl() putchar('\n') /* print a newline char */
72 #define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */
73 /* print a line followed by a newline char to a file */
74 #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));)
75 #define sqrd(X) ((X) * (X)) /* macro to square things */
77 /* 2D Euclidean distance */
78 #ifndef HAVE_HYPOT
79 # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
80 #endif
81 #define rad(X) ((M_PI / 180.0) * (X)) /* convert from degrees to radians */
82 #define deg(X) ((180.0 / M_PI) * (X)) /* convert from radians to degrees */
84 /* macro to convert argument to a string literal */
85 #define STRING(X) STRING_(X)
86 #define STRING_(X) #X
88 #ifndef WORDS_BIGENDIAN
89 # define put16(W, FH) BLK(int16_t w = (W); fwrite(&w, 2, 1, (FH));)
90 # define put32(W, FH) BLK(int32_t w = (W); fwrite(&w, 4, 1, (FH));)
92 # ifdef __GNUC__
93 __attribute__((unused))
94 # endif
95 static inline int16_t get16(FILE *fh) {
96 int16_t w;
97 if (fread(&w, 2, 1, fh) == 0) {
98 /* We check feof() and ferror() afterwards, so checking the return
99 * value achieves nothing, but we get a warning from glibc's
100 * _FORTIFY_SOURCE if we don't pretend to. */
102 return w;
105 # ifdef __GNUC__
106 __attribute__((unused))
107 # endif
108 static inline int32_t get32(FILE *fh) {
109 int32_t w;
110 if (fread(&w, 4, 1, fh) == 0) {
111 /* We check feof() and ferror() afterwards, so checking the return
112 * value achieves nothing, but we get a warning from glibc's
113 * _FORTIFY_SOURCE if we don't pretend to. */
115 return w;
117 #else
118 void useful_put16(int16_t, FILE *);
119 void useful_put32(int32_t, FILE *);
120 int16_t useful_get16(FILE *);
121 int32_t useful_get32(FILE *);
123 # define put16(W, FH) useful_put16(W, FH)
124 # define put32(W, FH) useful_put32(W, FH)
125 # define get16(FH) useful_get16(FH)
126 # define get32(FH) useful_get32(FH)
127 #endif
129 #endif /* !USEFUL_H */