| 1 | /* useful.h |
|---|
| 2 | * Lots of oddments that come in handy generally |
|---|
| 3 | * Copyright (C) 1993-2003,2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 | /* In C++ code, #include<algorithm> and use std::max and std::min instead. */ |
|---|
| 39 | #ifndef __cplusplus |
|---|
| 40 | /* Return max/min of two numbers. */ |
|---|
| 41 | /* May be defined already (e.g. by Borland C in stdlib.h) */ |
|---|
| 42 | /* NB Bad news if X or Y has side-effects... */ |
|---|
| 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 |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | /* M_PI, etc may be defined in math.h */ |
|---|
| 52 | #ifndef M_PI |
|---|
| 53 | # ifdef PI /* MSVC defines PI IIRC */ |
|---|
| 54 | # define M_PI PI |
|---|
| 55 | # else |
|---|
| 56 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
|---|
| 57 | # endif |
|---|
| 58 | #endif |
|---|
| 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 |
|---|
| 65 | |
|---|
| 66 | #define MM_PER_INCH 25.4 /* exact value */ |
|---|
| 67 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
|---|
| 68 | |
|---|
| 69 | /* DJGPP needs these: */ |
|---|
| 70 | |
|---|
| 71 | #ifndef EXIT_FAILURE |
|---|
| 72 | # define EXIT_FAILURE 1 |
|---|
| 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 */ |
|---|
| 97 | #define fputnl(FH) putc('\n', (FH)) /* print a newline char to a file */ |
|---|
| 98 | /* print a line followed by a newline char to a file */ |
|---|
| 99 | #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); putc('\n', (FH));) |
|---|
| 100 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
|---|
| 101 | |
|---|
| 102 | /* 2D Euclidean distance */ |
|---|
| 103 | #ifndef HAVE_HYPOT |
|---|
| 104 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
|---|
| 105 | #endif |
|---|
| 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 */ |
|---|
| 108 | |
|---|
| 109 | /* macro to convert argument to a string literal */ |
|---|
| 110 | #define STRING(X) STRING_(X) |
|---|
| 111 | #define STRING_(X) #X |
|---|
| 112 | |
|---|
| 113 | #include "osdepend.h" |
|---|
| 114 | |
|---|
| 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 |
|---|
| 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 *); |
|---|
| 129 | |
|---|
| 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) |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|
| 136 | #endif /* !USEFUL_H */ |
|---|