comparison bwa-0.6.2/utils.c @ 2:a294fbfcb1db draft default tip

Uploaded BWA
author ashvark
date Fri, 18 Jul 2014 07:55:59 -0400
parents dd1186b11b3b
children
comparison
equal deleted inserted replaced
1:a9636dc1e99a 2:a294fbfcb1db
1 /* The MIT License
2
3 Copyright (c) 2008 Genome Research Ltd (GRL).
4
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25
26 /* Contact: Heng Li <lh3@sanger.ac.uk> */
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <zlib.h>
33 #include <errno.h>
34 #include <sys/resource.h>
35 #include <sys/time.h>
36 #include "utils.h"
37
38 FILE *err_xopen_core(const char *func, const char *fn, const char *mode)
39 {
40 FILE *fp = 0;
41 if (strcmp(fn, "-") == 0)
42 return (strstr(mode, "r"))? stdin : stdout;
43 if ((fp = fopen(fn, mode)) == 0) {
44 fprintf(stderr, "[%s] fail to open file '%s'. Abort!\n", func, fn);
45 abort();
46 }
47 return fp;
48 }
49 FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp)
50 {
51 if (freopen(fn, mode, fp) == 0) {
52 fprintf(stderr, "[%s] fail to open file '%s': ", func, fn);
53 perror(NULL);
54 fprintf(stderr, "Abort!\n");
55 abort();
56 }
57 return fp;
58 }
59 gzFile err_xzopen_core(const char *func, const char *fn, const char *mode)
60 {
61 gzFile fp;
62 if (strcmp(fn, "-") == 0)
63 return gzdopen(fileno((strstr(mode, "r"))? stdin : stdout), mode);
64 if ((fp = gzopen(fn, mode)) == 0) {
65 fprintf(stderr, "[%s] fail to open file '%s'. Abort!\n", func, fn);
66 abort();
67 }
68 return fp;
69 }
70 void err_fatal(const char *header, const char *fmt, ...)
71 {
72 va_list args;
73 va_start(args, fmt);
74 fprintf(stderr, "[%s] ", header);
75 vfprintf(stderr, fmt, args);
76 fprintf(stderr, " Abort!\n");
77 va_end(args);
78 abort();
79 }
80
81 void err_fatal_simple_core(const char *func, const char *msg)
82 {
83 fprintf(stderr, "[%s] %s Abort!\n", func, msg);
84 abort();
85 }
86
87 size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
88 {
89 size_t ret = fwrite(ptr, size, nmemb, stream);
90 if (ret != nmemb)
91 {
92 err_fatal_simple_core("fwrite", strerror(errno));
93 }
94 return ret;
95 }
96
97 int err_printf(const char *format, ...)
98 {
99 va_list arg;
100 int done;
101
102 va_start(arg, format);
103 done = vfprintf(stdout, format, arg);
104 int saveErrno = errno;
105 va_end(arg);
106
107 if (done < 0)
108 {
109 err_fatal_simple_core("vfprintf(stdout)", strerror(saveErrno));
110 }
111 return done;
112 }
113
114 int err_fprintf(FILE *stream, const char *format, ...)
115 {
116 va_list arg;
117 int done;
118
119 va_start(arg, format);
120 done = vfprintf(stream, format, arg);
121 int saveErrno = errno;
122 va_end(arg);
123
124 if (done < 0)
125 {
126 err_fatal_simple_core("vfprintf", strerror(saveErrno));
127 }
128 return done;
129 }
130
131 int err_fflush(FILE *stream)
132 {
133 int ret = fflush(stream);
134 if (ret != 0)
135 {
136 err_fatal_simple_core("fflush", strerror(errno));
137 }
138 return ret;
139 }
140
141 int err_fclose(FILE *stream)
142 {
143 int ret = fclose(stream);
144 if (ret != 0)
145 {
146 err_fatal_simple_core("fclose", strerror(errno));
147 }
148 return ret;
149 }
150
151 double cputime()
152 {
153 struct rusage r;
154 getrusage(RUSAGE_SELF, &r);
155 return r.ru_utime.tv_sec + r.ru_stime.tv_sec + 1e-6 * (r.ru_utime.tv_usec + r.ru_stime.tv_usec);
156 }
157
158 double realtime()
159 {
160 struct timeval tp;
161 struct timezone tzp;
162 gettimeofday(&tp, &tzp);
163 return tp.tv_sec + tp.tv_usec * 1e-6;
164 }