1 /*
2 Copyright 2009 John Marshall (jm18@sanger.ac.uk)
3 
4     This file is part of Velvet.
5 
6     Velvet is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     Velvet is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with Velvet; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20 */
21 #ifndef UTILITY_H_
22 #define UTILITY_H_
23 
24 #ifdef __GNUC__
25 #define ATTRIBUTE(list)  __attribute__ (list)
26 #else
27 #define ATTRIBUTE(list)
28 #endif
29 
30 #include <stdio.h>
31 
32 // Wrappers for malloc(), calloc(), and realloc() that always succeed.
33 // These functions print an error message and exit on failure, rather than
34 // requiring the calling function to check for NULL.  The arguments contain
35 // the type itself -- mallocOrExit(n, Foo) rather than malloc(n * sizeof Foo)
36 // -- to enable type checking and so that it can be shown in error messages.
37 #define mallocOrExit(count, type) \
38                ((type *) mallocOrExit3((count), sizeof(type), #type))
39 #define callocOrExit(count, type) \
40                ((type *) callocOrExit3((count), sizeof(type), #type))
41 #define reallocOrExit(ptr, count, type) \
42                ((type *) reallocOrExit4((ptr), (count), sizeof(type), #type))
43 
44 // However there are types for which just appending a '*' produces the
45 // wrong type or a syntax error, rather than a pointer-to-<type>.  These
46 // less type-safe wrappers are provided for use in these unusual cases.
47 #define mallocOrExitWithoutCast(count, type) \
48                (mallocOrExit3((count), sizeof(type), #type))
49 #define callocOrExitWithoutCast(count, type) \
50                (callocOrExit3((count), sizeof(type), #type))
51 #define reallocOrExitWithoutCast(ptr, count, type) \
52                (reallocOrExit4((ptr), (count), sizeof(type), #type))
53 
54 // (Implementation functions -- use the macro wrappers above.)
55 void *mallocOrExit3(size_t count, size_t size, const char *name);
56 void *callocOrExit3(size_t count, size_t size, const char *name);
57 void *reallocOrExit4(void *ptr, size_t count, size_t size, const char *name);
58 
59 // Sets the program name to be prepended to error messages.
60 void setProgramName(const char *name);
61 
62 // Prints an error message to standard error (with printf-style formatting
63 // and optionally appending a perror-style description of errno), and calls
64 // exit() with the specified exit status.
65 void exitErrorf(int exitStatus, boolean showErrno, const char *format, ...)
66        ATTRIBUTE((format(printf, 3, 4), noreturn));
67 
68 // Velvet-specific logging utility
69 void velvetLog(const char *format, ...)
70 	ATTRIBUTE((format(printf, 1, 2)));
71 
72 // fprintf wrapper which exits upon failure (e.g. disk full)
73 void velvetFprintf(FILE * file, const char * format, ...)
74 	ATTRIBUTE((format(printf, 2, 3)));
75 
76 // String Buffer
77 
78 typedef struct
79 {
80 	char *str;
81 	size_t length;
82 	size_t allocated;
83 }
84 StringBuffer;
85 
86 StringBuffer *newStringBuffer(size_t size);
87 void destroyStringBuffer(StringBuffer *buffer, boolean freeString);
88 void appendStringBuffer(StringBuffer *buffer, char *str);
89 void resetStringBuffer(StringBuffer *buffer);
90 
91 // Solaris 10 and earlier versions lack titmersub()
92 #if defined (__SVR4) && defined (__sun) && !defined(timersub)
93 #define timersub(a, b, res) \
94        *res.tv_sec = *a.tv_sec - *b.tv_sec; \
95        *res.tv_usec = 0;
96 #endif
97 
98 #endif
99