source: git/src/useful.h @ 3ff3478

RELEASE/1.0RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 3ff3478 was 3ff3478, checked in by Olly Betts <olly@…>, 20 years ago

Fix to allow compilation with GCC 3.4

git-svn-id: file:///home/survex-svn/survex/trunk@2709 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 3.9 KB
Line 
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., 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. */
39#ifdef __cplusplus
40# include <algorithm>
41using std::min;
42using std::max;
43#else
44/* May be defined already (e.g. by Borland C in stdlib.h) */
45/* NB Bad news if X or Y has side-effects... */
46# ifndef max
47#  define max(X, Y) ((X) > (Y) ? (X) : (Y))
48# endif
49# ifndef min
50#  define min(X, Y) ((X) < (Y) ? (X) : (Y))
51# endif
52#endif
53
54/* M_PI, etc may be defined in math.h */
55#ifndef M_PI
56# ifdef PI /* MSVC defines PI IIRC */
57#  define M_PI PI
58# else
59#  define M_PI 3.14159265358979323846264338327950288419716939937510582097494459
60# endif
61#endif
62#ifndef M_PI_2
63# define M_PI_2 (M_PI / 2.0)
64#endif
65#ifndef M_PI_4
66# define M_PI_4 (M_PI / 4.0)
67#endif
68
69#define MM_PER_INCH 25.4 /* exact value */
70#define METRES_PER_FOOT 0.3048 /* exact value */
71
72/* DJGPP needs these: */
73
74#ifndef EXIT_FAILURE
75# define EXIT_FAILURE 1
76#endif /* !EXIT_FAILURE */
77
78#ifndef EXIT_SUCCESS
79# define EXIT_SUCCESS 0
80#endif /* !EXIT_SUCCESS */
81
82#ifndef SEEK_SET
83# define SEEK_SET 0
84# define SEEK_CUR 1
85# define SEEK_END 2
86#endif /* !SEEK_SET */
87
88/* Older UNIX libraries and DJGPP libraries have HUGE instead of HUGE_VAL */
89#ifndef HUGE_VAL
90# ifdef HUGE
91#  define HUGE_VAL HUGE
92# else
93#  error Neither HUGE_VAL nor HUGE is defined
94# endif
95#endif
96
97#include "ostypes.h"
98
99#define putnl() putchar('\n')    /* print a newline char */
100#define fputnl(FH) putc('\n', (FH)) /* print a newline char to a file */
101/* print a line followed by a newline char to a file */
102#define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); putc('\n', (FH));)
103#define sqrd(X) ((X) * (X))        /* macro to square things */
104
105/* 2D Euclidean distance */
106#ifndef HAVE_HYPOT
107# define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
108#endif
109#define rad(X) ((M_PI / 180.0) * (X))  /* convert from degrees to radians */
110#define deg(X) ((180.0 / M_PI) * (X))  /* convert from radians to degrees */
111
112/* macro to convert argument to a string literal */
113#define STRING(X) STRING_(X)
114#define STRING_(X) #X
115
116#include "osdepend.h"
117
118#ifndef WORDS_BIGENDIAN
119extern INT16_T useful_w16;
120extern INT32_T useful_w32;
121
122# define put16(W, FH) BLK(INT16_T w = (W); fwrite(&w, 2, 1, (FH));)
123# define put32(W, FH) BLK(INT32_T w = (W); fwrite(&w, 4, 1, (FH));)
124# define get16(FH) (fread(&useful_w16, 2, 1, (FH)), useful_w16)
125# define get32(FH) (fread(&useful_w32, 4, 1, (FH)), useful_w32)
126#else
127/* FIXME: why are these "Far"? */
128void Far useful_put16(INT16_T, FILE *);
129void Far useful_put32(INT32_T, FILE *);
130INT16_T Far useful_get16(FILE *);
131INT32_T Far useful_get32(FILE *);
132
133# define put16(W, FH) useful_put16(W, FH)
134# define put32(W, FH) useful_put32(W, FH)
135# define get16(FH) useful_get16(FH)
136# define get32(FH) useful_get32(FH)
137#endif
138
139#endif /* !USEFUL_H */
Note: See TracBrowser for help on using the repository browser.