-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.c
206 lines (177 loc) · 5.62 KB
/
utils.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* File: utils.c
* Author: Richard Durbin ([email protected])
* Copyright (C) R Durbin, 1996
*-------------------------------------------------------------------
* Description: core utility functions
* Exported functions:
* HISTORY:
* Last edited: Jun 20 13:06 2020 (rd109)
* * Feb 22 14:52 2019 (rd109): added fzopen()
* Created: Thu Aug 15 18:32:26 1996 (rd)
*-------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include "utils.h"
void die (char *format, ...)
{
va_list args ;
va_start (args, format) ;
fprintf (stderr, "FATAL ERROR: ") ;
vfprintf (stderr, format, args) ;
fprintf (stderr, "\n") ;
va_end (args) ;
exit (-1) ;
}
void warn (char *format, ...)
{
va_list args ;
va_start (args, format) ;
fprintf (stderr, "WARNING: ") ;
vfprintf (stderr, format, args) ;
fprintf (stderr, "\n") ;
va_end (args) ;
exit (-1) ;
}
static char* commandLine = 0 ;
void storeCommandLine (int argc, char **argv)
{
int i, totLen = 0 ;
for (i = 0 ; i < argc ; ++i) totLen += 1 + strlen(argv[i]) ;
if (commandLine) free (commandLine) ;
commandLine = new (totLen, char) ;
strcpy (commandLine, argv[0]) ;
for (i = 1 ; i < argc ; ++i) { strcat (commandLine, " ") ; strcat (commandLine, argv[i]) ; }
}
char *getCommandLine (void) { return commandLine ; }
long totalAllocated = 0 ;
void *myalloc (size_t size)
{
void *p = (void*) malloc (size) ;
if (!p) die ("myalloc failure requesting %d bytes", size) ;
totalAllocated += size ;
return p ;
}
void *mycalloc (size_t number, size_t size)
{
void *p = (void*) calloc (number, size) ;
if (!p) die ("mycalloc failure requesting %d objects of size %d", number, size) ;
totalAllocated += size*number ;
return p ;
}
char *fgetword (FILE *f)
{
int n = 0 ;
static char *buf = 0 ;
int bufSize = 64 ;
char *cp ;
if (!buf) buf = (char*) myalloc (bufSize) ;
cp = buf ;
while (!feof (f) && (*cp = getc (f)))
if (isgraph(*cp) && !isspace(*cp))
{ ++cp ; ++n ;
if (n >= bufSize)
{ bufSize *= 2 ;
if (!(buf = (char*) realloc (buf, bufSize)))
die ("fgetword realloc failure requesting %d bytes", bufSize) ;
}
}
else
{ while ((*cp = getc (f)) && (isspace(*cp) || !isgraph(*cp)) && *cp != '\n' && !feof(f)) ;
ungetc (*cp, f) ;
break ;
}
*cp = 0 ;
return buf ;
}
// #define WITH_GZOPEN
#ifdef WITH_GZOPEN
#include <zlib.h>
#endif
FILE *fzopen(const char *path, const char *mode)
{ /* very cool from https://stackoverflow.com/users/3306211/fernando-mut */
#ifdef WITH_GZOPEN
gzFile zfp; /* fernando said *zfp - makes me worry.... */
/* try gzopen */
zfp = gzopen(path,mode);
if (zfp == NULL)
return fopen(path,mode);
/* open file pointer */
return funopen(zfp,
(int(*)(void*,char*,int))gzread,
(int(*)(void*,const char*,int))gzwrite,
(fpos_t(*)(void*,fpos_t,int))gzseek,
(int(*)(void*))gzclose);
#else
return fopen(path,mode);
#endif
}
FILE *fopenTag (char* root, char* tag, char* mode)
{
if (strlen (tag) > 30) die ("tag %s in fopenTag too long - should be < 30 chars", tag) ;
char *fileName = new (strlen (root) + 32, char) ;
strcpy (fileName, root) ;
strcat (fileName, ".") ;
strcat (fileName, tag) ;
FILE *f = fzopen (fileName, mode) ;
free (fileName) ;
return f ;
}
/***************** rusage for timing information ******************/
#include <sys/resource.h>
#ifndef RUSAGE_SELF /* to prevent "RUSAGE_SELF redefined" gcc warning, fixme if this is more intricate */
#define RUSAGE_SELF 0
#endif
#ifdef RUSAGE_STRUCTURE_DEFINITIONS
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* integral max resident set size */
long ru_ixrss; /* integral shared text memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
struct timeval {
time_t tv_sec; /* seconds since Jan. 1, 1970 */
suseconds_t tv_usec; /* and microseconds */
} ;
#endif /* RUSAGE STRUCTURE_DEFINITIONS */
static struct rusage rOld, rFirst ;
void timeUpdate (FILE *f)
{
static bool isFirst = 1 ;
struct rusage rNew ;
int secs, usecs ;
getrusage (RUSAGE_SELF, &rNew) ;
if (!isFirst)
{ secs = rNew.ru_utime.tv_sec - rOld.ru_utime.tv_sec ;
usecs = rNew.ru_utime.tv_usec - rOld.ru_utime.tv_usec ;
if (usecs < 0) { usecs += 1000000 ; secs -= 1 ; }
fprintf (f, "user\t%d.%06d", secs, usecs) ;
secs = rNew.ru_stime.tv_sec - rOld.ru_stime.tv_sec ;
usecs = rNew.ru_stime.tv_usec - rOld.ru_stime.tv_usec ;
if (usecs < 0) { usecs += 1000000 ; secs -= 1 ; }
fprintf (f, "\tsystem\t%d.%06d", secs, usecs) ;
fprintf (f, "\tmax_RSS\t%ld", rNew.ru_maxrss - rOld.ru_maxrss) ;
fprintf (f, "\tmemory\t%li", totalAllocated) ;
fputc ('\n', f) ;
}
else
{ rFirst = rNew ;
isFirst = false ;
}
rOld = rNew ;
}
void timeTotal (FILE *f) { rOld = rFirst ; timeUpdate (f) ; }
/********************* end of file ***********************/