File: utility.c

package info (click to toggle)
velvet 1.2.10%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 56,204 kB
  • sloc: ansic: 22,940; perl: 1,194; python: 295; sh: 125; makefile: 121
file content (199 lines) | stat: -rw-r--r-- 4,707 bytes parent folder | download | duplicates (6)
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
/*
Copyright 2009 John Marshall (jm18@sanger.ac.uk) 

    This file is part of Velvet.

    Velvet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Velvet 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Velvet; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>

#include "globals.h"
#include "utility.h"

static void allocExitError(const char *function, unsigned long long count,
                          unsigned long long size, const char *name)
{
       if (size == 1)
               exitErrorf(EXIT_FAILURE, true,
                          "Can't %s %llu %ss",
                          function, count, name);
       else
               exitErrorf(EXIT_FAILURE, true,
                          "Can't %s %llu %ss totalling %llu bytes",
                          function, count, name, count * size);
}

void *mallocOrExit3(size_t count, size_t size, const char *name)
{
       void *p = malloc(count * size);
       if (p == NULL && count > 0)
               allocExitError("malloc", count, size, name);

       return p;
}

void *callocOrExit3(size_t count, size_t size, const char *name)
{
       void *p = calloc(count, size);
       if (p == NULL && count > 0)
               allocExitError("calloc", count, size, name);

       return p;
}

void *reallocOrExit4(void *ptr, size_t count, size_t size, const char *name)
{
       void *p = realloc(ptr, count * size);
       if (p == NULL && count > 0)
               allocExitError("realloc", count, size, name);

       return p;
}


static const char *programName = NULL;

void setProgramName(const char *name)
{
       programName = name;
}

void exitErrorf(int exitStatus, boolean showErrno, const char *format, ...)
{
       int savedErrno = errno;
       va_list args;
       va_start(args, format);
       if (programName)
               fprintf(stderr, "%s: ", programName);
       vfprintf(stderr, format, args);
       if (showErrno)
               fprintf(stderr, ": %s", strerror(savedErrno));
       fprintf(stderr, "\n");
       va_end(args);

#ifdef DEBUG 
	abort();
#endif 
       exit(exitStatus);
}

void velvetLog(const char *format, ...)
{
  static boolean timeIsSet = false;
  static struct timeval tvStart;
  struct timeval tvNow;
  struct timeval tvDiff;
  va_list args;

  if (!timeIsSet)
  {
    gettimeofday(&tvStart, NULL);
    timeIsSet = true;
  }

  gettimeofday(&tvNow, NULL);
  timersub(&tvNow, &tvStart, &tvDiff);

  printf("[%ld.%06ld] ", (long) tvDiff.tv_sec, (long) tvDiff.tv_usec);
  va_start(args, format);
  vprintf(format, args);
  va_end(args);

#ifdef DEBUG 
  fflush(stdout);
#endif
}

void velvetFprintf(FILE * file, const char * format, ...) 
{
	va_list args;

	va_start(args, format);
	if (vfprintf(file, format, args) < 0) {
		if (programName)
		       fprintf(stderr, "%s: ", programName);
		fprintf(stderr, "Could not write into file\n");
		va_end(args);
#ifdef DEBUG 
		abort();
#endif 
		exit(EXIT_FAILURE);
	}
	va_end(args);
}

StringBuffer *newStringBuffer(size_t size)
{
	StringBuffer *buffer;

	buffer = callocOrExit(1, StringBuffer);
	if (size > 0)
	{
		buffer->str = callocOrExit(size, char);
		buffer->allocated = size;
	}
	buffer->length = 1; /* virtual count for the final '\0' */

	return buffer;
}

void destroyStringBuffer(StringBuffer *buffer, boolean freeString)
{
	if (!buffer)
		return;

	if (freeString && buffer->allocated > 0)
		free(buffer->str);
	free(buffer);
}

void appendStringBuffer(StringBuffer *buffer, char *str)
{
	int strSize;
	int newSize;

	if (!buffer)
		return;

	strSize = strlen(str);
	newSize = buffer->allocated;

	while (strSize + buffer->length > newSize)
		newSize *= 2;
	if (newSize != buffer->allocated)
	{
		buffer->str = reallocOrExit(buffer->str, newSize, char);
		if (!buffer->allocated)
			*buffer->str = '\0';
		buffer->allocated = newSize;
	}
	buffer->length += strSize;
	buffer->str = strcat(buffer->str, str);
}

void resetStringBuffer(StringBuffer *buffer)
{
	if (buffer && buffer->allocated > 0)
	{
		buffer->length = 1; /* Virtual count for the final '\0' */
		*buffer->str = '\0';
	}
}