]> git.donarmstrong.com Git - fastq-tools.git/blob - src/common.c
b3f47edc166e5a2027cd6f3cb34cc2e04e2830d9
[fastq-tools.git] / src / common.c
1
2 /*
3  * This file is part of fastq-tools.
4  *
5  * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
6  *
7  */
8
9
10 #include "common.h"
11 #include "version.h"
12 #include <stdlib.h>
13
14
15 void print_version(FILE *f, const char* prog_name)
16 {
17     fprintf(f, "%s (fastq-tools) %s\n",
18             prog_name, FASTQ_TOOLS_VERSION);
19 }
20
21
22 void or_die(int b, const char* msg)
23 {
24     if (b == 0) {
25         fputs(msg, stderr);
26         exit(1);
27     }
28 }
29
30
31 void* malloc_or_die(size_t n)
32 {
33     void* p = malloc(n);
34     if (p == NULL) {
35         fprintf(stderr, "Can not allocate %zu bytes.\n", n);
36         exit(1);
37     }
38     return p;
39 }
40
41
42 void* realloc_or_die(void* ptr, size_t n)
43 {
44     void* p = realloc(ptr, n);
45     if (p == NULL) {
46         fprintf(stderr, "Can not allocate %zu bytes.\n", n);
47         exit(1);
48     }
49     return p;
50 }
51
52
53 FILE* fopen_or_die(const char* path, const char* mode)
54 {
55     FILE* f = fopen(path, mode);
56     if (f == NULL) {
57         fprintf(stderr, "Can not open file %s with mode %s.\n", path, mode);
58         exit(1);
59     }
60     return f;
61 }
62
63
64