-
Notifications
You must be signed in to change notification settings - Fork 0
/
Catrack.c
296 lines (262 loc) · 9.95 KB
/
Catrack.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/************************************************************************************\
* *
* Copyright (c) 2014, Dr. Eugene W. Myers (EWM). All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, *
* are permitted provided that the following conditions are met: *
* *
* · Redistributions of source code must retain the above copyright notice, this *
* list of conditions and the following disclaimer. *
* *
* · Redistributions in binary form must reproduce the above copyright notice, this *
* list of conditions and the following disclaimer in the documentation and/or *
* other materials provided with the distribution. *
* *
* · The name of EWM may not be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY EWM ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EWM BE LIABLE *
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN *
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* For any issues regarding this software and its use, contact EWM at: *
* *
* Eugene W. Myers Jr. *
* Bautzner Str. 122e *
* 01099 Dresden *
* GERMANY *
* Email: [email protected] *
* *
\************************************************************************************/
/********************************************************************************************
*
* Concate in block order all "block tracks" <DB>.<track>.# into a single track
* <DB>.<track>
*
* Author: Gene Myers
* Date : June 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "DB.h"
#ifdef HIDE_FILES
#define PATHSEP "/."
#else
#define PATHSEP "/"
#endif
static char *Usage = "[-v] <path:db|dam> <track:name>";
int main(int argc, char *argv[])
{ char *prefix;
FILE *aout, *dout;
int VERBOSE;
// Process arguments
{ int i, j, k;
int flags[128];
ARG_INIT("Catrack")
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
{ ARG_FLAGS("v") }
else
argv[j++] = argv[i];
argc = j;
VERBOSE = flags['v'];
if (argc != 3)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
exit (1);
}
}
{ char *pwd, *root;
int plen;
plen = strlen(argv[1]);
if (strcmp(argv[1]+(plen-3),".dam") == 0)
root = Root(argv[1],".dam");
else
root = Root(argv[1],".db");
pwd = PathTo(argv[1]);
prefix = Strdup(Catenate(pwd,PATHSEP,root,"."),"Allocating track name");
free(pwd);
free(root);
aout = fopen(Catenate(prefix,argv[2],".","anno"),"r");
if (aout != NULL)
{ fprintf(stderr,"%s: Track file %s%s.anno already exists!\n",Prog_Name,prefix,argv[2]);
fclose(aout);
exit (1);
}
dout = fopen(Catenate(prefix,argv[2],".","data"),"r");
if (dout != NULL)
{ fprintf(stderr,"%s: Track file %s%s.data already exists!\n",Prog_Name,prefix,argv[2]);
fclose(dout);
exit (1);
}
aout = Fopen(Catenate(prefix,argv[2],".","anno"),"w");
if (aout == NULL)
exit (1);
dout = NULL;
}
{ int tracktot, tracksiz;
int64 trackoff;
int nfiles;
char data[1024];
void *anno;
anno = NULL;
trackoff = 0;
tracktot = tracksiz = 0;
fwrite(&tracktot,sizeof(int),1,aout);
fwrite(&tracksiz,sizeof(int),1,aout);
nfiles = 0;
while (1)
{ FILE *afile, *dfile;
int i, size, tracklen;
afile = fopen(Numbered_Suffix(prefix,nfiles+1,Catenate(".",argv[2],".","anno")),"r");
if (afile == NULL)
break;
dfile = fopen(Numbered_Suffix(prefix,nfiles+1,Catenate(".",argv[2],".","data")),"r");
if (VERBOSE)
{ fprintf(stderr,"Concatenating %s%d.%s ...\n",prefix,nfiles+1,argv[2]);
fflush(stderr);
}
if (fread(&tracklen,sizeof(int),1,afile) != 1)
SYSTEM_ERROR
if (fread(&size,sizeof(int),1,afile) != 1)
SYSTEM_ERROR
if (nfiles == 0)
{ tracksiz = size;
if (dfile != NULL)
{ dout = Fopen(Catenate(prefix,argv[2],".","data"),"w");
if (dout == NULL)
{ fclose(afile);
fclose(dfile);
goto error;
}
}
else
{ anno = Malloc(size,"Allocating annotation record");
if (anno == NULL)
{ fclose(afile);
goto error;
}
}
}
else
{ int escape = 1;
if (tracksiz != size)
{ fprintf(stderr,"%s: Track block %d does not have the same annotation size (%d)",
Prog_Name,nfiles+1,size);
fprintf(stderr," as previous blocks (%d)\n",tracksiz);
}
else if (dfile == NULL && dout != NULL)
fprintf(stderr,"%s: Track block %d does not have data but previous blocks do\n",
Prog_Name,nfiles+1);
else if (dfile != NULL && dout == NULL)
fprintf(stderr,"%s: Track block %d has data but previous blocks do not\n",
Prog_Name,nfiles+1);
else
escape = 0;
if (escape)
{ fclose(afile);
if (dfile != NULL) fclose(dfile);
if (anno != NULL) free(anno);
goto error;
}
}
if (dfile != NULL)
{ int64 dlen;
if (size == 4)
{ int anno4;
for (i = 0; i < tracklen; i++)
{ if (fread(&anno4,sizeof(int),1,afile) != 1)
SYSTEM_ERROR
anno4 += trackoff;
fwrite(&anno4,sizeof(int),1,aout);
}
if (fread(&anno4,sizeof(int),1,afile) != 1)
SYSTEM_ERROR
dlen = anno4;
}
else
{ int64 anno8;
for (i = 0; i < tracklen; i++)
{ if (fread(&anno8,sizeof(int64),1,afile) != 1)
SYSTEM_ERROR
anno8 += trackoff;
fwrite(&anno8,sizeof(int64),1,aout);
}
if (fread(&anno8,sizeof(int64),1,afile) != 1)
SYSTEM_ERROR
dlen = anno8;
}
trackoff += dlen;
for (i = 1024; i < dlen; i += 1024)
{ if (fread(data,1024,1,dfile) != 1)
SYSTEM_ERROR
fwrite(data,1024,1,dout);
}
i -= 1024;
if (i < dlen)
{ if (fread(data,dlen-i,1,dfile) != 1)
SYSTEM_ERROR
fwrite(data,dlen-i,1,dout);
}
}
else
{ for (i = 0; i < tracklen; i++)
{ if (fread(anno,size,1,afile) != 1)
SYSTEM_ERROR
fwrite(anno,size,1,aout);
}
}
tracktot += tracklen;
nfiles += 1;
if (dfile != NULL) fclose(dfile);
fclose(afile);
}
if (nfiles == 0)
{ fprintf(stderr,"%s: Couldn't find first track block %s1.%s.anno\n",
Prog_Name,prefix,argv[2]);
goto error;
}
else
{ if (dout != NULL)
{ if (tracksiz == 4)
{ int anno4 = trackoff;
fwrite(&anno4,sizeof(int),1,aout);
}
else
{ int64 anno8 = trackoff;
fwrite(&anno8,sizeof(int64),1,aout);
}
}
else
{ fwrite(anno,tracksiz,1,aout);
free(anno);
}
rewind(aout);
fwrite(&tracktot,sizeof(int),1,aout);
fwrite(&tracksiz,sizeof(int),1,aout);
}
}
fclose(aout);
if (dout != NULL)
fclose(dout);
free(prefix);
exit (0);
error:
fclose(aout);
unlink(Catenate(prefix,argv[2],".","anno"));
if (dout != NULL)
{ fclose(dout);
unlink(Catenate(prefix,argv[2],".","data"));
}
free(prefix);
exit (1);
}