1 | #include "wandio.h" |
---|
2 | #include "config.h" |
---|
3 | #include <stdlib.h> |
---|
4 | #include <assert.h> |
---|
5 | #include <errno.h> |
---|
6 | |
---|
7 | struct compression_type compression_type[] = { |
---|
8 | { "GZ", "gz", WANDIO_COMPRESS_ZLIB }, |
---|
9 | { "BZ2", "bz2", WANDIO_COMPRESS_BZ2 }, |
---|
10 | { "NONE", "", WANDIO_COMPRESS_NONE } |
---|
11 | }; |
---|
12 | |
---|
13 | #define READ_TRACE 0 |
---|
14 | #define WRITE_TRACE 0 |
---|
15 | |
---|
16 | io_t *wandio_create(const char *filename) |
---|
17 | { |
---|
18 | io_t *io = peek_open(stdio_open(filename)); |
---|
19 | char buffer[1024]; |
---|
20 | int len; |
---|
21 | if (!io) |
---|
22 | return NULL; |
---|
23 | len = wandio_peek(io, buffer, sizeof(buffer)); |
---|
24 | #if HAVE_LIBZ |
---|
25 | /* auto detect gzip compressed data */ |
---|
26 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { |
---|
27 | io = zlib_open(io); |
---|
28 | } |
---|
29 | /* auto detect compress(1) compressed data (gzip can read this) */ |
---|
30 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') { |
---|
31 | io = zlib_open(io); |
---|
32 | } |
---|
33 | #endif |
---|
34 | #if HAVE_LIBBZ2 |
---|
35 | /* auto detect bzip compressed data */ |
---|
36 | if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { |
---|
37 | io = bz_open(io); |
---|
38 | } |
---|
39 | #endif |
---|
40 | return peek_open(thread_open(io)); |
---|
41 | } |
---|
42 | |
---|
43 | off_t wandio_tell(io_t *io) |
---|
44 | { |
---|
45 | if (!io->source->tell) { |
---|
46 | errno = -ENOSYS; |
---|
47 | return -1; |
---|
48 | } |
---|
49 | return io->source->tell(io); |
---|
50 | } |
---|
51 | |
---|
52 | off_t wandio_seek(io_t *io, off_t offset, int whence) |
---|
53 | { |
---|
54 | if (!io->source->seek) { |
---|
55 | errno = -ENOSYS; |
---|
56 | return -1; |
---|
57 | } |
---|
58 | return io->source->seek(io,offset,whence); |
---|
59 | } |
---|
60 | |
---|
61 | off_t wandio_read(io_t *io, void *buffer, off_t len) |
---|
62 | { |
---|
63 | off_t ret; |
---|
64 | #if READ_TRACE |
---|
65 | fprintf(stderr,"read(%s): %d bytes\n",io->source->name, (int)len); |
---|
66 | #endif |
---|
67 | ret=io->source->read(io,buffer,len); |
---|
68 | return ret; |
---|
69 | } |
---|
70 | |
---|
71 | off_t wandio_peek(io_t *io, void *buffer, off_t len) |
---|
72 | { |
---|
73 | off_t ret; |
---|
74 | assert(io->source->peek); /* If this fails, it means you're calling |
---|
75 | * peek on something that doesn't support |
---|
76 | * peeking. Push a peek_open() on the io |
---|
77 | * first. |
---|
78 | */ |
---|
79 | ret=io->source->peek(io, buffer, len); |
---|
80 | return ret; |
---|
81 | } |
---|
82 | |
---|
83 | void wandio_destroy(io_t *io) |
---|
84 | { io->source->close(io); } |
---|
85 | |
---|
86 | iow_t *wandio_wcreate(const char *filename, int compression_level, int flags) |
---|
87 | { |
---|
88 | iow_t *iow; |
---|
89 | |
---|
90 | assert ( compression_level >= 0 && compression_level <= 9 ); |
---|
91 | |
---|
92 | iow=stdio_wopen(filename); |
---|
93 | #if HAVE_LIBZ |
---|
94 | if (compression_level != 0 && |
---|
95 | (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_ZLIB) { |
---|
96 | iow = zlib_wopen(iow,compression_level); |
---|
97 | } |
---|
98 | #endif |
---|
99 | #if HAVE_LIBBZ2 |
---|
100 | else if (compression_level != 0 && |
---|
101 | (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_BZ2) { |
---|
102 | iow = bz_wopen(iow,compression_level); |
---|
103 | } |
---|
104 | #endif |
---|
105 | return thread_wopen(iow); |
---|
106 | } |
---|
107 | |
---|
108 | off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len) |
---|
109 | { |
---|
110 | #if WRITE_TRACE |
---|
111 | fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len); |
---|
112 | #endif |
---|
113 | return iow->source->write(iow,buffer,len); |
---|
114 | } |
---|
115 | |
---|
116 | void wandio_wdestroy(iow_t *iow) |
---|
117 | { |
---|
118 | iow->source->close(iow); |
---|
119 | } |
---|
120 | |
---|