File: Catrack.c

package info (click to toggle)
dazzdb 1.0%2B20161112-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 500 kB
  • sloc: ansic: 8,187; makefile: 187
file content (265 lines) | stat: -rw-r--r-- 7,157 bytes parent folder | download | duplicates (2)
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
/********************************************************************************************
 *
 *  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;
    FILE *lfile = NULL;

    anno     = NULL;
    trackoff = 0;
    tracktot = tracksiz = 0;
    fwrite(&tracktot,sizeof(int),1,aout);
    fwrite(&tracksiz,sizeof(int),1,aout);

    nfiles = 0;
    while (1)
      { FILE *dfile, *afile;
        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 (nfiles > 0)
          fclose(lfile);
        lfile = afile;

        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);
      }
  
    if (nfiles == 0)
      { fprintf(stderr,"%s: Couldn't find first track block %s1.%s.anno\n",
                       Prog_Name,prefix,argv[2]);
        goto error;
      }
    else
      { char *byte;

        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);
              }
          }
        while (fread(&byte,1,1,lfile) == 1)
          fwrite(&byte,1,1,aout);
        fclose(lfile);

        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);
}