source: git/src/useful.h @ f96897b

faster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since f96897b was 04078a7, checked in by Olly Betts <olly@…>, 15 months ago

Avoid including <config.h> from headers

Any including file should have included it, so instead just #error
if PACKAGE is not defined.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/* useful.h
2 * Lots of oddments that come in handy generally
3 * Copyright (C) 1993-2003,2004,2010,2011,2014 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#ifndef PACKAGE
25# error config.h must be included first in each C/C++ source file
26#endif
27
28#include <stdint.h>
29#include <stdlib.h> /* for Borland C which #defines max() & min() there */
30#include <stdio.h>
31#include <math.h>
32#include "osalloc.h"
33
34/* Macro to allow easy building of macros contain multiple statements, such
35 * that the likes of “if (x == y) macro1(x); else x = 2;” works properly  */
36#define BLK(X) do {X} while(0)
37
38/* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */
39#define NOP (void)0
40
41/* In C++ code, #include<algorithm> and use std::max and std::min instead. */
42#ifndef __cplusplus
43/* Return max/min of two numbers. */
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#define putnl() putchar('\n')    /* print a newline char */
73#define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */
74/* print a line followed by a newline char to a file */
75#define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));)
76#define sqrd(X) ((X) * (X))        /* macro to square things */
77
78/* 2D Euclidean distance */
79#ifndef HAVE_HYPOT
80# define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
81#endif
82#define rad(X) ((M_PI / 180.0) * (X))  /* convert from degrees to radians */
83#define deg(X) ((180.0 / M_PI) * (X))  /* convert from radians to degrees */
84
85/* macro to convert argument to a string literal */
86#define STRING(X) STRING_(X)
87#define STRING_(X) #X
88
89#include "osdepend.h"
90
91#ifndef WORDS_BIGENDIAN
92# define put16(W, FH) BLK(int16_t w = (W); fwrite(&w, 2, 1, (FH));)
93# define put32(W, FH) BLK(int32_t w = (W); fwrite(&w, 4, 1, (FH));)
94
95# ifdef __GNUC__
96__attribute__((unused))
97# endif
98static inline int16_t get16(FILE *fh) {
99    int16_t w;
100    if (fread(&w, 2, 1, fh) == 0) {
101        /* We check feof() and ferror() afterwards, so checking the return
102         * value achieves nothing, but we get a warning from glibc's
103         * _FORTIFY_SOURCE if we don't pretend to. */
104    }
105    return w;
106}
107
108# ifdef __GNUC__
109__attribute__((unused))
110# endif
111static inline int32_t get32(FILE *fh) {
112    int32_t w;
113    if (fread(&w, 4, 1, fh) == 0) {
114        /* We check feof() and ferror() afterwards, so checking the return
115         * value achieves nothing, but we get a warning from glibc's
116         * _FORTIFY_SOURCE if we don't pretend to. */
117    }
118    return w;
119}
120#else
121void useful_put16(int16_t, FILE *);
122void useful_put32(int32_t, FILE *);
123int16_t useful_get16(FILE *);
124int32_t useful_get32(FILE *);
125
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)
130#endif
131
132#endif /* !USEFUL_H */
Note: See TracBrowser for help on using the repository browser.