-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstruct.c
183 lines (162 loc) · 4.45 KB
/
struct.c
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
#include "struct.h"
/**************************************************************************/
/* helper functions for error msgs for allocating memory */
void progress(char *format, ...)
/* Print progress message */
{
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
va_end(args);
}
void verboseDot()
/* Print "i-am-alive" dot */
{
putchar('.');
fflush(stdout);
}
void err(char *format, ...)
/* Print error message but do not exit */
{
va_list args;
va_start(args, format);
fprintf(stderr, "[Error] ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
}
void errAbort(char *format, ...)
/* Print error message and exit */
{
va_list args;
va_start(args, format);
fprintf(stderr, "[Error] ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
long clock1000()
/* A millisecond clock. */
{
struct timeval tv;
static long origSec;
gettimeofday(&tv, NULL);
if (origSec == 0) origSec = tv.tv_sec;
return (tv.tv_sec-origSec)*1000 + tv.tv_usec / 1000;
}
void uglyTime(char *label, ...)
/* Print label and how long it's been since last call. Call with
* a NULL label to initialize. */
{
static long lastTime = 0;
long time = clock1000();
va_list args;
va_start(args, label);
if (label != NULL)
{
vfprintf(stdout, label, args);
fprintf(stdout, " [%.3f seconds elapsed]\n", (time - lastTime)/1000.);
}
lastTime = time;
va_end(args);
}
void* xmalloc ( int size )
/* Wrapper for standard mallc */
{
/*malloc: The function malloc() returns a pointer to a chunk of memory of size size, or NULL if there is an error.
*The memory pointed to will be on the heap, not the stack, so make sure to free it when you are done with it.*/
register void* value = malloc(size);
if (value == NULL)
errAbort("Memory exhausted (xmalloc)");
return value;
}
void* xrealloc ( void* ptr, int size )
/* Wrapper for standard reallc */
/* realloc may move the memory block to a new location, in which case the new location is returned.
* The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved.
* If the new size is larger, the value of the newly allocated portion is indeterminate.*/
{
register void* value = realloc(ptr, size);
if (value == NULL)
errAbort("Memory exhausted (xrealloc)");
return value;
}
/**************************************************************************/
struct dyStack *dsNew(int size)
/* Initialize the stack */
{
int stackSize = (size+1) * sizeof(int);
struct dyStack *ds = malloc(stackSize);
dsClear(ds);
return ds;
}
void dsPush(struct dyStack *ds, int element)
/* Add element to the stack */
{
ds->items[++ds->top] = element;
}
void dsPrint(struct dyStack *ds)
/* Print out the stack elements */
{
int i;
printf("Stack contains %d elements\n", dsSize(ds));
for (i=0; i<dsSize(ds); i++)
printf("%d ", ds->items[i]);
putchar('\n');
}
bool isInStack(struct dyStack *ds, int element)
/* Test whether an elemente is in stack */
{
bool flag = FALSE;
int i;
for (i=0; i<dsSize(ds); i++)
{
if (ds->items[i]==element)
{
flag = TRUE; break;
}
}
return flag;
}
int dsIntersect(struct dyStack *ds1, struct dyStack *ds2)
/* Return the number of common components between two arrays */
{
int cnt = 0;
int i;
for (i=0; i<dsSize(ds1); i++)
if (isInStack(ds2, ds1->items[i])) cnt++;
return cnt;
}
/**************************************************************************/
/* file-related operations */
void *addSuffix(char *head, char *suffix)
/* Return a string containing headsuffix */
{
char *ret = NULL;
int size = strlen(head) + strlen(suffix) + 1;
AllocArray(ret, size);
snprintf(ret, size, "%s%s", head, suffix);
return ret;
}
FILE *mustOpen(const char *fileName, char *mode)
/* Open a file or die */
{
FILE *f;
if (sameString(fileName, "stdin")) return stdin;
if (sameString(fileName, "stdout")) return stdout;
if ((f = fopen(fileName, mode)) == NULL)
{
char *modeName = "";
if (mode)
{
if (mode[0] == 'r') modeName = " to read";
else if (mode[0] == 'w') modeName = " to write";
else if (mode[0] == 'a') modeName = " to append";
}
errAbort("Can't open %s%s: %s", fileName, modeName, strerror(errno));
}
return f;
}
/**************************************************************************/