Skip to content

Commit

Permalink
Merge pull request #74 from JuliaIO/yyc/0.7
Browse files Browse the repository at this point in the history
Fix test errors and most of 0.7 depwarns
  • Loading branch information
yuyichao authored Jul 24, 2017
2 parents 03eadbc + a5551d8 commit 9282d61
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/MAT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ VERSION >= v"0.4.0-dev+6521" && __precompile__()
module MAT

using HDF5, Compat
using Compat.String

include("MAT_HDF5.jl")
include("MAT_v5.jl")
Expand All @@ -54,7 +53,7 @@ function matopen(filename::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Boo
rawfid = open(filename, "r")

# Check for MAT v4 file
magic = read(rawfid, UInt8, 4)
magic = read!(rawfid, Vector{UInt8}(4))
for i = 1:length(magic)
if magic[i] == 0
close(rawfid)
Expand All @@ -77,7 +76,7 @@ function matopen(filename::AbstractString, rd::Bool, wr::Bool, cr::Bool, tr::Boo
# Check for HDF5 file
for offset = 512:512:fs-8
seek(rawfid, offset)
if read(rawfid, UInt8, 8) == HDF5_HEADER
if read!(rawfid, Vector{UInt8}(8)) == HDF5_HEADER
close(rawfid)
return MAT_HDF5.matopen(filename, rd, wr, cr, tr, ff)
end
Expand Down
14 changes: 7 additions & 7 deletions src/MAT_HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@

module MAT_HDF5

using HDF5, Compat
using Compat.String
using HDF5

import Base: read, write, close
import HDF5: names, exists, HDF5ReferenceObj, HDF5BitsKind
Expand Down Expand Up @@ -123,16 +122,17 @@ function read_complex{T}(dtype::HDF5Datatype, dset::HDF5Dataset, ::Type{Array{T}
end
memtype = build_datatype_complex(T)
sz = size(dset)
dbuf = Array{T}(2, sz...)
st = sizeof(T)
buf = Array{UInt8}(2*st, sz...)
buf = reinterpret(UInt8, dbuf, (2 * st, sz...))
HDF5.h5d_read(dset.id, memtype.id, HDF5.H5S_ALL, HDF5.H5S_ALL, HDF5.H5P_DEFAULT, buf)

if T == Float32
d = reinterpret(Complex64, buf, sz)
d = reinterpret(Complex64, dbuf, sz)
elseif T == Float64
d = reinterpret(Complex128, buf, sz)
d = reinterpret(Complex128, dbuf, sz)
else
d = reinterpret(T, slicedim(buf, 1, 1:st), sz) + im*reinterpret(T, slicedim(buf, 1, st+1:2*st), sz)
d = slicedim(dbuf, 1, 1) + im * slicedim(dbuf, 1, 2)
end
length(d) == 1 ? d[1] : d
end
Expand Down Expand Up @@ -592,7 +592,7 @@ function read(obj::HDF5Object, ::Type{MatlabString})
if ndims(data) == 1
return convert(String, convert(Vector{Char}, data))
elseif ndims(data) == 2
return datap = Compat.String[rstrip(convert(String, convert(Vector{Char}, vec(data[i, :])))) for i = 1:size(data, 1)]
return datap = String[rstrip(convert(String, convert(Vector{Char}, vec(data[i, :])))) for i = 1:size(data, 1)]
else
return data
end
Expand Down
13 changes: 6 additions & 7 deletions src/MAT_v5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
# http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf

module MAT_v5
using Libz, BufferedStreams, HDF5, Compat
using Compat.String
using Libz, BufferedStreams, HDF5
import Base: read, write, close
import HDF5: names, exists

Expand Down Expand Up @@ -88,7 +87,7 @@ const CONVERT_TYPES = Type[
read_bswap{T}(f::IO, swap_bytes::Bool, ::Type{T}) =
swap_bytes ? bswap(read(f, T)) : read(f, T)
function read_bswap{T}(f::IO, swap_bytes::Bool, ::Type{T}, dim::Union{Int, Tuple{Vararg{Int}}})
d = read(f, T, dim)
d = read!(f, Array{T}(dim))
if swap_bytes
for i = 1:length(d)
@inbounds d[i] = bswap(d[i])
Expand Down Expand Up @@ -254,11 +253,11 @@ function read_string(f::IO, swap_bytes::Bool, dimensions::Vector{Int32})
# would be ISO-8859-1 and not UTF-8. However, MATLAB 2012b always saves strings with
# a 2-byte encoding in v6 format, and saves UTF-8 in v7 format. Thus, this may never
# happen in the wild.
chars = read(f, UInt8, nbytes)
chars = read!(f, Vector{UInt8}(nbytes))
if dimensions[1] <= 1
data = String(chars)
else
data = Vector{Compat.String}(dimensions[1])
data = Vector{String}(dimensions[1])
for i = 1:dimensions[1]
data[i] = rstrip(String(chars[i:dimensions[1]:end]))
end
Expand Down Expand Up @@ -287,7 +286,7 @@ function read_string(f::IO, swap_bytes::Bool, dimensions::Vector{Int32})
elseif dimensions[1] == 1
data = String(take!(bufs[1]))
else
data = [rstrip(String(take!(buf))) for buf in bufs]
data = String[rstrip(String(take!(buf))) for buf in bufs]
end
else
error("Unsupported string type")
Expand All @@ -300,7 +299,7 @@ end
function read_matrix(f::IO, swap_bytes::Bool)
(dtype, nbytes) = read_header(f, swap_bytes)
if dtype == miCOMPRESSED
return read_matrix(ZlibInflateInputStream(read(f, UInt8, nbytes)), swap_bytes)
return read_matrix(ZlibInflateInputStream(read!(f, Vector{UInt8}(nbytes))), swap_bytes)
elseif dtype != miMATRIX
error("Unexpected data type")
elseif nbytes == 0
Expand Down
4 changes: 1 addition & 3 deletions test/read.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using MAT, Base.Test
using Compat
using Compat.String

function check(filename, result)
matfile = matopen(filename)
Expand Down Expand Up @@ -80,7 +78,7 @@ for format in ["v6", "v7", "v7.3"]
result = Dict(
"simple_string" => "the quick brown fox",
"accented_string" => "thé qüîck browñ fòx",
"concatenated_strings" => Compat.String["this is a string", "this is another string"],
"concatenated_strings" => String["this is a string", "this is another string"],
"cell_strings" => Any["this is a string" "this is another string"],
"empty_string" => ""
)
Expand Down
3 changes: 1 addition & 2 deletions test/write.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MAT
using Compat

tmpfile = string(tempname, ".mat")

Expand Down Expand Up @@ -104,6 +103,6 @@ sd = SortedDict(Dict(
"Complex128" => [1.0 -1.0 1.0+1.0im 1.0-1.0im -1.0+1.0im -1.0-1.0im 1.0im],
"simple_string" => "the quick brown fox",
"a1x2" => [1.0 2.0],
"sparse_empty" => sparse(Array(Float64, 0, 0))
"sparse_empty" => sparse(Matrix{Float64}(0, 0))
))
test_write(sd)

0 comments on commit 9282d61

Please sign in to comment.