163
163
# Easy access to gz reading/writing functions (Internal)
164
164
gzgetc(s::GZipStream) =
165
@test_eof_gzerr(s, ccall(dlsym(_zlib, :gzgetc), Int32, (Ptr{Void},), s.gz_file), -1)
165
@test_eof_gzerr(s, ccall((:gzgetc, _zlib), Int32, (Ptr{Void},), s.gz_file), -1)
167
167
gzungetc(c::Integer, s::GZipStream) =
168
@test_eof_gzerr(s, ccall(dlsym(_zlib, :gzungetc), Int32, (Int32, Ptr{Void}), c, s.gz_file), -1)
168
@test_eof_gzerr(s, ccall((:gzungetc, _zlib), Int32, (Int32, Ptr{Void}), c, s.gz_file), -1)
170
170
gzgets(s::GZipStream, a::Array{Uint8}) =
171
@test_eof_gzerr2(s, ccall(dlsym(_zlib, :gzgets), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
171
@test_eof_gzerr2(s, ccall((:gzgets, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
172
172
s.gz_file, a, int32(length(a))), C_NULL)
174
174
gzgets(s::GZipStream, p::Ptr{Uint8}, len::Integer) =
175
@test_eof_gzerr2(s, ccall(dlsym(_zlib, :gzgets), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
175
@test_eof_gzerr2(s, ccall((:gzgets, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
176
176
s.gz_file, p, int32(len)), C_NULL)
178
178
gzputc(s::GZipStream, c::Integer) =
179
@test_gzerror(s, ccall(dlsym(_zlib, :gzputc), Int32, (Ptr{Void}, Int32),
179
@test_gzerror(s, ccall((:gzputc, _zlib), Int32, (Ptr{Void}, Int32),
180
180
s.gz_file, int32(c)), -1)
182
182
gzwrite(s::GZipStream, p::Ptr, len::Integer) =
183
@test_gzerror0(s, ccall(dlsym(_zlib, :gzwrite), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
183
@test_gzerror0(s, ccall((:gzwrite, _zlib), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
184
184
s.gz_file, p, len))
186
186
gzread(s::GZipStream, p::Ptr, len::Integer) =
187
@test_gzerror(s, ccall(dlsym(_zlib, :gzread), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
187
@test_gzerror(s, ccall((:gzread, _zlib), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
188
188
s.gz_file, p, len), -1)
190
# Doesn't exist in zlib 1.2.3 or earlier
191
if dlsym_e(_zlib, :gzbuffer) != C_NULL
192
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) =
193
ccall(dlsym(_zlib, :gzbuffer), Int32, (Ptr{Void}, Uint32), gz_file, gz_buf_size)
195
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = int32(-1)
200
# Use 64-bit functions if available
202
if dlsym_e(_zlib, :gzopen64) != C_NULL
203
const _gzopen = :gzopen64
204
const _gzseek = :gzseek64
205
const _gztell = :gztell64
206
#_gzoffset = :gzoffset64 ## not implemented
208
const _gzopen = :gzopen
209
const _gzseek = :gzseek
210
const _gztell = :gztell
211
#_gzoffset = :gzoffset ## not implemented
190
let _zlib_h = dlopen("libz.so.1")
191
global gzbuffer, _gzopen, _gzseek, _gztell
193
# Doesn't exist in zlib 1.2.3 or earlier
194
if dlsym_e(_zlib_h, :gzbuffer) != C_NULL
195
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) =
196
ccall((:gzbuffer, _zlib), Int32, (Ptr{Void}, Uint32), gz_file, gz_buf_size)
198
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = int32(-1)
203
# Use 64-bit functions if available
205
if dlsym_e(_zlib_h, :gzopen64) != C_NULL
206
const _gzopen = :gzopen64
207
const _gzseek = :gzseek64
208
const _gztell = :gztell64
209
#_gzoffset = :gzoffset64 ## not implemented
211
const _gzopen = :gzopen
212
const _gzseek = :gzseek
213
const _gztell = :gztell
214
#_gzoffset = :gzoffset ## not implemented
214
218
function gzopen(fname::String, gzmode::String, gz_buf_size::Integer)
295
299
s.name *= " (closed)"
297
ret = (@test_z_ok ccall(dlsym(_zlib, :gzclose), Int32, (Ptr{Void},), s.gz_file))
301
ret = (@test_z_ok ccall((:gzclose, _zlib), Int32, (Ptr{Void},), s.gz_file))
302
306
flush(s::GZipStream, fl::Integer) =
303
@test_z_ok ccall(dlsym(_zlib, :gzflush), Int32, (Ptr{Void}, Int32), s.gz_file, int32(fl))
307
@test_z_ok ccall((:gzflush, _zlib), Int32, (Ptr{Void}, Int32), s.gz_file, int32(fl))
304
308
flush(s::GZipStream) = flush(s, Z_SYNC_FLUSH)
306
310
truncate(s::GZipStream, n::Integer) = error("truncate is not supported for GZipStreams")
308
312
# Note: seeks to byte position within uncompressed data stream
309
313
seek(s::GZipStream, n::Integer) =
310
(ccall(dlsym(_zlib, _gzseek), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
314
(ccall((_gzseek, _zlib), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
311
315
s.gz_file, n, SEEK_SET)!=-1 || # Mimick behavior of seek(s::IOStream, n)
312
316
error("seek (gzseek) failed"))
316
320
# Note: skips bytes within uncompressed data stream
317
321
skip(s::GZipStream, n::Integer) =
318
(ccall(dlsym(_zlib, _gzseek), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
322
(ccall((_gzseek, _zlib), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
319
323
s.gz_file, n, SEEK_CUR)!=-1 ||
320
324
error("skip (gzseek) failed")) # Mimick behavior of skip(s::IOStream, n)
322
326
position(s::GZipStream) =
323
ccall(dlsym(_zlib, _gztell), ZFileOffset, (Ptr{Void},), s.gz_file)
327
ccall((_gztell, _zlib), ZFileOffset, (Ptr{Void},), s.gz_file)
325
eof(s::GZipStream) = bool(ccall(dlsym(_zlib, :gzeof), Int32, (Ptr{Void},), s.gz_file))
329
eof(s::GZipStream) = bool(ccall((:gzeof, _zlib), Int32, (Ptr{Void},), s.gz_file))
327
331
function check_eof(s::GZipStream)
328
332
# Force eof to be set...
398
402
readall(s::GZipStream) = readall(s, Z_BIG_BUFSIZE)
400
404
# TODO: Create a c-wrapper based on gzreadline
401
function readuntil(s::GZipStream, delim)
405
buf = memio(GZ_LINE_BUFSIZE, false)
408
while c != delim && !eof(s)
424
function readline(s::GZipStream)
405
function readuntil(s::GZipStream, c::Uint8)
425
406
buf = Array(Uint8, GZ_LINE_BUFSIZE)
428
409
if gzgets(s, buf) == C_NULL # Throws an exception on error
433
414
# since gzgets didn't return C_NULL, there must be a \0 in the buffer
434
415
eos = memchr(buf, '\0', pos)
435
if eos == 1 || buf[eos-1] == '\n'
436
return bytestring(buf[1:eos-1])
416
if eos == 1 || buf[eos-1] == c
439
420
# If we're at the end of the file, return the string
440
if eof(s) return bytestring(buf[1:eos-1]) end
421
if eof(s) return buf[1:eos-1] end
442
423
# Otherwise, append to the end of the previous buffer