aboutsummaryrefslogtreecommitdiffstats
path: root/test/ccured_olden/bh/util.c
blob: b7e3347d597eb651b456ee6c0262ba16abed69e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/****************************************************************************/
/* UTIL: various useful routines and functions.                             */
/*                                                                          */
/* Copyright (c) 1993 by Joshua E. Barnes, Honolulu, HI.                    */
/* It's free because it's yours.                                            */
/****************************************************************************/

#include "stdinc.h"
#include "real.h"
#include "vectmath.h"
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#ifndef _MSVC
#include <sys/times.h>
#include <sys/param.h>
#endif


#ifndef HZ
#  include <time.h>
#  define HZ CLK_TCK
#endif

/*
 * ERROR: print error message and exit.
 */

void error(string msg, ...)
{
    va_list args;

    va_start(args, msg);
    vfprintf(stderr, msg, args);
    va_end(args);
    exit(-1);					/* quit with error status   */
}

/*
 * EPRINTF: print error message, but don't exit.
 */

void eprintf(string msg, ...)
{
    va_list args;

    va_start(args, msg);
    vfprintf(stderr, msg, args);
    va_end(args);
}


extern double drand48(void);			/* should be in math.h      */

/*
 * XRANDOM: generate floating-point random number.
 */

real xrandom(real xl, real xh)
{
    real r = (real)rand() / (real)RAND_MAX; 
    return (xl + (xh - xl) * r);
}

/*
 * RSQR: compute x*x.
 */

real rsqr(real x)
{
    return (x * x);
}

/*
 * DISTV: subtract vectors and return distance between.
 */

real distv(vector v, vector u)
{
    real s, d;
    int n = NDIM;

    s = 0.0;
    while (--n >= 0) {
	d = (*v++) - (*u++);
	s += d * d;
    }
    return (rsqrt(s));
}

/*
 * STREQ: test for equality of strings.
 */

bool streq(string a, string b)
{
    return (strcmp(a, b) == 0);
}

/*
 * SCANOPT: scan string of the form "word1,word2,..." for match. Warning:
 * words must be separated by exactly one comma -- no spaces allowed!
 */

bool scanopt(string opt, string key)
{
    char *op, *kp;

    op = (char *) opt;				/* start scan of options    */
    while (*op != NULLCHR) {			/* loop over words in opt   */
	kp = (char *) key;			/*   start at front of key  */
	while ((*op != ',' ? *op : NULLCHR)	/*   loop while this word   */
	         == *kp) {			/*   ...matches text of key */
	    if (*kp++ == NULLCHR)			/*     at end of key word?  */
		return (TRUE);			/*       keyword found      */
	    op++;				/*     else advance ptrs    */
	}
	while (*op != NULLCHR && *op++ != ',')	/*   loop till end of word, */
	    continue;				/*     passing "," at end   */
    }
    return (FALSE);				/* keyword not found        */
}

/*
 * CPUTIME: compute CPU time in minutes.
 */

real cputime()
{
#ifdef _MSVC
    return 1.0;
#else
    struct tms buffer;

    if (times(&buffer) == -1)
	error("times() call failed\n");
    return (buffer.tms_utime / (60.0 * HZ));
#endif
}


void *allocate(nb)
int nb;
{
    void *mem;

    mem = (void *) calloc(nb, 1);		/* calloc zeros memory      */
    if (mem == NULL)
	error("allocate: not enuf memory (%d bytes)\n", nb);
    return (mem);
}