Skip to content

Commit 07c7d62

Browse files
scivisiongtokic
andcommitted
attributes:character: allow writing empty character
fixes #44 Co-authored-by: gtokic <gtokic@users.noreply.github.com>
1 parent 772e5ca commit 07c7d62

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ endif()
1212

1313
project(h5fortran
1414
LANGUAGES C Fortran
15-
VERSION 4.10.2
15+
VERSION 4.10.3
1616
)
1717

1818
include(CTest)

src/attr.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ subroutine attr_create(self, obj_name, attr_name, dtype, attr_dims, attr_id, dty
3030

3131
if(dtype == H5T_NATIVE_CHARACTER) then
3232
if(.not. present(charlen)) error stop "ERROR:h5fortran:attr_create: character type must specify charlen"
33+
if (charlen < 1) error stop "ERROR:h5fortran:attr_create: character type must specify charlen > 0"
3334

3435
call H5Tset_size_f(dtype_id, int(charlen, SIZE_T), ier)
3536
call estop(ier, "attr_create:H5Aset_size", self%filename, obj_name, attr_name)

src/attr_write.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type is (integer(int64))
2020
type is(character(*))
2121
dtype = H5T_NATIVE_CHARACTER
2222
charlen = len(A) !< workaround for GCC 8.3.0 bug
23+
if(charlen == 0) charlen = 1 !< empty string is OK but charlen is strictly positive.
2324
class default
2425
error stop "ERROR:h5fortran:writeattr: unknown dataset type for " // obj_name // ":" // attr // " in " // self%filename
2526
end select

src/read_ascii.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ elemental function pad_trim(s) result(t)
9090

9191
i = index(s, C_NULL_CHAR) - 1
9292
if (i < 0) i = len_trim(s)
93-
if (i == 0) i = 1
9493

95-
t = s(:i)
94+
if (i > 0) then
95+
t = s(1:i)
96+
else
97+
t = ""
98+
endif
9699

97100
end function pad_trim
98101

test/test_attributes.f90

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
program test_attributes
22

33
use, intrinsic:: iso_fortran_env, only: int32, real32, real64, stderr=>error_unit
4+
use, intrinsic:: iso_c_binding
45

56
use h5fortran, only: hdf5_file, h5write_attr, h5read_attr, HSIZE_T
67

@@ -35,7 +36,7 @@ subroutine test_write_attributes(path)
3536

3637
integer :: i2(1,1), i3(1,1,1), i4(1,1,1,1), i5(1,1,1,1,1), i6(1,1,1,1,1,1), i7(1,1,1,1,1,1,1)
3738

38-
call h%open(path, action='w')
39+
call h%open(path, action='w', debug=.true.)
3940

4041
call h%write('/x', 1)
4142

@@ -55,9 +56,11 @@ subroutine test_write_attributes(path)
5556

5657
call h%writeattr("/x", "c1d", [character(5) :: 'one', 'two', 'three'])
5758
call h%writeattr("/x", "c2d", reshape([character(5) :: 'one', 'two', 'three', 'four', 'five', 'six'], [2,3]))
59+
call h%writeattr("/x", "empty_char", "")
5860

5961
call h%close()
6062

63+
!> test overwrite of attributes
6164
call h%open(path, action='a')
6265
call h%writeattr('/x', 'int32-scalar', 142)
6366
call h%writeattr('/x', 'real32_1d', [real(real32) :: 142, 84])
@@ -89,9 +92,19 @@ subroutine test_read_attributes(path)
8992
call h%read('/x', x)
9093
if (x/=1) error stop 'readattr: unexpected value'
9194

95+
!> character scalar
9296
call h%readattr('/x', 'char', attr_str)
9397
if (attr_str /= 'overwrite attrs') error stop 'overwrite attrs failed: ' // attr_str
9498

99+
call h%readattr("/x", "empty_char", attr_str)
100+
print *, trim(attr_str) == c_null_char
101+
if (len_trim(attr_str) /= 0) then
102+
write(stderr, '(a,i0)') "empty char attribute: expected 0 length, got length: ", len_trim(attr_str)
103+
error stop "empty char attribute failed"
104+
endif
105+
if (trim(attr_str) /= "") error stop "empty char attribute failed, got: " // trim(attr_str)
106+
107+
!> scalar numbers
95108
call h%readattr('/x', 'int32-scalar', int32_0)
96109
if (int32_0 /= 142) error stop 'readattr: int32-scalar'
97110

@@ -117,6 +130,8 @@ subroutine test_read_attributes(path)
117130
call h%readattr('/x', 'i6', i6)
118131
call h%readattr('/x', 'i7', i7)
119132

133+
!> character array
134+
120135
call h%shape("/x", dims, "c1d")
121136
if(dims(1) /= 3) error stop "attr % shape: c1d"
122137

0 commit comments

Comments
 (0)