Skip to content

Commit 574bb40

Browse files
scivisiongtokic
andcommitted
dataset:character: allow writing empty character
Co-authored-by: gtokic <gtokic@users.noreply.github.com>
1 parent 07c7d62 commit 574bb40

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

src/write.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@
6565

6666
if(dtype == H5T_NATIVE_CHARACTER) then
6767
if(.not. present(charlen)) error stop "ERROR:h5fortran:hdf_create: character type must specify charlen"
68+
if (charlen < 1) error stop "ERROR:h5fortran:attr_create: character type must specify charlen > 0"
6869

69-
call h5tset_size_f(dtype_id, int(charlen, SIZE_T), ier)
70+
call H5Tset_size_f(dtype_id, int(charlen, SIZE_T), ier)
7071
call estop(ier, "create:H5Tset_size", self%filename, dname)
7172
endif
7273

src/write_scalar.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
type is (character(*))
2828
dtype = H5T_NATIVE_CHARACTER
2929
charlen = len(A) !< workaround for GCC 8.3.0 bug
30+
if(charlen == 0) charlen = 1 !< empty string is OK but charlen is strictly positive.
3031
class default
3132
error stop "ERROR:h5fortran:write: unknown variable type for " // dname
3233
end select

src/writer.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type is (integer(int64))
2424
type is (character(*))
2525
dtype = H5T_NATIVE_CHARACTER
2626
charlen = len(A) !< workaround for GCC 8.3.0 bug
27+
if(charlen == 0) charlen = 1 !< empty string is OK but charlen is strictly positive.
2728
class default
2829
error stop "ERROR:h5fortran:writer:unknown variable type for " // dname
2930
end select

test/test_attributes.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ subroutine test_write_attributes(path)
5454
call h%writeattr('/x', 'i6', i6)
5555
call h%writeattr('/x', 'i7', i7)
5656

57+
call h%writeattr("/x", "1d_emptyp", [character(1) :: "", ""])
5758
call h%writeattr("/x", "c1d", [character(5) :: 'one', 'two', 'three'])
5859
call h%writeattr("/x", "c2d", reshape([character(5) :: 'one', 'two', 'three', 'four', 'five', 'six'], [2,3]))
5960
call h%writeattr("/x", "empty_char", "")

test/test_string.f90

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ subroutine test_write(fn)
2929

3030
type(hdf5_file) :: h
3131

32-
call h%open(fn, action='w')
32+
call h%open(fn, action='w', debug=.true.)
3333

34+
call h%write("/empty", "")
3435
call h%write('/little', '42')
3536
call h%write('/MySentence', 'this is a little sentence.')
3637
call h%write('/vector_scalar', ['vector scalar'])
38+
39+
call h%write("/1d_empty", [character(1) :: ""])
3740
call h%write("/1d", [character(3) :: "hi", "bye"])
3841
call h%write("/2d", reshape([character(5) :: "one", "two", "three", "four", "five", "six"], [2,3]))
3942

@@ -55,6 +58,10 @@ subroutine test_read(fn)
5558

5659
call h%open(fn, action='r')
5760

61+
call h%read('/empty', value)
62+
if(len_trim(value) /= 0) error stop 'test_string: empty string failure: len_trim /= 0'
63+
if (value /= '') error stop 'test_string: empty string failure: value = ' // trim(value)
64+
5865
call h%read('/little', value)
5966

6067
if(len_trim(value) /= 2) then

0 commit comments

Comments
 (0)