Skip to content

Commit

Permalink
Merge f71b10a into 13f7ad6
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jul 28, 2017
2 parents 13f7ad6 + f71b10a commit 98ff94d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
julia 0.5
BinDeps 0.6.0
Blosc
Compat 0.17.0
Compat 0.24.0
@osx Homebrew 0.3.1
@windows WinRPM
30 changes: 15 additions & 15 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module HDF5
using Compat

using Base: unsafe_convert
using Compat: StringVector

import Base:
close, convert, done, eltype, endof, flush, getindex, ==,
Expand Down Expand Up @@ -1330,7 +1331,7 @@ function read{S<:String}(obj::DatasetOrAttribute, ::Type{S})
objtype = datatype(obj)
try
if h5t_is_variable_str(objtype.id)
buf = Ptr{UInt8}[C_NULL]
buf = Cstring[C_NULL]
memtype_id = h5t_copy(H5T_C_S1)
h5t_set_size(memtype_id, H5T_VARIABLE)
h5t_set_cset(memtype_id, h5t_get_cset(datatype(obj)))
Expand Down Expand Up @@ -1371,10 +1372,10 @@ function read{S<:String}(obj::DatasetOrAttribute, ::Type{Array{S}})
ret = Array{S}(sz)
if isvar
# Variable-length
buf = Vector{Ptr{UInt8}}(len)
buf = Vector{Cstring}(len)
h5t_set_size(memtype_id, H5T_VARIABLE)
readarray(obj, memtype_id, buf)
# FIXME? Who owns the memory for each string? Will Julia free it?
# FIXME? Who owns the memory for each string? Julia v0.6+ won't free it.
for i = 1:len
ret[i] = unsafe_string(buf[i])
end
Expand All @@ -1384,10 +1385,13 @@ function read{S<:String}(obj::DatasetOrAttribute, ::Type{Array{S}})
buf = Vector{UInt8}(len*ilen)
h5t_set_size(memtype_id, ilen)
readarray(obj, memtype_id, buf)
p = pointer(buf)
src = 1
for i = 1:len
ret[i] = unsafe_string(p)
p += ilen
slen = findnext(buf, 0x00, src) - src # find null terminator
sv = StringVector(slen)
copy!(sv, 1, buf, src, slen)
ret[i] = String(sv)
src += ilen
end
end
end
Expand Down Expand Up @@ -1902,11 +1906,7 @@ function h5a_write{T<:HDF5Scalar}(attr_id::Hid, mem_type_id::Hid, x::T)
h5a_write(attr_id, mem_type_id, tmp)
end
function h5a_write{S<:String}(attr_id::Hid, memtype_id::Hid, strs::Array{S})
len = length(strs)
p = Array{Ptr{UInt8}}(size(strs))
for i = 1:len
p[i] = pointer(strs[i])
end
p = Ref{Cstring}(strs)
h5a_write(attr_id, memtype_id, p)
end
function h5a_write{T<:Union{HDF5Scalar,CharType}}(attr_id::Hid, memtype_id::Hid, v::HDF5Vlen{T})
Expand All @@ -1926,11 +1926,11 @@ function h5d_write{T<:HDF5Scalar}(dataset_id::Hid, memtype_id::Hid, x::T)
h5d_write(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp)
end
function h5d_write{S<:String}(dataset_id::Hid, memtype_id::Hid, strs::Array{S})
len = length(strs)
p = Array{Ptr{UInt8}}(size(strs))
for i = 1:len
p[i] = !isassigned(strs, i) || isempty(strs[i]) ? pointer(EMPTY_STRING) : pointer(strs[i])
nonnull_str = copy(strs)
for i = 1:length(nonnull_str)
isassigned(nonnull_str, i) || (nonnull_str[i] = "")
end
p = Ref{Cstring}(nonnull_str)
h5d_write(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, p)
end
function h5d_write{T<:Union{HDF5Scalar,CharType}}(dataset_id::Hid, memtype_id::Hid, v::HDF5Vlen{T})
Expand Down
8 changes: 8 additions & 0 deletions test/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,12 @@ end

@test HDF5.unpad(UInt8[0x43, 0x43, 0x41], 1) == "CCA"

# don't silently truncate data
fn = tempname()
f = h5open(fn, "w")
@test_throws ArgumentError write(f, "test", ["hello","there","\0"])
# @test_throws ArgumentError write(f, "trunc", "\0")
close(f)
rm(fn)

end # testset plain

0 comments on commit 98ff94d

Please sign in to comment.