[421b7d2] | 1 | /* useful.h |
---|
[d1b1380] | 2 | * Lots of oddments that come in handy generally |
---|
[3ff3478] | 3 | * Copyright (C) 1993-2003,2004 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 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. |
---|
[846746e] | 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 |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 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 |
---|
[1b71c05] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
| 20 | /* only include once */ |
---|
| 21 | #ifndef USEFUL_H |
---|
| 22 | #define USEFUL_H |
---|
| 23 | |
---|
[fa42426] | 24 | #include <config.h> |
---|
| 25 | |
---|
[d1b1380] | 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 |
---|
[a420b49] | 32 | * that the likes of `if (x == y) macro1(x); else x = 2;' works properly */ |
---|
| 33 | #define BLK(X) do {X} while(0) |
---|
[d1b1380] | 34 | |
---|
| 35 | /* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */ |
---|
| 36 | #define NOP (void)0 |
---|
| 37 | |
---|
[0be7a88] | 38 | /* In C++ code, #include<algorithm> and use std::max and std::min instead. */ |
---|
| 39 | #ifndef __cplusplus |
---|
[3ff3478] | 40 | /* Return max/min of two numbers. */ |
---|
| 41 | /* May be defined already (e.g. by Borland C in stdlib.h) */ |
---|
[d1b1380] | 42 | /* NB Bad news if X or Y has side-effects... */ |
---|
[3ff3478] | 43 | # ifndef max |
---|
| 44 | # define max(X, Y) ((X) > (Y) ? (X) : (Y)) |
---|
| 45 | # endif |
---|
| 46 | # ifndef min |
---|
| 47 | # define min(X, Y) ((X) < (Y) ? (X) : (Y)) |
---|
| 48 | # endif |
---|
[a420b49] | 49 | #endif |
---|
[d1b1380] | 50 | |
---|
[1ec8db0] | 51 | /* M_PI, etc may be defined in math.h */ |
---|
[bca0071] | 52 | #ifndef M_PI |
---|
| 53 | # ifdef PI /* MSVC defines PI IIRC */ |
---|
| 54 | # define M_PI PI |
---|
[d1b1380] | 55 | # else |
---|
[bca0071] | 56 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
---|
[d1b1380] | 57 | # endif |
---|
[bca0071] | 58 | #endif |
---|
[bceebf4] | 59 | #ifndef M_PI_2 |
---|
| 60 | # define M_PI_2 (M_PI / 2.0) |
---|
| 61 | #endif |
---|
| 62 | #ifndef M_PI_4 |
---|
| 63 | # define M_PI_4 (M_PI / 4.0) |
---|
| 64 | #endif |
---|
[d1b1380] | 65 | |
---|
[a420b49] | 66 | #define MM_PER_INCH 25.4 /* exact value */ |
---|
[d1b1380] | 67 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
| 68 | |
---|
| 69 | /* DJGPP needs these: */ |
---|
| 70 | |
---|
| 71 | #ifndef EXIT_FAILURE |
---|
[ab8da6c] | 72 | # define EXIT_FAILURE 1 |
---|
[d1b1380] | 73 | #endif /* !EXIT_FAILURE */ |
---|
| 74 | |
---|
| 75 | #ifndef EXIT_SUCCESS |
---|
| 76 | # define EXIT_SUCCESS 0 |
---|
| 77 | #endif /* !EXIT_SUCCESS */ |
---|
| 78 | |
---|
| 79 | #ifndef SEEK_SET |
---|
| 80 | # define SEEK_SET 0 |
---|
| 81 | # define SEEK_CUR 1 |
---|
| 82 | # define SEEK_END 2 |
---|
| 83 | #endif /* !SEEK_SET */ |
---|
| 84 | |
---|
| 85 | /* Older UNIX libraries and DJGPP libraries have HUGE instead of HUGE_VAL */ |
---|
| 86 | #ifndef HUGE_VAL |
---|
| 87 | # ifdef HUGE |
---|
| 88 | # define HUGE_VAL HUGE |
---|
| 89 | # else |
---|
| 90 | # error Neither HUGE_VAL nor HUGE is defined |
---|
| 91 | # endif |
---|
| 92 | #endif |
---|
| 93 | |
---|
| 94 | #include "ostypes.h" |
---|
| 95 | |
---|
| 96 | #define putnl() putchar('\n') /* print a newline char */ |
---|
[870439d] | 97 | #define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */ |
---|
[d1b1380] | 98 | /* print a line followed by a newline char to a file */ |
---|
[870439d] | 99 | #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));) |
---|
[a420b49] | 100 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
---|
[489529e] | 101 | |
---|
| 102 | /* 2D Euclidean distance */ |
---|
| 103 | #ifndef HAVE_HYPOT |
---|
| 104 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
---|
| 105 | #endif |
---|
[bca0071] | 106 | #define rad(X) ((M_PI / 180.0) * (X)) /* convert from degrees to radians */ |
---|
| 107 | #define deg(X) ((180.0 / M_PI) * (X)) /* convert from radians to degrees */ |
---|
[d1b1380] | 108 | |
---|
[bd1913f] | 109 | /* macro to convert argument to a string literal */ |
---|
[fa42426] | 110 | #define STRING(X) STRING_(X) |
---|
| 111 | #define STRING_(X) #X |
---|
[d1b1380] | 112 | |
---|
| 113 | #include "osdepend.h" |
---|
| 114 | |
---|
[c0563da] | 115 | #ifndef WORDS_BIGENDIAN |
---|
| 116 | extern INT16_T useful_w16; |
---|
| 117 | extern INT32_T useful_w32; |
---|
| 118 | |
---|
| 119 | # define put16(W, FH) BLK(INT16_T w = (W); fwrite(&w, 2, 1, (FH));) |
---|
| 120 | # define put32(W, FH) BLK(INT32_T w = (W); fwrite(&w, 4, 1, (FH));) |
---|
| 121 | # define get16(FH) (fread(&useful_w16, 2, 1, (FH)), useful_w16) |
---|
| 122 | # define get32(FH) (fread(&useful_w32, 4, 1, (FH)), useful_w32) |
---|
| 123 | #else |
---|
[caa8d8d] | 124 | /* FIXME: why are these "Far"? */ |
---|
| 125 | void Far useful_put16(INT16_T, FILE *); |
---|
| 126 | void Far useful_put32(INT32_T, FILE *); |
---|
| 127 | INT16_T Far useful_get16(FILE *); |
---|
| 128 | INT32_T Far useful_get32(FILE *); |
---|
[d1b1380] | 129 | |
---|
[c0563da] | 130 | # define put16(W, FH) useful_put16(W, FH) |
---|
| 131 | # define put32(W, FH) useful_put32(W, FH) |
---|
| 132 | # define get16(FH) useful_get16(FH) |
---|
| 133 | # define get32(FH) useful_get32(FH) |
---|
[d1b1380] | 134 | #endif |
---|
| 135 | |
---|
| 136 | #endif /* !USEFUL_H */ |
---|