source: git/src/useful.h @ 91a758c

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

Revert "Merge osdepend.c and .h into filename.c and .h"

This change breaks the win32 build. Not seeing why currently but
it was just a code cleanup so revert for now.

This reverts commit ed04c55304f1fd95c126daf9612665b517eab4e7.

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[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
[5b894c3]26#include <stdint.h>
[d1b1380]27#include <stdlib.h> /* for Borland C which #defines max() & min() there */
28#include <stdio.h>
29#include <math.h>
30#include "osalloc.h"
31
32/* Macro to allow easy building of macros contain multiple statements, such
[0804fbe]33 * that the likes of “if (x == y) macro1(x); else x = 2;” works properly  */
[a420b49]34#define BLK(X) do {X} while(0)
[d1b1380]35
36/* Macro to do nothing, but avoid compiler warnings about empty if bodies &c */
37#define NOP (void)0
38
[0580c6a]39/* In C++ code, #include<algorithm> and use std::max and std::min instead. */
40#ifndef __cplusplus
41/* Return max/min of two numbers. */
42/* May be defined already (e.g. by Borland C in stdlib.h) */
[d1b1380]43/* NB Bad news if X or Y has side-effects... */
[0580c6a]44# ifndef max
45#  define max(X, Y) ((X) > (Y) ? (X) : (Y))
46# endif
47# ifndef min
48#  define min(X, Y) ((X) < (Y) ? (X) : (Y))
49# endif
[a420b49]50#endif
[d1b1380]51
[1ec8db0]52/* M_PI, etc may be defined in math.h */
[bca0071]53#ifndef M_PI
54# ifdef PI /* MSVC defines PI IIRC */
55#  define M_PI PI
[d1b1380]56# else
[bca0071]57#  define M_PI 3.14159265358979323846264338327950288419716939937510582097494459
[d1b1380]58# endif
[bca0071]59#endif
[bceebf4]60#ifndef M_PI_2
61# define M_PI_2 (M_PI / 2.0)
62#endif
63#ifndef M_PI_4
64# define M_PI_4 (M_PI / 4.0)
65#endif
[d1b1380]66
[a420b49]67#define MM_PER_INCH 25.4 /* exact value */
[d1b1380]68#define METRES_PER_FOOT 0.3048 /* exact value */
69
70#define putnl() putchar('\n')    /* print a newline char */
[e02a6a6]71#define fputnl(FH) PUTC('\n', (FH)) /* print a newline char to a file */
[d1b1380]72/* print a line followed by a newline char to a file */
[e02a6a6]73#define fputsnl(SZ, FH) BLK(fputs((SZ), (FH)); PUTC('\n', (FH));)
[a420b49]74#define sqrd(X) ((X) * (X))        /* macro to square things */
[27b8b59]75
76/* 2D Euclidean distance */
77#ifndef HAVE_HYPOT
78# define hypot(X, Y) sqrt(sqrd((double)(X)) + sqrd((double)(Y)))
79#endif
[bca0071]80#define rad(X) ((M_PI / 180.0) * (X))  /* convert from degrees to radians */
81#define deg(X) ((180.0 / M_PI) * (X))  /* convert from radians to degrees */
[d1b1380]82
[bd1913f]83/* macro to convert argument to a string literal */
[fa42426]84#define STRING(X) STRING_(X)
85#define STRING_(X) #X
[d1b1380]86
[7dbc68f]87#include "osdepend.h"
88
[c0563da]89#ifndef WORDS_BIGENDIAN
[8c45eea]90# define put16(W, FH) BLK(int16_t w = (W); fwrite(&w, 2, 1, (FH));)
91# define put32(W, FH) BLK(int32_t w = (W); fwrite(&w, 4, 1, (FH));)
[5dd874a]92
[d9434a0]93# ifdef __GNUC__
94__attribute__((unused))
95# endif
[5dd874a]96static inline int16_t get16(FILE *fh) {
97    int16_t w;
[a4454620]98    if (fread(&w, 2, 1, fh) == 0) {
99        /* We check feof() and ferror() afterwards, so checking the return
100         * value achieves nothing, but we get a warning from glibc's
101         * _FORTIFY_SOURCE if we don't pretend to. */
102    }
[5dd874a]103    return w;
104}
105
[d9434a0]106# ifdef __GNUC__
107__attribute__((unused))
108# endif
[5dd874a]109static inline int32_t get32(FILE *fh) {
110    int32_t w;
[a4454620]111    if (fread(&w, 4, 1, fh) == 0) {
112        /* We check feof() and ferror() afterwards, so checking the return
113         * value achieves nothing, but we get a warning from glibc's
114         * _FORTIFY_SOURCE if we don't pretend to. */
115    }
[5dd874a]116    return w;
117}
[c0563da]118#else
[9965b2b]119void useful_put16(int16_t, FILE *);
120void useful_put32(int32_t, FILE *);
121int16_t useful_get16(FILE *);
122int32_t useful_get32(FILE *);
[d1b1380]123
[c0563da]124# define put16(W, FH) useful_put16(W, FH)
125# define put32(W, FH) useful_put32(W, FH)
126# define get16(FH) useful_get16(FH)
127# define get32(FH) useful_get32(FH)
[d1b1380]128#endif
129
130#endif /* !USEFUL_H */
Note: See TracBrowser for help on using the repository browser.