[421b7d2] | 1 | /* useful.h |
---|
[d1b1380] | 2 | * Lots of oddments that come in handy generally |
---|
[64d37a3] | 3 | * Copyright (C) 1993-2003 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 |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 | |
---|
| 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 |
---|
[a420b49] | 41 | # define max(X, Y) ((X) > (Y) ? (X) : (Y)) |
---|
| 42 | #endif |
---|
[d1b1380] | 43 | #ifndef min |
---|
[a420b49] | 44 | # define min(X, Y) ((X) < (Y) ? (X) : (Y)) |
---|
| 45 | #endif |
---|
[d1b1380] | 46 | |
---|
[1ec8db0] | 47 | /* M_PI, etc may be defined in math.h */ |
---|
[bca0071] | 48 | #ifndef M_PI |
---|
| 49 | # ifdef PI /* MSVC defines PI IIRC */ |
---|
| 50 | # define M_PI PI |
---|
[d1b1380] | 51 | # else |
---|
[bca0071] | 52 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
---|
[d1b1380] | 53 | # endif |
---|
[bca0071] | 54 | #endif |
---|
[bceebf4] | 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 |
---|
[d1b1380] | 61 | |
---|
[a420b49] | 62 | #define MM_PER_INCH 25.4 /* exact value */ |
---|
[d1b1380] | 63 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
| 64 | |
---|
| 65 | /* DJGPP needs these: */ |
---|
| 66 | |
---|
| 67 | #ifndef EXIT_FAILURE |
---|
[ab8da6c] | 68 | # define EXIT_FAILURE 1 |
---|
[d1b1380] | 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 */ |
---|
[a420b49] | 93 | #define fputnl(FH) putc('\n', (FH)) /* print a newline char to a file */ |
---|
[d1b1380] | 94 | /* print a line followed by a newline char to a file */ |
---|
[a420b49] | 95 | #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); putc('\n', (FH));) |
---|
| 96 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
---|
[27b8b59] | 97 | |
---|
| 98 | /* 2D Euclidean distance */ |
---|
| 99 | #ifndef HAVE_HYPOT |
---|
| 100 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
---|
| 101 | #endif |
---|
[bca0071] | 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 */ |
---|
[d1b1380] | 104 | |
---|
[bd1913f] | 105 | /* macro to convert argument to a string literal */ |
---|
[fa42426] | 106 | #define STRING(X) STRING_(X) |
---|
| 107 | #define STRING_(X) #X |
---|
[d1b1380] | 108 | |
---|
| 109 | #include "osdepend.h" |
---|
| 110 | |
---|
[c0563da] | 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 |
---|
[64d37a3] | 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 *); |
---|
[d1b1380] | 125 | |
---|
[c0563da] | 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) |
---|
[d1b1380] | 130 | #endif |
---|
| 131 | |
---|
| 132 | #endif /* !USEFUL_H */ |
---|