Skip to content

Commit 183791e

Browse files
committed
read character: automatically truncate to c_null_char if present
1 parent 20b1e45 commit 183791e

3 files changed

Lines changed: 17 additions & 22 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.3
1+
1.4.4

src/reader.in.f90

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
submodule (nc4fortran:read) reader
22
!! This submodule is for reading 0-D..7-D data
33

4+
use, intrinsic :: iso_c_binding, only : c_null_char
45
implicit none (type, external)
56

67
contains
78

89
module procedure nc_read_scalar
9-
integer :: varid, ier
10+
integer :: varid, ier, i
11+
integer, allocatable :: dims(:)
1012

1113
if(.not.self%is_open) error stop 'nc4fortran:reader file handle not open'
1214

@@ -17,15 +19,14 @@
1719
type is (character(*))
1820
!! NetCDF4 requires knowing the exact character length as if it were an array without fill values
1921
!! HDF5 is not this strict; having a longer string variable than disk variable is OK in HDF5, but not NetCDF4
20-
charshape : block
21-
integer, allocatable :: dims(:)
22-
call self%shape(dname, dims)
23-
charbuf : block
24-
character(len=dims(1)) :: buf
25-
ier = nf90_get_var(self%ncid, varid, buf)
26-
value = buf
27-
end block charbuf
28-
end block charshape
22+
call self%shape(dname, dims)
23+
charbuf : block
24+
character(len=dims(1)) :: buf
25+
ier = nf90_get_var(self%ncid, varid, buf)
26+
i = index(buf, c_null_char) - 1
27+
if(i == -1) i = len_trim(buf)
28+
value = buf(:i)
29+
end block charbuf
2930
type is (real(real64))
3031
ier = nf90_get_var(self%ncid, varid, value)
3132
type is (real(real32))

src/tests/test_string.f90

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ program test_string
1212

1313
character(2) :: value
1414
character(1024) :: val1k
15-
character(:), allocatable :: final
16-
integer :: i
1715
character(*), parameter :: path='test_string.nc'
1816

1917
call h%open(path, action='w')
@@ -26,23 +24,19 @@ program test_string
2624
call h%open(path, action='r')
2725
call h%read('little', value)
2826

29-
if (value /= '42') then
30-
write(stderr,*) 'test_string: read/write verification failure. Value: '// value
31-
error stop
32-
endif
27+
if (value /= '42') error stop 'test_string: read/write verification failure. Value: '// value
3328

3429
print *,'test_string_rw: reading too much data'
3530
!! try reading too much data, then truncating to first C_NULL
3631
call h%read('little', val1k)
37-
i = index(val1k, c_null_char)
38-
final = val1k(:i-1)
3932

40-
if (len(final) /= 2) then
41-
write(stderr, *) 'trimming str to c_null did not work, got len() = ', len(final)
42-
write(stderr, *) iachar(final(3:3))
33+
if (len_trim(val1k) /= 2) then
34+
write(stderr, *) 'expected len_trim 2, got len_trim = ', len(val1k)
4335
error stop
4436
endif
4537

4638
call h%close()
4739

40+
print *, 'OK: test_string'
41+
4842
end program

0 commit comments

Comments
 (0)