forked from richarddurbin/pbwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
200 lines (172 loc) · 6.02 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
/* File: utils.c
* Author: Richard Durbin ([email protected])
* Modified by Daniel Lawson ([email protected]) in December 2014, adding gzip output for paintSparse
* Copyright (C) Genome Research Limited, 1996-
*-------------------------------------------------------------------
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any later
* version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*-------------------------------------------------------------------
* Description: core utility functions
* Exported functions:
* HISTORY:
* Last edited: Dec 28 14:02 2014 (dl)
* adding gzip output for paintSparse
* 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) ;
timeUpdate (stderr) ;
exit (-1) ;
}
void warn (char *format, ...)
{
static int count = 0 ;
va_list args ;
va_start (args, format) ;
fprintf (stderr, "ERROR: ") ;
vfprintf (stderr, format, args) ;
fprintf (stderr, "\n") ;
va_end (args) ;
if (++count > 9) die ("too many errors") ;
}
long int totalAllocated = 0 ;
void *_myalloc (long size)
{
void *p = (void*) malloc (size) ;
if (!p) die ("myalloc failure requesting %d bytes", size) ;
totalAllocated += size ;
return p ;
}
void *_mycalloc (long number, int size)
{
void *p = (void*) calloc (number, size) ;
if (!p) die ("mycalloc failure requesting %d of size %d bytes", number, size) ;
totalAllocated += number*size ;
return p ;
}
/*************************************************/
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 = myalloc (strlen (root) + 32, char) ;
strcpy (fileName, root) ;
strcat (fileName, ".") ;
strcat (fileName, tag) ;
FILE *f = fopen (fileName, mode) ;
free (fileName) ;
return f ;
}
gzFile gzopenTag (char* root, char* tag, char* mode)
{
if (strlen (tag) > 40) die ("tag %s in gzopenTag too long - should be < 30 chars", tag) ;
char *fileName = myalloc (strlen (root) + 42, char) ;
strcpy (fileName, root) ;
strcat (fileName, ".") ;
strcat (fileName, tag) ;
gzFile f = gzopen (fileName, mode) ;
free (fileName) ;
return f ;
}
/*************************************************/
char *fgetword (FILE *f) // pass NULL to free alloced memory
{
int n = 0 ;
static char *buf = 0 ;
int bufSize = 64 ;
char *cp ;
if (!f) { if (buf) free(buf); buf = NULL; return NULL; }
if (!buf) buf = myalloc (bufSize, char) ;
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 ((isspace(*cp) || !isgraph(*cp)) && *cp != '\n' && !feof(f)) *cp = getc (f) ;
/* previous line was
while ((*cp = getc(f)) && (isspace(*cp) || !isgraph(*cp)) && *cp != '\n' && !feof(f)) ;
*/
ungetc (*cp, f) ;
break ;
}
*cp = 0 ;
return buf ;
}
/***************** 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 */
void timeUpdate (FILE *f)
{
static BOOL isFirst = TRUE ;
static struct rusage rOld ;
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
isFirst = FALSE ;
rOld = rNew ;
}
/********************* end of file ***********************/