[421b7d2] | 1 | /* useful.h |
---|
[d1b1380] | 2 | * Lots of oddments that come in handy generally |
---|
[a4454620] | 3 | * Copyright (C) 1993-2003,2004,2010,2011,2014 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 |
---|
[ecbc6c18] | 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 | |
---|
[8c45eea] | 26 | #if HAVE_STDINT_H |
---|
| 27 | # include <stdint.h> |
---|
| 28 | #endif |
---|
| 29 | |
---|
[d1b1380] | 30 | #include <stdlib.h> /* for Borland C which #defines max() & min() there */ |
---|
| 31 | #include <stdio.h> |
---|
| 32 | #include <math.h> |
---|
| 33 | #include "osalloc.h" |
---|
| 34 | |
---|
| 35 | /* Macro to allow easy building of macros contain multiple statements, such |
---|
[0804fbe] | 36 | * that the likes of “if (x == y) macro1(x); else x = 2;” works properly */ |
---|
[a420b49] | 37 | #define BLK(X) do {X} while(0) |
---|
[d1b1380] | 38 | |
---|
| 39 | /* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */ |
---|
| 40 | #define NOP (void)0 |
---|
| 41 | |
---|
[0580c6a] | 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) */ |
---|
[d1b1380] | 46 | /* NB Bad news if X or Y has side-effects... */ |
---|
[0580c6a] | 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 |
---|
[a420b49] | 53 | #endif |
---|
[d1b1380] | 54 | |
---|
[1ec8db0] | 55 | /* M_PI, etc may be defined in math.h */ |
---|
[bca0071] | 56 | #ifndef M_PI |
---|
| 57 | # ifdef PI /* MSVC defines PI IIRC */ |
---|
| 58 | # define M_PI PI |
---|
[d1b1380] | 59 | # else |
---|
[bca0071] | 60 | # define M_PI 3.14159265358979323846264338327950288419716939937510582097494459 |
---|
[d1b1380] | 61 | # endif |
---|
[bca0071] | 62 | #endif |
---|
[bceebf4] | 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 |
---|
[d1b1380] | 69 | |
---|
[a420b49] | 70 | #define MM_PER_INCH 25.4 /* exact value */ |
---|
[d1b1380] | 71 | #define METRES_PER_FOOT 0.3048 /* exact value */ |
---|
| 72 | |
---|
| 73 | /* DJGPP needs these: */ |
---|
| 74 | |
---|
| 75 | #ifndef EXIT_FAILURE |
---|
[ab8da6c] | 76 | # define EXIT_FAILURE 1 |
---|
[d1b1380] | 77 | #endif /* !EXIT_FAILURE */ |
---|
| 78 | |
---|
| 79 | #ifndef EXIT_SUCCESS |
---|
| 80 | # define EXIT_SUCCESS 0 |
---|
| 81 | #endif /* !EXIT_SUCCESS */ |
---|
| 82 | |
---|
| 83 | #ifndef SEEK_SET |
---|
| 84 | # define SEEK_SET 0 |
---|
| 85 | # define SEEK_CUR 1 |
---|
| 86 | # define SEEK_END 2 |
---|
| 87 | #endif /* !SEEK_SET */ |
---|
| 88 | |
---|
| 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 |
---|
| 97 | |
---|
| 98 | #include "ostypes.h" |
---|
| 99 | |
---|
| 100 | #define putnl() putchar('\n') /* print a newline char */ |
---|
[e02a6a6] | 101 | #define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */ |
---|
[d1b1380] | 102 | /* print a line followed by a newline char to a file */ |
---|
[e02a6a6] | 103 | #define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));) |
---|
[a420b49] | 104 | #define sqrd(X) ((X) * (X)) /* macro to square things */ |
---|
[27b8b59] | 105 | |
---|
| 106 | /* 2D Euclidean distance */ |
---|
| 107 | #ifndef HAVE_HYPOT |
---|
| 108 | # define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y))) |
---|
| 109 | #endif |
---|
[bca0071] | 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 */ |
---|
[d1b1380] | 112 | |
---|
[bd1913f] | 113 | /* macro to convert argument to a string literal */ |
---|
[fa42426] | 114 | #define STRING(X) STRING_(X) |
---|
| 115 | #define STRING_(X) #X |
---|
[d1b1380] | 116 | |
---|
| 117 | #include "osdepend.h" |
---|
| 118 | |
---|
[c0563da] | 119 | #ifndef WORDS_BIGENDIAN |
---|
[8c45eea] | 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));) |
---|
[5dd874a] | 122 | |
---|
| 123 | static inline int16_t get16(FILE *fh) { |
---|
| 124 | int16_t w; |
---|
[a4454620] | 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. */ |
---|
| 129 | } |
---|
[5dd874a] | 130 | return w; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | static inline int32_t get32(FILE *fh) { |
---|
| 134 | int32_t w; |
---|
[a4454620] | 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. */ |
---|
| 139 | } |
---|
[5dd874a] | 140 | return w; |
---|
| 141 | } |
---|
[c0563da] | 142 | #else |
---|
[9965b2b] | 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 *); |
---|
[d1b1380] | 147 | |
---|
[c0563da] | 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) |
---|
[d1b1380] | 152 | #endif |
---|
| 153 | |
---|
| 154 | #endif /* !USEFUL_H */ |
---|