diff --git a/src/MAT.jl b/src/MAT.jl index f2076cf..c96e9e2 100644 --- a/src/MAT.jl +++ b/src/MAT.jl @@ -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") @@ -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) @@ -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 diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index 09b24e2..2a8df57 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -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 @@ -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 @@ -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 diff --git a/src/MAT_v5.jl b/src/MAT_v5.jl index 1f742eb..df1934c 100644 --- a/src/MAT_v5.jl +++ b/src/MAT_v5.jl @@ -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 @@ -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]) @@ -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 @@ -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") @@ -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 diff --git a/test/read.jl b/test/read.jl index a84ad2a..65ef7de 100644 --- a/test/read.jl +++ b/test/read.jl @@ -1,6 +1,4 @@ using MAT, Base.Test -using Compat -using Compat.String function check(filename, result) matfile = matopen(filename) @@ -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" => "" ) diff --git a/test/write.jl b/test/write.jl index 407d41e..226390c 100644 --- a/test/write.jl +++ b/test/write.jl @@ -1,5 +1,4 @@ using MAT -using Compat tmpfile = string(tempname, ".mat") @@ -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)