-
Notifications
You must be signed in to change notification settings - Fork 70
/
paf.c
67 lines (62 loc) · 1.62 KB
/
paf.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
#include <zlib.h>
#include <stdio.h>
#include <string.h>
#include "paf.h"
#include "kseq.h"
KSTREAM_INIT(gzFile, gzread, 0x10000)
paf_file_t *paf_open(const char *fn)
{
kstream_t *ks;
gzFile fp;
paf_file_t *pf;
fp = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
if (fp == 0) return 0;
ks = ks_init(fp);
pf = (paf_file_t*)calloc(1, sizeof(paf_file_t));
pf->fp = ks;
return pf;
}
int paf_close(paf_file_t *pf)
{
kstream_t *ks;
if (pf == 0) return 0;
free(pf->buf.s);
ks = (kstream_t*)pf->fp;
gzclose(ks->f);
ks_destroy(ks);
free(pf);
return 0;
}
int paf_parse(int l, char *s, paf_rec_t *pr) // s must be NULL terminated
{ // on return: <0 for failure; 0 for success; >0 for filtered
char *q, *r;
int i, t;
for (i = t = 0, q = s; i <= l; ++i) {
if (i < l && s[i] != '\t') continue;
s[i] = 0;
if (t == 0) pr->qn = q;
else if (t == 1) pr->ql = strtol(q, &r, 10);
else if (t == 2) pr->qs = strtol(q, &r, 10);
else if (t == 3) pr->qe = strtol(q, &r, 10);
else if (t == 4) pr->rev = (*q == '-');
else if (t == 5) pr->tn = q;
else if (t == 6) pr->tl = strtol(q, &r, 10);
else if (t == 7) pr->ts = strtol(q, &r, 10);
else if (t == 8) pr->te = strtol(q, &r, 10);
else if (t == 9) pr->ml = strtol(q, &r, 10);
else if (t == 10) pr->bl = strtol(q, &r, 10);
++t, q = i < l? &s[i+1] : 0;
}
if (t < 10) return -1;
return 0;
}
int paf_read(paf_file_t *pf, paf_rec_t *r)
{
int ret, dret;
file_read_more:
ret = ks_getuntil((kstream_t*)pf->fp, KS_SEP_LINE, &pf->buf, &dret);
if (ret < 0) return ret;
ret = paf_parse(pf->buf.l, pf->buf.s, r);
if (ret < 0) goto file_read_more;
return ret;
}