Skip to content

Commit 8f935c4

Browse files
committed
Merge branch 'framework/sanitize_c_null_char' into hotfix-v6.2 (PR #161)
This merge adds code to remove C null characters from strings read by PIO. The PIO2 library fills in unused characters of a string variable with C null characters, and replaces C null characters with spaces when reading strings. However, the PIO1 library does not perform these conversions. Consequently, if a file written by the PIO2 library is read by the PIO1 library, C null characters may be present in the resulting Fortran string. Under the assumption that C null characters will generally not be expected or handled in MPAS (e.g., in timestamp strings), this commit makes calls to MPAS_sanitize_string to convert any C null characters in strings read by PIO into spaces. * framework/sanitize_c_null_char: Remove C null characters from strings read by PIO Add new MPAS_sanitize_string routine to mpas_c_interfacing
2 parents 5ef9bc2 + 9bd56e6 commit 8f935c4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/framework/mpas_c_interfacing.F

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@ module mpas_c_interfacing
44
contains
55

66

7+
!-----------------------------------------------------------------------
8+
! routine mpas_sanitize_string
9+
!
10+
!> \brief Converts C null characters in a Fortran string to spaces
11+
!> \author Michael Duda
12+
!> \date 19 February 2019
13+
!> \details
14+
!> Converts all C null characters in a Fortran string to spaces.
15+
!> This may be useful for strings that were provided by C code through other
16+
!> Fortran code external to MPAS.
17+
!
18+
!-----------------------------------------------------------------------
19+
subroutine mpas_sanitize_string(str)
20+
21+
use iso_c_binding, only : c_null_char
22+
23+
implicit none
24+
25+
character(len=*), intent(inout) :: str
26+
27+
integer :: i
28+
29+
do i=1,len(str)
30+
if (str(i:i) == c_null_char) then
31+
str(i:i) = ' '
32+
end if
33+
end do
34+
35+
end subroutine mpas_sanitize_string
36+
37+
738
!-----------------------------------------------------------------------
839
! routine mpas_c_to_f_string
940
!

src/framework/mpas_io.F

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,8 @@ end subroutine MPAS_io_get_var_real5d
21872187

21882188
subroutine MPAS_io_get_var_char0d(handle, fieldname, val, ierr)
21892189

2190+
use mpas_c_interfacing, only : MPAS_sanitize_string
2191+
21902192
implicit none
21912193

21922194
type (MPAS_IO_Handle_type), intent(inout) :: handle
@@ -2198,23 +2200,31 @@ subroutine MPAS_io_get_var_char0d(handle, fieldname, val, ierr)
21982200
if (present(ierr)) ierr = MPAS_IO_NOERR
21992201

22002202
call MPAS_io_get_var_generic(handle, fieldname, charVal=val, ierr=ierr)
2203+
call MPAS_sanitize_string(val)
22012204

22022205
end subroutine MPAS_io_get_var_char0d
22032206

22042207

22052208
subroutine MPAS_io_get_var_char1d(handle, fieldname, val, ierr)
22062209

2210+
use mpas_c_interfacing, only : MPAS_sanitize_string
2211+
22072212
implicit none
22082213

22092214
type (MPAS_IO_Handle_type), intent(inout) :: handle
22102215
character (len=*), intent(in) :: fieldname
22112216
character (len=*), dimension(:), intent(out) :: val
22122217
integer, intent(out), optional :: ierr
22132218

2219+
integer :: i
2220+
22142221
! call mpas_log_write('Called MPAS_io_get_var_char1d()')
22152222
if (present(ierr)) ierr = MPAS_IO_NOERR
22162223

22172224
call MPAS_io_get_var_generic(handle, fieldname, charArray1d=val, ierr=ierr)
2225+
do i=1,size(val)
2226+
call MPAS_sanitize_string(val(i))
2227+
end do
22182228

22192229
end subroutine MPAS_io_get_var_char1d
22202230

0 commit comments

Comments
 (0)