Skip to content

Commit

Permalink
Add position(::GZip.Stream,of_raw::Bool=false) that gives the current…
Browse files Browse the repository at this point in the history
… position in the stream on *compressed* bytes instead of the normal uncompressed
  • Loading branch information
quinnj committed May 8, 2015
1 parent dbd40e5 commit 66f6e5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/GZip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ gzread(s::GZipStream, p::Ptr, len::Integer) =
-1)

let _zlib_h = Libdl.dlopen(_zlib)
global gzbuffer, _gzopen, _gzseek, _gztell, _gzrewind, _gzdirect
global gzbuffer, _gzopen, _gzseek, _gztell, _gzrewind, _gzdirect, _gzoffset

# Doesn't exist in zlib 1.2.3 or earlier
if Libdl.dlsym_e(_zlib_h, :gzbuffer) != C_NULL
Expand All @@ -219,12 +219,12 @@ let _zlib_h = Libdl.dlopen(_zlib)
const _gzopen = :gzopen64
const _gzseek = :gzseek64
const _gztell = :gztell64
#_gzoffset = :gzoffset64 ## not implemented
const _gzoffset = :gzoffset64
else
const _gzopen = :gzopen
const _gzseek = :gzseek
const _gztell = :gztell
#_gzoffset = :gzoffset ## not implemented
const _gzoffset = :gzoffset
end
const _gzrewind = :gzrewind
const _gzdirect = :gzdirect
Expand Down Expand Up @@ -340,8 +340,9 @@ skip(s::GZipStream, n::Integer) =
s.gz_file, n, SEEK_CUR)!=-1 ||
error("skip (gzseek) failed")) # Mimick behavior of skip(s::IOStream, n)

position(s::GZipStream) =
ccall((_gztell, _zlib), ZFileOffset, (Ptr{Void},), s.gz_file)
position(s::GZipStream, of_raw::Bool=false) = of_raw ?
ccall((_gzoffset, _zlib), ZFileOffset, (Ptr{Void},), s.gz_file)
ccall((_gztell, _zlib), ZFileOffset, (Ptr{Void},), s.gz_file)

eof(s::GZipStream) = @compat Bool(ccall((:gzeof, _zlib), Int32, (Ptr{Void},), s.gz_file))

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ write(gzfile, data) == length(data.data)
@test flush(gzfile) == Z_OK

pos = position(gzfile)
pos2 = position(gzfile,true)
@test_throws ErrorException seek(gzfile, 100) # can't seek backwards on write
@test position(gzfile) == pos
@test position(gzfile,true) == pos2
@test skip(gzfile, 100)
@test position(gzfile) == pos + 100
@test position(gzfile,true) == pos

@test_throws MethodError truncate(gzfile, 100)
@test_throws MethodError seekend(gzfile)
Expand Down

0 comments on commit 66f6e5b

Please sign in to comment.