forked from ANGSD/angsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aio.cpp
117 lines (102 loc) · 2.69 KB
/
aio.cpp
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
#include <cstring>
#include <sys/stat.h>
#include "aio.h"
int aio::fexists(const char* str){///@param str Filename given as a string.
struct stat buffer ;
return (stat(str, &buffer )==0 ); /// @return Function returns 1 if file exists.
}
size_t aio::fsize(const char* fname){
struct stat st ;
stat(fname,&st);
return st.st_size;
}
std::vector <char *> dumpedFiles;//small hack for getting a nice vector of outputfiles
FILE *aio::openFile(const char* a,const char* b){
if(0)
fprintf(stderr,"[%s] %s %s",__FUNCTION__,a,b);
char *c = new char[strlen(a)+strlen(b)+1];
strcpy(c,a);
strcat(c,b);
// fprintf(stderr,"\t-> Dumping file: %s\n",c);
dumpedFiles.push_back(strdup(c));
FILE *fp = NULL;
fp = fopen(c,"w");
if(fp==NULL){
fprintf(stderr,"\t-> Problem opening file: \'%s\' check permissions\n",c);
exit(0);
}
delete [] c;
return fp;
}
BGZF *aio::openFileBG(const char* a,const char* b){
char *c = new char[strlen(a)+strlen(b)+1];
strcpy(c,a);
strcat(c,b);
dumpedFiles.push_back(strdup(c));
BGZF *fp = bgzf_open(c,"w6h");
delete [] c;
return fp;
}
htsFile *aio::openFileHts(const char* a,const char* b){
char *c = new char[strlen(a)+strlen(b)+1];
strcpy(c,a);
strcat(c,b);
dumpedFiles.push_back(strdup(c));
htsFile *fp = hts_open(c,"w");
delete [] c;
return fp;
}
htsFile *aio::openFileHtsBcf(const char* a,const char* b){
char *c = new char[strlen(a)+strlen(b)+1];
strcpy(c,a);
strcat(c,b);
dumpedFiles.push_back(strdup(c));
htsFile *fp = hts_open(c,"wb");
delete [] c;
return fp;
}
FILE *aio::getFILE(const char*fname,const char* mode){
int writeFile = 0;
for(size_t i=0;i<strlen(mode);i++)
if(mode[i]=='w')
writeFile = 1;
FILE *fp;
if(NULL==(fp=fopen(fname,mode))){
fprintf(stderr,"\t-> Error opening FILE handle for file:%s exiting\n",fname);
exit(0);
}
return fp;
}
//checks that newer is newer than older
int aio::isNewer(const char *newer,const char *older){
if (strstr(older, "ftp://") == older || strstr(older, "http://") == older)
return 0;
// fprintf(stderr,"newer:%s older:%s\n",newer,older);
// return 0;
struct stat one;
struct stat two;
stat(newer, &one );
stat(older, &two );
return one.st_mtime>=two.st_mtime;
}
ssize_t aio::bgzf_write(BGZF *fp, const void *data, size_t length){
if(length>0)
return ::bgzf_write(fp,data,length);
return 0;
}
int aio::tgets(gzFile gz,char**buf,int *l){
int rlen = 0;
neverUseGoto:
char *tok = gzgets(gz,*buf+rlen,*l-rlen);
if(!tok)
return rlen;
int tmp = tok?strlen(tok):0;
if(tok[tmp-1]!='\n'){
rlen += tmp;
*l *= 2;
*buf = (char*) realloc(*buf,*l);
goto neverUseGoto;
}
rlen += tmp;
return rlen;
}