-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwztsv.h
147 lines (127 loc) · 4.1 KB
/
wztsv.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
/**
* The MIT License (MIT)
* Copyright (c) 2016-2017 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* usage:
* tsv_t *tsv = tsv_open("input.tsv");
* while (tsv_read(tsv)) {
* for (i=0; i < tsv->n; ++i) printf("\t|%s", tsv->fields[i]);
* printf("\n");
* }
* tsv_close(tsv);
**/
#include <string.h>
#include <stdlib.h>
#include <zlib.h>
#include "wzio.h"
typedef struct tsv_t {
char **fields;
size_t n, m; /* number of fields, and allocated */
gzFile fp;
int finished;
int line_max;
char *line;
} tsv_t;
static inline tsv_t *tsv_open(char *fn) {
tsv_t *tsv = calloc(1, sizeof(tsv_t));
tsv->fp = wzopen(fn);
tsv->fields = NULL;
tsv->line_max = 10000;
tsv->line = calloc(tsv->line_max, sizeof(char));
tsv->finished = 0;
return tsv;
}
/* tsv_close release the memory */
static inline void tsv_close(tsv_t *tsv) {
unsigned i;
for (i = 0; i < tsv->m; ++i)
free(tsv->fields[i]);
free(tsv->fields);
free(tsv->line);
gzclose(tsv->fp);
free(tsv);
}
static inline void __tsv_clear_fields(tsv_t *tsv) {
tsv->n = 0;
}
/* i is 0-based */
static inline void __tsv_set_field(tsv_t *tsv, int i, const char *s) {
tsv->fields[i] = realloc(tsv->fields[i], strlen(s)+1);
strcpy(tsv->fields[i], s);
}
static inline void __tsv_append_field(tsv_t *tsv, const char *s) {
if (tsv->n == tsv->m) {
tsv->fields = realloc(tsv->fields, (++tsv->m)*sizeof(char*));
/* the newly incremented memory is always initialized */
tsv->fields[tsv->m-1] = NULL;
}
__tsv_set_field(tsv, tsv->n++, s);
}
static inline char *tsv_field(tsv_t *tsv, unsigned k) {
if (k < tsv->n)
return tsv->fields[k];
else
return NULL;
}
static inline int tsv_is_blankline(tsv_t *tsv) {
if (tsv->line[0] == '\0') return 1;
else return 0;
}
#define tsv_num_fields(tsv) (tsv)->n
/* return NULL when done */
static inline int tsv_read(tsv_t *tsv) {
if (tsv->finished)
return 0;
char *tok;
char *line_null = tsv->line; // point to the terminating NULL
*line_null = '\0';
char *tmp = NULL;
while (1) {
int c = gzgetc(tsv->fp);
if (c == '\n' || c == EOF) { /* process current line */
__tsv_clear_fields(tsv);
if (line_null == tsv->line) { // empty line
if (c == EOF) tsv->finished = 1;
break;
}
// keep original line
tmp = realloc(tmp, strlen(tsv->line) + 1);
strcpy(tmp, tsv->line);
tok = strtok(tmp, "\t");
while (tok != NULL) {
__tsv_append_field(tsv, tok);
tok = strtok(NULL, "\t");
}
if (c == EOF)
tsv->finished = 1;
break;
} else { /* read line */
if (line_null >= tsv->line + tsv->line_max) { // reaches max
tsv->line_max <<= 1;
int line_len = line_null - tsv->line;
tsv->line = realloc(tsv->line, tsv->line_max);
line_null = tsv->line + line_len;
}
*line_null++ = c;
*line_null = '\0';
}
}
free(tmp);
return 1;
}