Skip to content

Commit

Permalink
Merge pull request #22 from garborg/track04
Browse files Browse the repository at this point in the history
Remove deprecation warnings on 0.4
  • Loading branch information
jakebolewski committed Mar 19, 2015
2 parents 810f370 + c1f70c7 commit df5704c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 82 deletions.
4 changes: 2 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Notes
* This implementation mimics the :class:`IOStream` implementation, and should
be a drop-in replacement for :class:`IOStream`, with some caveats:

* :func:`seek_end` and :func:`truncate` are not available
* :func:`seekend` and :func:`truncate` are not available
* :func:`readuntil` is available, but is not very efficient.
(But :func:`readline` works fine.)

Expand All @@ -43,7 +43,7 @@ following :class:`IO`/:class:`IOStream` functions are supported:
* :func:`write()`
* :func:`peek()`

Due to limitations in ``zlib``, :func:`seek_end` and :func:`truncate` are not available.
Due to limitations in ``zlib``, :func:`seekend` and :func:`truncate` are not available.

---------
Functions
Expand Down
69 changes: 38 additions & 31 deletions src/GZip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module GZip
using Compat

import Base: show, fd, close, flush, truncate, seek,
skip, position, eof, read, readall,
seekend, skip, position, eof, read, readall,
readline, write, peek

