]> git.donarmstrong.com Git - fastq-tools.git/blobdiff - src/common.c
a program to output a nonredundant list of readss
[fastq-tools.git] / src / common.c
diff --git a/src/common.c b/src/common.c
new file mode 100644 (file)
index 0000000..4598819
--- /dev/null
@@ -0,0 +1,56 @@
+
+/*
+ * This file is part of fastq-tools.
+ *
+ * Copyright (c) 2011 by Daniel C. Jones <dcjones@cs.washington.edu>
+ *
+ */
+
+
+#include "common.h"
+#include <stdlib.h>
+
+
+void or_die(int b, const char* msg)
+{
+    if (b == 0) {
+        fputs(msg, stderr);
+        exit(1);
+    }
+}
+
+
+void* malloc_or_die(size_t n)
+{
+    void* p = malloc(n);
+    if (p == NULL) {
+        fprintf(stderr, "Can not allocate %zu bytes.\n", n);
+        exit(1);
+    }
+    return p;
+}
+
+
+void* realloc_or_die(void* ptr, size_t n)
+{
+    void* p = realloc(ptr, n);
+    if (p == NULL) {
+        fprintf(stderr, "Can not allocate %zu bytes.\n", n);
+        exit(1);
+    }
+    return p;
+}
+
+
+FILE* fopen_or_die(const char* path, const char* mode)
+{
+    FILE* f = fopen(path, mode);
+    if (f == NULL) {
+        fprintf(stderr, "Can not open file %s with mode %s.\n", path, mode);
+        exit(1);
+    }
+    return f;
+}
+
+
+