Fix coding style
[survex.git] / src / useful.h
blob0598813612eacf625e681e07acc394fb9d19a28c
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 #include <config.h>
26 #if HAVE_STDINT_H
27 # include <stdint.h>
28 #endif
30 #include <stdlib.h> /* for Borland C which #defines max() & min() there */
31 #include <stdio.h>
32 #include <math.h>
33 #include "osalloc.h"
35 /* Macro to allow easy building of macros contain multiple statements, such
36 * that the likes of “if (x == y) macro1(x); else x = 2;” works properly */
37 #define BLK(X) do {X} while(0)
39 /* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */
40 #define NOP (void)0
42 /* In C++ code, #include<algorithm> and use std::max and std::min instead. */
43 #ifndef __cplusplus
44 /* Return max/min of two numbers. */
45 /* May be defined already (e.g. by Borland C in stdlib.h) */
46 /* NB Bad news if X or Y has side-effects... */
47 # ifndef max
48 # define max(X, Y) ((X) > (Y) ? (X) : (Y))
49 # endif
50 # ifndef min
51 # define min(X, Y) ((X) < (Y) ? (X) : (Y))
52 # endif
53 #endif
55 /* M_PI, etc may be defined in math.h */
56 #ifndef M_PI
57 # ifdef PI /* MSVC defines PI IIRC */
58 # define M_PI PI
59 # else
60 # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459
61 # endif
62 #endif
63 #ifndef M_PI_2
64 # define M_PI_2 (M_PI / 2.0)
65 #endif
66 #ifndef M_PI_4
67 # define M_PI_4 (M_PI / 4.0)
68 #endif
70 #define MM_PER_INCH 25.4 /* exact value */
71 #define METRES_PER_FOOT 0.3048 /* exact value */
73 /* DJGPP needs these: */
75 #ifndef EXIT_FAILURE
76 # define EXIT_FAILURE 1
77 #endif /* !EXIT_FAILURE */
79 #ifndef EXIT_SUCCESS
80 # define EXIT_SUCCESS 0
81 #endif /* !EXIT_SUCCESS */
83 #ifndef SEEK_SET
84 # define SEEK_SET 0
85 # define SEEK_CUR 1
86 # define SEEK_END 2
87 #endif /* !SEEK_SET */
89 /* Older UNIX libraries and DJGPP libraries have HUGE instead of HUGE_VAL */
90 #ifndef HUGE_VAL
91 # ifdef HUGE
92 # define HUGE_VAL HUGE
93 # else
94 # error Neither HUGE_VAL nor HUGE is defined
95 # endif
96 #endif
98 #include "ostypes.h"
100 #define putnl() putchar('\n') /* print a newline char */
101 #define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */
102 /* print a line followed by a newline char to a file */
103 #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));)
104 #define sqrd(X) ((X) * (X)) /* macro to square things */
106 /* 2D Euclidean distance */
107 #ifndef HAVE_HYPOT
108 # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
109 #endif
110 #define rad(X) ((M_PI / 180.0) * (X)) /* convert from degrees to radians */
111 #define deg(X) ((180.0 / M_PI) * (X)) /* convert from radians to degrees */
113 /* macro to convert argument to a string literal */
114 #define STRING(X) STRING_(X)
115 #define STRING_(X) #X
117 #include "osdepend.h"
119 #ifndef WORDS_BIGENDIAN
120 # define put16(W, FH) BLK(int16_t w = (W); fwrite(&w, 2, 1, (FH));)
121 # define put32(W, FH) BLK(int32_t w = (W); fwrite(&w, 4, 1, (FH));)
123 static inline int16_t get16(FILE *fh) {
124 int16_t w;
125 if (fread(&w, 2, 1, fh) == 0) {
126 /* We check feof() and ferror() afterwards, so checking the return
127 * value achieves nothing, but we get a warning from glibc's
128 * _FORTIFY_SOURCE if we don't pretend to. */
130 return w;
133 static inline int32_t get32(FILE *fh) {
134 int32_t w;
135 if (fread(&w, 4, 1, fh) == 0) {
136 /* We check feof() and ferror() afterwards, so checking the return
137 * value achieves nothing, but we get a warning from glibc's
138 * _FORTIFY_SOURCE if we don't pretend to. */
140 return w;
142 #else
143 void useful_put16(int16_t, FILE *);
144 void useful_put32(int32_t, FILE *);
145 int16_t useful_get16(FILE *);
146 int32_t useful_get32(FILE *);
148 # define put16(W, FH) useful_put16(W, FH)
149 # define put32(W, FH) useful_put32(W, FH)
150 # define get16(FH) useful_get16(FH)
151 # define get32(FH) useful_get32(FH)
152 #endif
154 #endif /* !USEFUL_H */