-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnstream.c
325 lines (250 loc) · 6.84 KB
/
nstream.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Copyright (C) 2002 - 2007 Pawel A. Gajda <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License, version 2 as
published by the Free Software Foundation (see file COPYING for details).
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include "nassert.h"
#include "nmalloc.h"
#include "nstream.h"
#include "ndie.h"
#define ZLIB_TRACE 0
static int do_gz_flush(void *stream)
{
return gzflush(stream, Z_FULL_FLUSH);
}
static int do_s_read(void *stream, void *buf, size_t size)
{
return fread(buf, 1, size, stream);
}
static int do_s_write(void *stream, const void *buf, size_t size)
{
return fwrite(buf, 1, size, stream);
}
static char *do_s_gets(void *stream, char *buf, size_t size)
{
return fgets(buf, size, stream);
}
static int do_gz_getc(void *stream)
{
register int c = gzgetc((gzFile)stream);
if (c == -1)
return EOF;
return c;
}
#if HAVE_GZUNGETC /* zlib >= 1.2.0.2 */
static int do_gz_ungetc(int c, void *stream)
{
register int cc = gzungetc(c, stream);
if (cc == -1)
return EOF;
n_assert(cc == c);
return cc;
}
#endif
static int zlib_fseek_wrap(void *stream, long offset, int whence)
{
z_off_t rc, off = offset;
rc = gzseek(stream, off, whence);
if (rc > 0)
rc = 0;
#if ZLIB_TRACE
if (rc < 0) {
int ec;
printf("zlib_fseek: %s\n", gzerror(stream, &ec));
}
#endif
return rc;
}
static tn_stream *n_stream_new(int type)
{
tn_stream *st;
st = n_malloc(sizeof(*st));
st->stream = NULL;
st->fd = -1;
st->type = type;
st->_write_hook = NULL;
st->_write_hook_arg = NULL;
switch (type) {
case TN_STREAM_STDIO:
st->st_open = (void *(*)(const char *, const char *))fopen;
st->st_dopen = (void *(*)(int, const char *))fdopen;
st->st_read = (int (*)(void*, void*, size_t))do_s_read;
st->st_write = (int (*)(void*, const void*, size_t))do_s_write;
st->st_gets = do_s_gets;
st->st_getc = (int (*)(void*))getc;
st->st_ungetc = (int (*)(int, void*))ungetc;
st->st_seek = (int (*)(void*, long, int))fseek;
st->st_tell = (long (*)(void*))ftell;
st->st_flush = (int (*)(void*))fflush;
st->st_close = (int (*)(void*))fclose;
break;
case TN_STREAM_GZIO:
st->st_open = (void *(*)(const char *, const char *))gzopen;
st->st_dopen = (void *(*)(int, const char *))gzdopen;
st->st_read = (int (*)(void*, void*, size_t))gzread;
st->st_write = (int (*)(void*, const void*, size_t))gzwrite;
st->st_gets = (char *(*)(void*, char*, size_t))gzgets;
st->st_getc = do_gz_getc;
#if HAVE_GZUNGETC
st->st_ungetc = do_gz_ungetc;
#else
st->st_ungetc = NULL;
#endif
st->st_seek = (int (*)(void*, long, int))zlib_fseek_wrap;
st->st_tell = (long (*)(void*))gztell;
st->st_flush = do_gz_flush;
st->st_close = (int (*)(void*))gzclose;
break;
default:
n_die("%d: unknown stream type\n", type);
n_assert(0);
break;
}
return st;
}
static int determine_type(const char *path, int *type)
{
const char *p;
int real_type;
if (*type == TN_STREAM_UNKNOWN)
*type = TN_STREAM_STDIO;
real_type = *type;
if ((p = strrchr(path, '.')) && strcmp(p, ".gz") == 0) {
*type = TN_STREAM_GZIO;
real_type = TN_STREAM_GZIO;
}
return real_type;
}
static gzFile do_gz_open(const char *path, const char *mode)
{
gzFile gzstream;
errno = 0;
if ((gzstream = gzopen(path, mode)) == NULL) {
if (errno == 0) {
if (Z_MEM_ERROR)
errno = ENOMEM;
else
errno = EIO;
}
}
return gzstream;
}
/* RET: bool */
static int do_open(tn_stream *st, const char *path, const char *mode,
int type, int real_type)
{
int rc = 1;
real_type = determine_type(path, &type);
switch (real_type) {
case TN_STREAM_STDIO:
if ((st->stream = fopen(path, mode)) == NULL)
rc = 0;
break;
case TN_STREAM_GZIO:
if ((st->stream = do_gz_open(path, mode)) == NULL)
rc = 0;
break;
default:
n_assert(0);
}
return rc;
}
tn_stream *n_stream_open(const char *path, const char *mode, int type)
{
tn_stream *st;
int real_type;
real_type = determine_type(path, &type);
if ((st = n_stream_new(type)) == NULL)
return NULL;
if (!do_open(st, path, mode, type, real_type)) {
n_stream_close(st);
st = NULL;
}
return st;
}
tn_stream *n_stream_dopen(int fd, const char *mode, int type)
{
tn_stream *st;
void *stream;
if ((st = n_stream_new(type)) == NULL)
return NULL;
if ((stream = st->st_dopen(fd, mode))) {
st->stream = stream;
st->fd = fd;
} else {
n_stream_close(st);
st = NULL;
}
return st;
}
int n_stream_vprintf(tn_stream *st, const char *fmt, va_list ap)
{
char buf[1024 * 32];
int n;
n = vsnprintf(buf, sizeof(buf), fmt, ap);
return st->st_write(st->stream, buf, n);
}
int n_stream_printf(tn_stream *st, const char *fmt, ...)
{
va_list ap;
int n;
va_start(ap, fmt);
n = n_stream_vprintf(st, fmt, ap);
va_end(ap);
return n;
}
inline
int n_stream_gets(tn_stream *st, char *buf, size_t size)
{
if (st->st_gets(st->stream, buf, size))
return strlen(buf);
return 0;
}
#ifndef MAX_CANON
# define MAX_CANON 256
#endif
#undef getc
int n_stream_getline(tn_stream *st, char **bufptr, size_t size)
{
char *buf = *bufptr;
size_t n;
if (buf == NULL) {
buf = n_malloc(MAX_CANON);
size = MAX_CANON;
}
*buf = '\0';
n = n_stream_gets(st, buf, size);
if (n < size - 1) {
*bufptr = buf;
return n;
}
while (1) {
register int c;
if (n == size - 1) {
size *= 2;
buf = n_realloc(buf, size);
}
c = st->st_getc(st->stream);
if (c == EOF)
break;
buf[n++] = c;
if (c == '\n')
break;
}
buf[n] = '\0';
*bufptr = buf;
return n;
}