-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwzio.h
137 lines (116 loc) · 3.26 KB
/
wzio.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
#ifndef _WZIO_H
#define _WZIO_H
#include <zlib.h>
#include "wzmisc.h"
#include "wvec.h"
/*******************************
** Open file for reading and **
** error handling **
*******************************/
static inline gzFile wzopen(char *path) {
gzFile fh;
if (strcmp(path, "-") == 0) {
fh = gzdopen(fileno(stdin), "r");
} else {
fh = gzopen(path, "r");
if (!fh) {
fprintf(stderr, "[%s:%d] Fatal, cannot open file: %s\n",
__func__, __LINE__, path);
fflush(stderr);
exit(1);
}
}
return fh;
}
static inline FILE *wzopen_out(char *path) {
FILE *out;
if (path) {
out = fopen(path, "w");
if (!out) {
fprintf(stderr, "[%s:%d] Fatal, cannot open file: %s\n",
__func__, __LINE__, path);
fflush(stderr);
exit(1);
}
} else {
out = stdout;
}
return out;
}
#define wzclose gzclose
/*****************************
** Read one line from file **
*****************************
* Usage:
* char *line;
* gzFile_read_line(fh, &line);
*
* "*s" is either NULL or
* previously allocated c-string
* returns 1 if hitting \n 0 if EOF */
static inline int gzFile_read_line(gzFile fh, char **s) {
if (s == NULL) {
fprintf(stderr, "[%s:%d] Fatal, empty string construct.\n", __func__, __LINE__);
fflush(stderr);
exit(1);
}
/* reset s */
int m = 10, l = 0; /* memory and string length */
*s = realloc(*s, m);
/* read until '\n' or EOF */
while (1) {
int c = gzgetc(fh);
if (l > m-2) { m <<= 1; *s = realloc(*s, m); }
if (c == '\n') {(*s)[l] = '\0'; return 1;}
if (c == EOF) {(*s)[l] = '\0'; return 0;}
(*s)[l++] = c;
}
return 0; /* should not come here */
}
/****************************
** Get one field by index **
****************************
field_index is 0-based
result creates a new allocated object,
return 0 if there are not enough fields, 1 if success */
static inline int line_get_field(const char *line, int field_index, const char *sep, char **field) {
char *working = calloc(strlen(line) + 1, sizeof(char));
strcpy(working, line);
char *tok;
tok = strtok(working, sep);
int i;
for (i=0; i<field_index; ++i)
tok = strtok(NULL, sep);
if (tok == NULL) { /* not enough fields */
free(working);
return 0;
}
*field = strdup(tok);
free(working);
return 1;
}
/********************************
** Get all fields of one line **
********************************
Usage:
char **fields; int nfields;
line_get_fields("my line", " ", &fields, &nfields);
free_fields(fields, nfields);
Note: separators/delimiters are not merged - the most likely use-case. */
#define free_fields(fds, nfds) free_char_array(fds, nfds)
static inline void line_get_fields(const char *line, const char *sep, char ***fields, int *nfields) {
*nfields = 1;
const char *s = line;
while ((s = strpbrk(s, sep)) != NULL) { (*nfields)++; s++; }
*fields = calloc(*nfields, sizeof(char *));
char *working = calloc(strlen(line) + 1, sizeof(char));
strcpy(working, line);
char *tok; int i;
tok = strtok(working, sep);
for (i=0; tok != NULL; ++i) {
(*fields)[i] = strdup(tok);
tok = strtok(NULL, sep);
}
free(working);
}
#endif /* _WZIO_H */