Go to the documentation of this file.
35 #define KS_SEP_SPACE 0 // isspace(): \t, \n, \v, \f, \r
36 #define KS_SEP_TAB 1 // isspace() && !' '
37 #define KS_SEP_LINE 2 // line separator: "\n" (Unix) or "\r\n" (Windows)
40 #define __KS_TYPE(type_t) \
41 typedef struct __kstream_t { \
43 int is_eof:2, bufsize:30; \
49 #define ks_eof(ks) ((ks)->is_eof && (ks)->begin >= (ks)->end)
50 #define ks_rewind(ks) ((ks)->is_eof = (ks)->begin = (ks)->end = 0)
52 #define __KS_BASIC(SCOPE, type_t, __bufsize) \
53 SCOPE kstream_t *ks_init(type_t f) \
55 kstream_t *ks = (kstream_t*)calloc(1, sizeof(kstream_t)); \
56 ks->f = f; ks->bufsize = __bufsize; \
57 ks->buf = (unsigned char*)malloc(__bufsize); \
60 SCOPE void ks_destroy(kstream_t *ks) \
67 #define __KS_INLINED(__read) \
68 static inline int ks_getc(kstream_t *ks) \
70 if (ks->is_eof && ks->begin >= ks->end) return -1; \
71 if (ks->begin >= ks->end) { \
73 ks->end = __read(ks->f, ks->buf, ks->bufsize); \
74 if (ks->end < ks->bufsize) ks->is_eof = 1; \
75 if (ks->end == 0) return -1; \
78 return (int)ks->buf[ks->begin++]; \
80 static inline int ks_getuntil(kstream_t *ks, int delimiter, kstring_t *str, int *dret) \
81 { return ks_getuntil2(ks, delimiter, str, dret, 0); }
84 #define KSTRING_T kstring_t
92 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
95 #define __KS_GETUNTIL(SCOPE, __read) \
96 SCOPE int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append) \
98 if (dret) *dret = 0; \
99 str->l = append? str->l : 0; \
100 uint64_t seek_pos = str->l; \
101 if (ks->begin >= ks->end && ks->is_eof) return -1; \
104 if (ks->begin >= ks->end) { \
107 ks->end = __read(ks->f, ks->buf, ks->bufsize); \
108 if (ks->end < ks->bufsize) ks->is_eof = 1; \
109 if (ks->end == 0) break; \
112 if (delimiter == KS_SEP_LINE) { \
113 for (i = ks->begin; i < ks->end; ++i) \
114 if (ks->buf[i] == '\n') break; \
115 } else if (delimiter > KS_SEP_MAX) { \
116 for (i = ks->begin; i < ks->end; ++i) \
117 if (ks->buf[i] == delimiter) break; \
118 } else if (delimiter == KS_SEP_SPACE) { \
119 for (i = ks->begin; i < ks->end; ++i) \
120 if (isspace(ks->buf[i])) break; \
121 } else if (delimiter == KS_SEP_TAB) { \
122 for (i = ks->begin; i < ks->end; ++i) \
123 if (isspace(ks->buf[i]) && ks->buf[i] != ' ') break; \
125 if (str->m - str->l < (size_t)(i - ks->begin + 1)) { \
126 str->m = str->l + (i - ks->begin) + 1; \
127 kroundup32(str->m); \
128 str->s = (char*)realloc(str->s, str->m); \
130 seek_pos += i - ks->begin; if ( i < ks->end ) seek_pos++; \
131 memcpy(str->s + str->l, ks->buf + ks->begin, i - ks->begin); \
132 str->l = str->l + (i - ks->begin); \
135 if (dret) *dret = ks->buf[i]; \
139 ks->seek_pos += seek_pos; \
142 str->s = (char*)calloc(1, 1); \
143 } else if (delimiter == KS_SEP_LINE && str->l > 1 && str->s[str->l-1] == '\r') --str->l; \
144 str->s[str->l] = '\0'; \
148 #define KSTREAM_INIT2(SCOPE, type_t, __read, __bufsize) \
150 __KS_BASIC(SCOPE, type_t, __bufsize) \
151 __KS_GETUNTIL(SCOPE, __read) \
154 #define KSTREAM_INIT(type_t, __read, __bufsize) KSTREAM_INIT2(static, type_t, __read, __bufsize)
156 #define KSTREAM_DECLARE(type_t, __read) \
158 extern int ks_getuntil2(kstream_t *ks, int delimiter, kstring_t *str, int *dret, int append); \
159 extern kstream_t *ks_init(type_t f); \
160 extern void ks_destroy(kstream_t *ks); \
167 #define kseq_rewind(ks) ((ks)->last_char = (ks)->f->is_eof = (ks)->f->begin = (ks)->f->end = 0)
169 #define __KSEQ_BASIC(SCOPE, type_t) \
170 SCOPE kseq_t *kseq_init(type_t fd) \
172 kseq_t *s = (kseq_t*)calloc(1, sizeof(kseq_t)); \
173 s->f = ks_init(fd); \
176 SCOPE void kseq_destroy(kseq_t *ks) \
179 free(ks->name.s); free(ks->comment.s); free(ks->seq.s); free(ks->qual.s); \
189 #define __KSEQ_READ(SCOPE) \
190 SCOPE int kseq_read(kseq_t *seq) \
193 kstream_t *ks = seq->f; \
194 if (seq->last_char == 0) { \
195 while ((c = ks_getc(ks)) != -1 && c != '>' && c != '@'); \
196 if (c == -1) return -1; \
197 seq->last_char = c; \
199 seq->comment.l = seq->seq.l = seq->qual.l = 0; \
200 if (ks_getuntil(ks, 0, &seq->name, &c) < 0) return -1; \
201 if (c != '\n') ks_getuntil(ks, KS_SEP_LINE, &seq->comment, 0); \
202 if (seq->seq.s == 0) { \
204 seq->seq.s = (char*)malloc(seq->seq.m); \
206 while ((c = ks_getc(ks)) != -1 && c != '>' && c != '+' && c != '@') { \
207 if (c == '\n') continue; \
208 seq->seq.s[seq->seq.l++] = c; \
209 ks_getuntil2(ks, KS_SEP_LINE, &seq->seq, 0, 1); \
211 if (c == '>' || c == '@') seq->last_char = c; \
212 if (seq->seq.l + 1 >= seq->seq.m) { \
213 seq->seq.m = seq->seq.l + 2; \
214 kroundup32(seq->seq.m); \
215 seq->seq.s = (char*)realloc(seq->seq.s, seq->seq.m); \
217 seq->seq.s[seq->seq.l] = 0; \
218 if (c != '+') return seq->seq.l; \
219 if (seq->qual.m < seq->seq.m) { \
220 seq->qual.m = seq->seq.m; \
221 seq->qual.s = (char*)realloc(seq->qual.s, seq->qual.m); \
223 while ((c = ks_getc(ks)) != -1 && c != '\n'); \
224 if (c == -1) return -2; \
225 while (ks_getuntil2(ks, KS_SEP_LINE, &seq->qual, 0, 1) >= 0 && seq->qual.l < seq->seq.l); \
226 seq->last_char = 0; \
227 if (seq->seq.l != seq->qual.l) return -2; \
231 #define __KSEQ_TYPE(type_t) \
233 kstring_t name, comment, seq, qual; \
238 #define KSEQ_INIT2(SCOPE, type_t, __read) \
239 KSTREAM_INIT(type_t, __read, 16384) \
240 __KSEQ_TYPE(type_t) \
241 __KSEQ_BASIC(SCOPE, type_t) \
244 #define KSEQ_INIT(type_t, __read) KSEQ_INIT2(static, type_t, __read)
246 #define KSEQ_DECLARE(type_t) \
248 __KSEQ_TYPE(type_t) \
249 extern kseq_t *kseq_init(type_t fd); \
250 void kseq_destroy(kseq_t *ks); \
251 int kseq_read(kseq_t *seq);