-
Notifications
You must be signed in to change notification settings - Fork 2
/
CUtils.cxx
147 lines (132 loc) · 3.11 KB
/
CUtils.cxx
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#ifdef USE_FTIME
#include <sys/timeb.h>
#elif defined(USE_GETTIMEOFDAY)
#include <sys/time.h>
#include <unistd.h>
#endif
#include "CUtils.h"
char *progname = "xsnoed";
char *progpath = "xsnoed";
#ifdef VAX
unsigned long LIB$WAIT( float* );
#endif
/*---------------------------------------------------------------------------
*/
void setProgname(char *aFilename, char *aPathname)
{
progname = aFilename;
progpath = aPathname;
}
/*
* get current accurate time in seconds using system clock - PH 02/01/98
*/
double double_time(void)
{
#ifdef USE_FTIME
/* support for systems without clock_gettime() - PH 08/12/98 */
struct timeb tb;
ftime(&tb);
return(tb.time + 1e-3 * tb.millitm);
#elif defined(USE_GETTIMEOFDAY)
/* add support for systems without clock_gettime() or ftime() - PH 01/20/99 */
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
return(tv.tv_sec + 1e-6 * tv.tv_usec);
#else
struct timespec ts;
/* (rumoured to be accurate to about 25ms) */
clock_gettime(CLOCK_REALTIME, &ts);
return(ts.tv_sec + 1e-9 * ts.tv_nsec);
#endif
}
/* portable sleep routine */
void usleep_(unsigned long usec)
{
#ifdef USE_NANOSLEEP
struct timespec ts;
ts.tv_sec = usec / 1000000UL;
ts.tv_nsec = (usec - ts.tv_sec * 1000000UL) * 1000;
nanosleep(&ts,NULL);
#elif VAX
float secs;
secs = usec * 0.000001;
LIB$WAIT(&secs);
#else
usleep(usec);
#endif
}
/* convert a 4-character barcode to an integer */
int bc2int(char* bc, char first)
{
int n;
if( *bc != first ) return( -1);
for( n=1; n<4; ++n ) if( bc[n]<'A' || bc[n]>'Z' ) return( -1);
n = ( ( bc[1]-'A' ) *26 + bc[2]-'A' ) * 26 + bc[3]-'A';
return( n<0 ? -1 : n );
}
/* convert an integer to a 4 character barcode */
char *int2bc(int barcode, char first)
{
static char bc[5];
if( barcode<0 ) {
strcpy( bc, " ");
} else {
bc[4] = 0;
bc[3] = barcode%26 + 'A';
bc[2] = (barcode/26)%26 + 'A';
bc[1] = (barcode/676)%26 + 'A';
bc[0] = first;
}
return( bc );
}
/* Printf buffer variables */
static char *sPrintfBuffer = NULL;
static int sPrintfBufferMax = 0;
static int sPrintfBufferLen = 0;
void SetPrintfOutput(char *buff, int size)
{
sPrintfBuffer = buff;
sPrintfBufferMax = size;
sPrintfBufferLen = 0;
if (buff && size) *buff = '\0'; /* start with null string in buffer */
}
#ifdef VAX
// necessary to avoid conflict with printf on VAX
int vaxPrintf(char *fmt,...)
#else
int Printf(char *fmt,...)
#endif
{
int len = 0;
va_list varArgList;
va_start(varArgList,fmt);
if (sPrintfBuffer) {
/* check to see if buffer is getting too full */
if (sPrintfBufferLen + 80 > sPrintfBufferMax) {
printf("%s: Printf buffer too full!\n", progname);
} else {
/* add output to buffer */
char *pt = strchr(sPrintfBuffer, '\0');
len = sprintf(pt,"%s: ",progname);
len += vsprintf(pt+len,fmt,varArgList);
sPrintfBufferLen = pt - sPrintfBuffer + len;
}
} else {
len = printf("%s: ", progname);
len += vprintf(fmt,varArgList);
}
va_end(varArgList);
return(len);
}
void quit(char *msg)
{
printf("%s: %s\n",progname,msg);
exit(0);
}