1 | /* useful.h |
---|
2 | * Lots of oddments that come in handy generally |
---|
3 | * Copyright (C) 1993-2003 Olly Betts |
---|
4 | * |
---|
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. |
---|
9 | * |
---|
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. |
---|
14 | * |
---|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | |
---|
20 | /* only include once */ |
---|
21 | #ifndef USEFUL_H |
---|
22 | #define USEFUL_H |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | |
---|
26 | #include <stdlib.h> /* for Borland C which #defines max() & min() there */ |
---|
27 | #include <stdio.h> |
---|
28 | #include <math.h> |
---|
29 | #include "osalloc.h" |
---|
30 | |
---|
31 | /* Macro to allow easy building of macros contain multiple statements, such |
---|
32 | * that the likes of `if (x == y) macro1(x); else x = 2;' works properly */ |
---|
33 | #define BLK(X) do {X} while(0) |
---|
34 | |
---|
35 | /* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */ |
---|
36 | #define NOP (void)0 |
---|
37 | |
---|
38 | /* Return max/min of two numbers (if not defined already) */ |
---|
39 | /* NB Bad news if X or Y has side-effects... */ |
---|
40 | #ifndef max |
---|
41 | # define max(X, Y) ((X) > (Y) ? (X) : (Y)) |
---|
42 | #endif |
---|
43 | #ifndef min |
---|
44 | # define min(X, Y) ((X) < (Y) ? (X) : (Y)) |
---|
45 | #endif |
---|
46 | |
---|
47 | /* M_PI, etc may be defined in math.h */ |
---|
48 | #ifndef M_PI |
---|
49 | # ifdef PI /* MSVC defines PI IIRC */ |
---|
50 | # define M_PI PI |
---|
51 | # else |
---|
52 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
---|
53 | # endif |
---|
54 | #endif |
---|
55 | #ifndef M_PI_2 |
---|
56 | # define M_PI_2 (M_PI / 2.0) |
---|
57 | #endif |
---|
58 | #ifndef M_PI_4 |
---|
59 | # define M_PI_4 (M_PI / 4.0) |
---|
60 | #endif |
---|
61 | |
---|
62 | #define MM_PER_INCH 25.4 /* exact value */ |
---|
63 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
64 | |
---|
65 | /* DJGPP needs these: */ |
---|
66 | |
---|
67 | #ifndef EXIT_FAILURE |
---|
68 | # define EXIT_FAILURE 1 |
---|
69 | #endif /* !EXIT_FAILURE */ |
---|
70 | |
---|
71 | #ifndef EXIT_SUCCESS |
---|
72 | # define EXIT_SUCCESS 0 |
---|
73 | #endif /* !EXIT_SUCCESS */ |
---|
74 | |
---|
75 | #ifndef SEEK_SET |
---|
76 | # define SEEK_SET 0 |
---|
77 | # define SEEK_CUR 1 |
---|
78 | # define SEEK_END 2 |
---|
79 | #endif /* !SEEK_SET */ |
---|
80 | |
---|
81 | /* Older UNIX libraries and DJGPP libraries have HUGE instead of HUGE_VAL */ |
---|
82 | #ifndef HUGE_VAL |
---|
83 | # ifdef HUGE |
---|
84 | # define HUGE_VAL HUGE |
---|
85 | # else |
---|
86 | # error Neither HUGE_VAL nor HUGE is defined |
---|
87 | # endif |
---|
88 | #endif |
---|
89 | |
---|
90 | #include "ostypes.h" |
---|
91 | |
---|
92 | #define putnl() putchar('\n') /* print a newline char */ |
---|
93 | #define fputnl(FH) putc('\n', (FH)) /* print a newline char to a file */ |
---|
94 | /* print a line followed by a newline char to a file */ |
---|
95 | #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); putc('\n', (FH));) |
---|
96 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
---|
97 | |
---|
98 | /* 2D Euclidean distance */ |
---|
99 | #ifndef HAVE_HYPOT |
---|
100 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
---|
101 | #endif |
---|
102 | #define rad(X) ((M_PI / 180.0) * (X)) /* convert from degrees to radians */ |
---|
103 | #define deg(X) ((180.0 / M_PI) * (X)) /* convert from radians to degrees */ |
---|
104 | |
---|
105 | /* macro to convert argument to a string literal */ |
---|
106 | #define STRING(X) STRING_(X) |
---|
107 | #define STRING_(X) #X |
---|
108 | |
---|
109 | #include "osdepend.h" |
---|
110 | |
---|
111 | #ifndef WORDS_BIGENDIAN |
---|
112 | extern INT16_T useful_w16; |
---|
113 | extern INT32_T useful_w32; |
---|
114 | |
---|
115 | # define put16(W, FH) BLK(INT16_T w = (W); fwrite(&w, 2, 1, (FH));) |
---|
116 | # define put32(W, FH) BLK(INT32_T w = (W); fwrite(&w, 4, 1, (FH));) |
---|
117 | # define get16(FH) (fread(&useful_w16, 2, 1, (FH)), useful_w16) |
---|
118 | # define get32(FH) (fread(&useful_w32, 4, 1, (FH)), useful_w32) |
---|
119 | #else |
---|
120 | /* FIXME: why are these "Far"? */ |
---|
121 | void Far useful_put16(INT16_T, FILE *); |
---|
122 | void Far useful_put32(INT32_T, FILE *); |
---|
123 | INT16_T Far useful_get16(FILE *); |
---|
124 | INT32_T Far useful_get32(FILE *); |
---|
125 | |
---|
126 | # define put16(W, FH) useful_put16(W, FH) |
---|
127 | # define put32(W, FH) useful_put32(W, FH) |
---|
128 | # define get16(FH) useful_get16(FH) |
---|
129 | # define get32(FH) useful_get32(FH) |
---|
130 | #endif |
---|
131 | |
---|
132 | #endif /* !USEFUL_H */ |
---|