export
Expand Down Expand Up @@ -94,7 +94,7 @@ GZipStream(name::String, gz_file::Ptr{Void}) = GZipStream(name, gz_file, Z_DEFAU
function gzerror(err::Integer, s::GZipStream)
e = Int32[err]
if !s._closed
msg_p = ccall((:gzerror, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Int32}),
msg_p = ccall((:gzerror, _zlib), Ptr{UInt8}, (Ptr{Void}, Ptr{Int32}),
s.gz_file, e)
msg = (msg_p == C_NULL ? "" : bytestring(msg_p))
else
Expand All @@ -108,7 +108,7 @@ type GZError <: Exception
err::Int32
err_str::String

GZError(e::Integer, str::String) = new(int32(e), str)
GZError(e::Integer, str::String) = new(@compat(Int32(e)), str)
GZError(e::Integer, s::GZipStream) = (a = gzerror(e, s); new(a[1], a[2]))
GZError(s::GZipStream) = (a = gzerror(s); new(a[1], a[2]))
end
Expand Down Expand Up @@ -140,7 +140,7 @@ macro test_gzerror(s, cc, val)
quote
if $(esc(s))._closed throw(EOFError()) end
ret = $(esc(cc))
if ret == $(esc(val)) throw(ret, GZError($(esc(s)))) end
if ret == $(esc(val)) throw(GZError(ret, $(esc(s)))) end
ret
end
end
Expand Down Expand Up @@ -171,35 +171,44 @@ gzgetc_raw(s::GZipStream) = ccall((:gzgetc, _zlib), Int32, (Ptr{Void},), s.gz_fi
gzungetc(c::Integer, s::GZipStream) =
@test_eof_gzerr(s, ccall((:gzungetc, _zlib), Int32, (Int32, Ptr{Void}), c, s.gz_file), -1)

gzgets(s::GZipStream, a::Array{Uint8}) =
@test_eof_gzerr2(s, ccall((:gzgets, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
s.gz_file, a, int32(length(a))), C_NULL)
gzgets(s::GZipStream, a::Array{UInt8}) =
@test_eof_gzerr2(s,
ccall((:gzgets, _zlib), Ptr{UInt8}, (Ptr{Void}, Ptr{UInt8}, Int32),
s.gz_file, a, @compat(Int32(length(a)))),
C_NULL)

gzgets(s::GZipStream, p::Ptr{Uint8}, len::Integer) =
@test_eof_gzerr2(s, ccall((:gzgets, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Uint8}, Int32),
s.gz_file, p, int32(len)), C_NULL)
gzgets(s::GZipStream, p::Ptr{UInt8}, len::Integer) =
@test_eof_gzerr2(s,
ccall((:gzgets, _zlib), Ptr{UInt8}, (Ptr{Void}, Ptr{UInt8}, Int32),
s.gz_file, p, @compat(Int32(len))),
C_NULL)

gzputc(s::GZipStream, c::Integer) =
@test_gzerror(s, ccall((:gzputc, _zlib), Int32, (Ptr{Void}, Int32),
s.gz_file, int32(c)), -1)
@test_gzerror(s,
ccall((:gzputc, _zlib), Int32, (Ptr{Void}, Int32),
s.gz_file, @compat(Int32(c))),
-1)

gzwrite(s::GZipStream, p::Ptr, len::Integer) =
len == 0 ? int32(0) : @test_gzerror0(s, ccall((:gzwrite, _zlib), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
s.gz_file, p, len))
len == 0 ? @compat(Int32(0)) :
@test_gzerror0(s, ccall((:gzwrite, _zlib), Int32, (Ptr{Void}, Ptr{Void}, UInt32),
s.gz_file, p, len))

gzread(s::GZipStream, p::Ptr, len::Integer) =
@test_gzerror(s, ccall((:gzread, _zlib), Int32, (Ptr{Void}, Ptr{Void}, Uint32),
s.gz_file, p, len), -1)
@test_gzerror(s,
ccall((:gzread, _zlib), Int32, (Ptr{Void}, Ptr{Void}, UInt32),
s.gz_file, p, len),
-1)

let _zlib_h = dlopen(_zlib)
global gzbuffer, _gzopen, _gzseek, _gztell

# Doesn't exist in zlib 1.2.3 or earlier
if dlsym_e(_zlib_h, :gzbuffer) != C_NULL
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) =
ccall((:gzbuffer, _zlib), Int32, (Ptr{Void}, Uint32), gz_file, gz_buf_size)
ccall((:gzbuffer, _zlib), Int32, (Ptr{Void}, UInt32), gz_file, gz_buf_size)
else
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = int32(-1)
gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = @compat Int32(-1)
end

#####
Expand Down Expand Up @@ -232,7 +241,7 @@ function gzopen(fname::String, gzmode::String, gz_buf_size::Integer)
gzmode *= "b"
end

gz_file = ccall((_gzopen, _zlib), Ptr{Void}, (Ptr{Uint8}, Ptr{Uint8}), fname, gzmode)
gz_file = ccall((_gzopen, _zlib), Ptr{Void}, (Ptr{UInt8}, Ptr{UInt8}), fname, gzmode)
if gz_file == C_NULL
throw(GZError(-1, "gzopen failed"))
end
Expand Down Expand Up @@ -264,7 +273,7 @@ function gzdopen(name::String, fd::Integer, gzmode::String, gz_buf_size::Integer
# not to close the original fd
dup_fd = ccall(:dup, Int32, (Int32,), fd)

gz_file = ccall((:gzdopen, _zlib), Ptr{Void}, (Int32, Ptr{Uint8}), dup_fd, gzmode)
gz_file = ccall((:gzdopen, _zlib), Ptr{Void}, (Int32, Ptr{UInt8}), dup_fd, gzmode)
if gz_file == C_NULL
throw(GZError(-1, "gzdopen failed"))
end
Expand Down Expand Up @@ -306,19 +315,17 @@ function close(s::GZipStream)
end

flush(s::GZipStream, fl::Integer) =
@test_z_ok ccall((:gzflush, _zlib), Int32, (Ptr{Void}, Int32), s.gz_file, int32(fl))
@test_z_ok ccall((:gzflush, _zlib), Int32, (Ptr{Void}, Int32), s.gz_file, @compat(Int32(fl)))
flush(s::GZipStream) = flush(s, Z_SYNC_FLUSH)

truncate(s::GZipStream, n::Integer) = error("truncate is not supported for GZipStreams")
truncate(s::GZipStream, n::Integer) = throw(MethodError(truncate, (GZipStream, Integer)))

# Note: seeks to byte position within uncompressed data stream
seek(s::GZipStream, n::Integer) =
(ccall((_gzseek, _zlib), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
s.gz_file, n, SEEK_SET)!=-1 || # Mimick behavior of seek(s::IOStream, n)
error("seek (gzseek) failed"))

seek_end(s::GZipStream) = error("seek_end is not supported for GZipStreams")

# Note: skips bytes within uncompressed data stream
skip(s::GZipStream, n::Integer) =
(ccall((_gzseek, _zlib), ZFileOffset, (Ptr{Void}, ZFileOffset, Int32),
Expand All @@ -328,7 +335,7 @@ skip(s::GZipStream, n::Integer) =
position(s::GZipStream) =
ccall((_gztell, _zlib), ZFileOffset, (Ptr{Void},), s.gz_file)

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

function peek(s::GZipStream)
c = gzgetc_raw(s)
Expand All @@ -344,7 +351,7 @@ function read{T}(s::GZipStream, a::Array{T})
nb = length(a)*sizeof(T)
# Note: this will overflow and succeed without warning if nb > 4GB
ret = ccall((:gzread, _zlib), Int32,
(Ptr{Void}, Ptr{Void}, Uint32), s.gz_file, a, nb)
(Ptr{Void}, Ptr{Void}, UInt32), s.gz_file, a, nb)
if ret == -1
throw(GZError(s))
end
Expand All @@ -358,20 +365,20 @@ function read{T}(s::GZipStream, a::Array{T})
end
end

function read(s::GZipStream, ::Type{Uint8})
function read(s::GZipStream, ::Type{UInt8})
ret = gzgetc(s)
if ret == -1
throw(GZError(s))
end
peek(s) # force eof to be set
uint8(ret)
@compat UInt8(ret)
end


# For this function, it's really unfortunate that zlib is
# not integrated with ios
function readall(s::GZipStream, bufsize::Int)
buf = Array(Uint8, bufsize)
buf = Array(UInt8, bufsize)
len = 0
while true
ret = gzread(s, pointer(buf)+len, bufsize)
Expand Down Expand Up @@ -402,7 +409,7 @@ end
readall(s::GZipStream) = readall(s, Z_BIG_BUFSIZE)

function readline(s::GZipStream)
buf = Array(Uint8, GZ_LINE_BUFSIZE)
buf = Array(UInt8, GZ_LINE_BUFSIZE)
pos = 1

if gzgets(s, buf) == C_NULL # Throws an exception on error
Expand Down Expand Up @@ -434,7 +441,7 @@ function readline(s::GZipStream)
end
end

write(s::GZipStream, b::Uint8) = gzputc(s, b)
write(s::GZipStream, b::UInt8) = gzputc(s, b)

function write{T}(s::GZipStream, a::Array{T})
if isbits(T)
Expand Down
77 changes: 39 additions & 38 deletions src/zlib_h.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@

# Constants

zlib_version = bytestring(ccall((:zlibVersion, _zlib), Ptr{Uint8}, ()))
ZLIB_VERSION = tuple([int(c) for c in split(zlib_version, '.')]...)
zlib_version = bytestring(ccall((:zlibVersion, _zlib), Ptr{UInt8}, ()))
ZLIB_VERSION = tuple([parse(Int, c) for c in split(zlib_version, '.')]...)

# Flush values
const Z_NO_FLUSH = int32(0)
const Z_PARTIAL_FLUSH = int32(1)
const Z_SYNC_FLUSH = int32(2)
const Z_FULL_FLUSH = int32(3)
const Z_FINISH = int32(4)
const Z_BLOCK = int32(5)
const Z_TREES = int32(6)
const Z_NO_FLUSH = @compat Int32(0)
const Z_PARTIAL_FLUSH = @compat Int32(1)
const Z_SYNC_FLUSH = @compat Int32(2)
const Z_FULL_FLUSH = @compat Int32(3)
const Z_FINISH = @compat Int32(4)
const Z_BLOCK = @compat Int32(5)
const Z_TREES = @compat Int32(6)

# Return codes
const Z_OK = int32(0)
const Z_STREAM_END = int32(1)
const Z_NEED_DICT = int32(2)
const Z_ERRNO = int32(-1)
const Z_STREAM_ERROR = int32(-2)
const Z_DATA_ERROR = int32(-3)
const Z_MEM_ERROR = int32(-4)
const Z_BUF_ERROR = int32(-5)
const Z_VERSION_ERROR = int32(-6)
const Z_OK = @compat Int32(0)
const Z_STREAM_END = @compat Int32(1)
const Z_NEED_DICT = @compat Int32(2)
const Z_ERRNO = @compat Int32(-1)
const Z_STREAM_ERROR = @compat Int32(-2)
const Z_DATA_ERROR = @compat Int32(-3)
const Z_MEM_ERROR = @compat Int32(-4)
const Z_BUF_ERROR = @compat Int32(-5)
const Z_VERSION_ERROR = @compat Int32(-6)


# Zlib errors as Exceptions
zerror(e::Integer) = bytestring(ccall((:zError, _zlib), Ptr{Uint8}, (Int32,), e))
zerror(e::Integer) = bytestring(ccall((:zError, _zlib), Ptr{UInt8}, (Int32,), e))
type ZError <: Exception
err::Int32
err_str::String
Expand All @@ -39,26 +39,26 @@ type ZError <: Exception
end

# Compression Levels
const Z_NO_COMPRESSION = int32(0)
const Z_BEST_SPEED = int32(1)
const Z_BEST_COMPRESSION = int32(9)
const Z_DEFAULT_COMPRESSION = int32(-1)
const Z_NO_COMPRESSION = @compat Int32(0)
const Z_BEST_SPEED = @compat Int32(1)
const Z_BEST_COMPRESSION = @compat Int32(9)
const Z_DEFAULT_COMPRESSION = @compat Int32(-1)

# Compression Strategy
const Z_FILTERED = int32(1)
const Z_HUFFMAN_ONLY = int32(2)
const Z_RLE = int32(3)
const Z_FIXED = int32(4)
const Z_DEFAULT_STRATEGY = int32(0)
const Z_FILTERED = @compat Int32(1)
const Z_HUFFMAN_ONLY = @compat Int32(2)
const Z_RLE = @compat Int32(3)
const Z_FIXED = @compat Int32(4)
const Z_DEFAULT_STRATEGY = @compat Int32(0)

# data_type field
const Z_BINARY = int32(0)
const Z_TEXT = int32(1)
const Z_BINARY = @compat Int32(0)
const Z_TEXT = @compat Int32(1)
const Z_ASCII = Z_TEXT
const Z_UNKNOWN = int32(2)
const Z_UNKNOWN = @compat Int32(2)

# deflate compression method
const Z_DEFLATED = int32(8)
const Z_DEFLATED = @compat Int32(8)

# misc
const Z_NULL = C_NULL
Expand All @@ -68,22 +68,23 @@ const Z_DEFAULT_BUFSIZE = 8192
const Z_BIG_BUFSIZE = 131072

# Constants for use with gzseek
const SEEK_SET = int32(0)
const SEEK_CUR = int32(1)
const SEEK_SET = @compat Int32(0)
const SEEK_CUR = @compat Int32(1)

# Create ZFileOffset alias
# This is usually the same as FileOffset,
# This is usually the same as FileOffset,
# unless we're on a 32-bit system and
# 64-bit functions are not available

# Get compile-time option flags
zlib_compile_flags = ccall((:zlibCompileFlags, _zlib), Uint, ())
zlib_compile_flags = ccall((:zlibCompileFlags, _zlib), UInt, ())

let _zlib_h = dlopen(_zlib)
global ZFileOffset

z_off_t_sz = 2 << ((zlib_compile_flags >> 6) & uint(3))
if z_off_t_sz == sizeof(FileOffset) || (sizeof(FileOffset) == 8 && dlsym_e(_zlib_h, :gzopen64) != C_NULL)
z_off_t_sz = 2 << ((zlib_compile_flags >> 6) & @compat(UInt(3)))
if z_off_t_sz == sizeof(FileOffset) ||
(sizeof(FileOffset) == 8 && dlsym_e(_zlib_h, :gzopen64) != C_NULL)
typealias ZFileOffset FileOffset
elseif z_off_t_sz == 4 # 64-bit functions not available
typealias ZFileOffset Int32
Expand Down
Loading

0 comments on commit df5704c

Please sign in to comment.