~ubuntu-branches/ubuntu/wily/julia/wily

« back to all changes in this revision

Viewing changes to extras/gzip.jl

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-01-16 12:29:42 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130116122942-x86e42akjq31repw
Tags: 0.0.0+20130107.gitd9656f41-1
* New upstream snashot
* No longer try to rebuild helpdb.jl.
   + debian/rules: remove helpdb.jl from build-arch rule
   + debian/control: move back python-sphinx to Build-Depends-Indep
* debian/copyright: reflect upstream changes
* Add Build-Conflicts on libatlas3-base (makes linalg tests fail)
* debian/rules: replace obsolete USE_DEBIAN makeflag by a list of
  USE_SYSTEM_* flags
* debian/rules: on non-x86 systems, use libm instead of openlibm
* dpkg-buildflags.patch: remove patch, applied upstream
* Refreshed other patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
function gzerror(err::Integer, s::GZipStream)
93
93
    e = Int32[err]
94
94
    if !s._closed
95
 
        msg_p = ccall(dlsym(_zlib, :gzerror), Ptr{Uint8}, (Ptr{Void}, Ptr{Int32}),
 
95
        msg_p = ccall((:gzerror, _zlib), Ptr{Uint8}, (Ptr{Void}, Ptr{Int32}),
96
96
                      s.gz_file, e)
97
97
        msg = (msg_p == C_NULL ? "" : bytestring(msg_p))
98
98
    else
162
162
 
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)
166
166
 
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)
169
169
 
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)
173
173
 
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)
177
177
 
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)
181
181
 
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))
185
185
 
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)
189
189
 
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)
194
 
else
195
 
    gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = int32(-1)
196
 
end
197
 
 
198
 
#####
199
 
 
200
 
# Use 64-bit functions if available
201
 
 
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
207
 
else
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
 
192
 
 
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)
 
197
    else
 
198
        gzbuffer(gz_file::Ptr, gz_buf_size::Integer) = int32(-1)
 
199
    end
 
200
 
 
201
    #####
 
202
 
 
203
    # Use 64-bit functions if available
 
204
 
 
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
 
210
    else
 
211
        const _gzopen = :gzopen
 
212
        const _gzseek = :gzseek
 
213
        const _gztell = :gztell
 
214
        #_gzoffset = :gzoffset    ## not implemented
 
215
    end
212
216
end
213
217
 
214
218
function gzopen(fname::String, gzmode::String, gz_buf_size::Integer)
224
228
        gzmode *= "b"
225
229
    end
226
230
 
227
 
    gz_file = ccall(dlsym(_zlib, _gzopen), Ptr{Void}, (Ptr{Uint8}, Ptr{Uint8}), fname, gzmode)
 
231
    gz_file = ccall((_gzopen, _zlib), Ptr{Void}, (Ptr{Uint8}, Ptr{Uint8}), fname, gzmode)
228
232
    if gz_file == C_NULL
229
233
        throw(GZError(-1, "gzopen failed"))
230
234
    end
258
262
    # not to close the original fd
259
263
    dup_fd = ccall(:dup, Int32, (Int32,), fd)
260
264
 
261
 
    gz_file = ccall(dlsym(_zlib, :gzdopen), Ptr{Void}, (Int32, Ptr{Uint8}), dup_fd, gzmode)
 
265
    gz_file = ccall((:gzdopen, _zlib), Ptr{Void}, (Int32, Ptr{Uint8}), dup_fd, gzmode)
262
266
    if gz_file == C_NULL
263
267
        throw(GZError(-1, "gzdopen failed"))
264
268
    end
294
298
 
295
299
    s.name *= " (closed)"
296
300
 
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))
298
302
 
299
303
    return ret
300
304
end
301
305
 
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)
305
309
 
306
310
truncate(s::GZipStream, n::Integer) = error("truncate is not supported for GZipStreams")
307
311
 
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"))
313
317
 
315
319
 
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)
321
325
 
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)
324
328
 
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))
326
330
 
327
331
function check_eof(s::GZipStream)
328
332
    # Force eof to be set...
341
345
                       Int128,Uint128,Float32,Float64,Complex64,Complex128)}(s::GZipStream, a::Array{T})
342
346
    nb = numel(a)*sizeof(T)
343
347
    # Note: this will overflow and succeed without warning if nb > 4GB
344
 
    ret = ccall(dlsym(_zlib, :gzread), Int32,
 
348
    ret = ccall((:gzread, _zlib), Int32,
345
349
                (Ptr{Void}, Ptr{Void}, Uint32), s.gz_file, a, nb)
346
350
    if ret == -1
347
351
        throw(GZError(s))
398
402
readall(s::GZipStream) = readall(s, Z_BIG_BUFSIZE)
399
403
 
400
404
# TODO: Create a c-wrapper based on gzreadline
401
 
function readuntil(s::GZipStream, delim)
402
 
    if delim == '\n'
403
 
        return readline(s)
404
 
    else
405
 
        buf = memio(GZ_LINE_BUFSIZE, false)
406
 
        c = read(s, Char)
407
 
        print(buf, c)
408
 
        while c != delim && !eof(s)
409
 
            try
410
 
                c = read(s, Char)
411
 
                print(buf, c)
412
 
            catch e
413
 
                if !isa(e, EOFError)
414
 
                    throw(e)
415
 
                end
416
 
            end
417
 
        end
418
 
        check_eof(s)
419
 
        takebuf_string(buf)
420
 
    end
421
 
end
422
 
 
423
 
 
424
 
function readline(s::GZipStream)
 
405
function readuntil(s::GZipStream, c::Uint8)
425
406
    buf = Array(Uint8, GZ_LINE_BUFSIZE)
426
407
    pos = 1
427
408
 
428
409
    if gzgets(s, buf) == C_NULL      # Throws an exception on error
429
 
        return ""
 
410
        return buf[1:0]
430
411
    end
431
412
 
432
413
    while(true)
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
 
417
            return buf[1:eos-1]
437
418
        end
438
419
 
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
441
422
 
442
423
        # Otherwise, append to the end of the previous buffer
443
424
 
450
431
        if gzgets(s, pointer(buf)+pos-1, GZ_LINE_BUFSIZE) == C_NULL
451
432
            # eof(s); remove extra buffer space
452
433
            grow(buf, -GZ_LINE_BUFSIZE)
453
 
            return bytestring(buf)
 
434
            return buf
454
435
        end
455
436
    end
456
437
end