-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
77 lines (56 loc) · 1.14 KB
/
util.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
#include <stdlib.h>
#include <stdint.h>
#include <zlib.h>
#include <limits.h>
#include "util.h"
#include "htslib/htslib/kseq.h"
KSTREAM_INIT(gzFile, gzread, 16384)
int nrows(char *fn)
{
gzFile file;
kstream_t *ks;
kstring_t str = {0,0,NULL};
file = gzopen(fn, "r");
if (!file) return -1;
ks = ks_init(file);
int i = 0;
while (ks_getuntil(ks, '\n', &str, 0) >= 0) {
++i;
}
gzclose(file);
ks_destroy(ks);
free(str.s);
return i;
}
int ncols(char *fn, int offset)
{
gzFile file;
kstream_t *ks;
kstring_t str = {0,0,NULL};
file = gzopen(fn, "r");
if (!file) return -1;
ks = ks_init(file);
ks_getuntil(ks, '\n', &str, 0);
char *token;
token = strtok(str.s, " \t");
int i = 1;
while((token = strtok(NULL, " \t"))) {
++i;
}
gzclose(file);
ks_destroy(ks);
free(str.s);
return i - offset;
}
void close_outf(outf_t *outf)
{
if (outf->pfout) fclose(outf->pfout);
if (outf->adfout) fclose(outf->adfout);
if (outf->safout) fclose(outf->safout);
}
int numPlaces(int n)
{
if (n < 0) return numPlaces ((n == INT_MIN) ? INT_MAX: -n);
if (n < 10) return 1;
return 1 + numPlaces (n / 10);
}