-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrip.c
executable file
·61 lines (41 loc) · 1.35 KB
/
strip.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
#include "utils.h"
int strip_main(int argc, char *argv[]){
if ( optind == argc || argc != optind + 1) {
fprintf(stderr, "\nUsage: atlas-utils strip <tsv>\n\n");
return 1;
}
kstring_t kt = {0, 0, 0};
int *fields, i, n;
gzFile fp;
fp = strcmp(argv[optind], "-")? gzopen(argv[optind], "r") : gzdopen(fileno(stdin), "r");
if (fp) {
kstream_t *ks;
ks = ks_init(fp);
if(ks_getuntil(ks, '\n', &kt, 0) >= 0){
if(kt.l == 0){
fprintf(stderr, "[ERR]: empty file? \n");
exit(-1);
}
if(kt.s[0] == '#') puts(kt.s);
else{
fprintf(stderr, "[ERR]: headline not start with [#]\n");
exit(-1);
}
}
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
fields = ksplit(&kt, '\t', &n);
for (i = 0; i < fields[1]; ++i)
if(kt.s[i] == ' ') kt.s[i] = '\0';
printf("%s", kt.s);
for (i = 1; i < n; ++i) printf("\t%s", kt.s + fields[i]);
printf("\n");
}
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[optind]);
exit(1);
}
free(kt.s);
return 0;
}