-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIO_stream.h
164 lines (151 loc) · 4.77 KB
/
IO_stream.h
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
#ifndef _IO_H
#define _IO_H
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <zlib.h>
//#include <err.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
/*
typedef pFILE FILE *;
#define OPEN_STREAM(TYPE,fileno,mode) \
TYPE open_stream_##TYPE(char* filename,void(*f)(int , const char *)) { \
int fd ; \
if (strncmp(filename,"-", 1)==0) { \
fd = fileno; \
} else { \
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666 ); \
if (fd==-1) err(1, "Failed to create output file (%s)", filename); \
} \
TYPE out = f(fd,mode); \
return out; \
}
#define CREATE_OUTFILE(TYPE) \
TYPE creat_outfile_##TYPE(char *outfile,char *suffix) {
char *out=(char *)calloc(strlen(outfile)+strlen(suffix)+2,sizeof(char));
sprintf(out,"%s%s",outfile,suffix);
if (!strncmp(outfile,"-",1)){
TYPE fo=open_stream_##TYPE(out,fdopen);
}
else{
TYPE fo=open_stream_##TYPE(out,gzdopen);
}
return fo;
}
OPEN_STREAM(pFILE,STDOUT_FILENO,"wb")
OPEN_STREAM(gzFile,STDOUT_FILENO,"wb")
CREATE_OUTFILE(gzFile)
CREATE_OUTFILE(pFILE)
*/
static inline FILE *fopen_input_stream(const char *filename);
static inline FILE *fopen_output_stream(const char *filename);
static inline FILE *fcreat_infile(const char *infile,const char *suffix);
static inline FILE *fcreat_outfile(const char *outfile,const char *suffix);
static inline gzFile open_output_stream(const char* filename);
static inline gzFile creat_outfile(char *outfile,char *suffix);
static inline gzFile open_input_stream(const char *filename);
static inline void error(const char *format, ...);
static inline long long usec(void);
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
long long usec(void){
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}
#else
long long usec(void){
struct timeval tv;
gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000)+((long long)tv.tv_usec)/1000;
}
#endif
void error(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
FILE *fopen_input_stream(const char *filename){
int fd ;
if (strncmp(filename,"-", 1)==0 || !strcmp(filename,"")) {
fd = STDIN_FILENO;
} else {
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
fd = open(filename, O_CREAT | O_RDONLY , 0666 );
#else
fd = _open(filename, _O_CREAT|_O_BINARY | _O_RDONLY , 0666 );
#endif
if (fd==-1) fprintf(stderr, "Failed to create input file (%s)", filename);
}
FILE *in = fdopen(fd,"rb");
return in;
}
FILE *fopen_output_stream(const char *filename) {
int fd ;
if (strncmp(filename,"-", 1)==0 || !strcmp(filename,"")) {
fd = STDOUT_FILENO;
} else {
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666 );
#else
fd = _open(filename, _O_CREAT|_O_BINARY | _O_WRONLY | _O_APPEND |_O_TRUNC, 0666 );
#endif
if (fd==-1) fprintf(stderr, "Failed to create output file (%s)", filename);
}
FILE *out = fdopen(fd,"wb");
return out;
}
FILE *fcreat_infile(const char *infile,const char *suffix) {
char in[strlen(infile)+strlen(suffix)+2];
sprintf(in,"%s%s",infile,suffix);
FILE *fi=fopen_output_stream(in);
return fi;
}
FILE *fcreat_outfile(const char *outfile,const char *suffix) {
char *out=(char *)calloc(strlen(outfile)+strlen(suffix)+2,sizeof(char));
sprintf(out,"%s%s",outfile,suffix);
FILE *fo=fopen_output_stream(out);
return fo;
}
gzFile open_output_stream(const char* filename) {
int fd ;
if (strncmp(filename,"-", 1)==0 || !strcmp(filename,"")) {
fd = STDOUT_FILENO;
} else {
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666 );
#else
fd = _open(filename, _O_CREAT|_O_BINARY | _O_WRONLY | _O_APPEND |_O_TRUNC, 0666 );
#endif
if (fd==-1) fprintf(stderr, "Failed to create output file (%s)", filename);
}
gzFile out = gzdopen(fd,"wb");
return out;
}
gzFile creat_outfile(char *outfile,char *suffix) {
char *out=(char *)calloc(strlen(outfile)+strlen(suffix)+2,sizeof(char));
sprintf(out,"%s%s",outfile,suffix);
gzFile fo=open_output_stream(out);
return fo;
}
gzFile open_input_stream(const char *filename){
int fd ;
if (strncmp(filename,"-", 1)==0 || !strcmp(filename,"")) {
fd = STDIN_FILENO;
} else {
#if defined(__APPLE__) && defined(__MACH__) || defined(unix) || defined(linux)
fd = open(filename, O_CREAT | O_RDONLY, 0666 );
#else
fd = _open(filename, _O_CREAT|_O_BINARY | _O_RDONLY , 0666 );
#endif
if (fd==-1) fprintf(stderr, "Failed to create input file (%s)", filename);
}
gzFile in = gzdopen(fd,"rb");
return in;
}
#endif