Skip to content

Commit

Permalink
refactor(mf6bmiutil): remove string_to_char_array function
Browse files Browse the repository at this point in the history
This function is evidently no longer necessary. The Fortran wiki claims it is (https://fortranwiki.org/fortran/show/Generating+C+Interfaces#strings), but the relevant section was added 14 years ago (https://fortranwiki.org/fortran/revision/diff/Generating+C+Interfaces/9), and more recently the consensus on the fortran-lang forum is that passing 'trim(str) // c_null_char' suffices (https://fortran-lang.discourse.group/t/best-practices-for-passing-c-strings)
  • Loading branch information
wpbonelli committed Aug 9, 2024
1 parent 97757fa commit 263e87a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
8 changes: 4 additions & 4 deletions srcbmi/mf6bmi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function bmi_get_component_name(name) result(bmi_status) &
integer(kind=c_int) :: bmi_status !< BMI status code
! -- local variables

name = string_to_char_array('MODFLOW 6', 9)
name = 'MODFLOW 6'//c_null_char
bmi_status = BMI_SUCCESS

end function bmi_get_component_name
Expand Down Expand Up @@ -612,7 +612,7 @@ function get_value_string(c_var_address, c_arr_ptr) result(bmi_status) &
call mem_setptr(srcstr, var_name, mem_path)
call get_mem_elem_size(var_name, mem_path, ilen)
call c_f_pointer(c_arr_ptr, tgtstr, shape=[ilen + 1])
tgtstr(1:len(srcstr) + 1) = string_to_char_array(srcstr, len(srcstr))
tgtstr(1:len(srcstr) + 1) = trim(srcstr)//c_null_char

else if (rank == 1) then
! an array of strings
Expand All @@ -634,7 +634,7 @@ function get_value_string(c_var_address, c_arr_ptr) result(bmi_status) &
allocate (character(ilen) :: tempstr)
do i = 1, isize
tempstr = srccharstr1d(i)
tgtstr1d(1:ilen + 1, i) = string_to_char_array(tempstr, ilen)
tgtstr1d(1:ilen + 1, i) = trim(tempstr)//c_null_char
end do
deallocate (tempstr)
else
Expand Down Expand Up @@ -1172,7 +1172,7 @@ function get_var_type(c_var_address, c_var_type) result(bmi_status) &

call get_mem_type(var_name, mem_path, mem_type)
c_var_type(1:len(trim(mem_type)) + 1) = &
string_to_char_array(trim(mem_type), len(trim(mem_type)))
trim(mem_type)//c_null_char

if (mem_type == 'UNKNOWN') then
write (bmi_last_error, fmt_general_err) 'unknown memory type'
Expand Down
4 changes: 2 additions & 2 deletions srcbmi/mf6bmiGrid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module mf6bmiGrid
use mf6bmiUtil
use mf6bmiError
use iso_c_binding, only: c_double, c_ptr, c_loc
use iso_c_binding, only: c_double, c_ptr, c_loc, c_null_char
use ConstantsModule, only: LENMODELNAME, LENMEMPATH
use KindModule, only: DP, I4B
use MemoryManagerModule, only: mem_setptr
Expand Down Expand Up @@ -81,7 +81,7 @@ function get_grid_type(grid_id, grid_type) result(bmi_status) &
else
return
end if
grid_type = string_to_char_array(trim(grid_type_f), len_trim(grid_type_f))
grid_type = trim(grid_type_f)//c_null_char
bmi_status = BMI_SUCCESS
end function get_grid_type

Expand Down
17 changes: 0 additions & 17 deletions srcbmi/mf6bmiUtil.f90
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,6 @@ pure function char_array_to_string(char_array, length) result(f_string)

end function char_array_to_string

!> @brief Convert Fortran string to C-style character string
!<
pure function string_to_char_array(string, length) result(c_array)
! -- dummy variables
integer(c_int), intent(in) :: length !< Fortran string length
character(len=length), intent(in) :: string !< string to convert
character(kind=c_char, len=1) :: c_array(length + 1) !< C-style character string
! -- local variables
integer(I4B) :: i

do i = 1, length
c_array(i) = string(i:i)
end do
c_array(length + 1) = C_NULL_CHAR

end function string_to_char_array

!> @brief Extract the model name from a memory address string
!<
function extract_model_name(var_address, success) result(model_name)
Expand Down
6 changes: 3 additions & 3 deletions srcbmi/mf6xmi.F90
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module mf6xmi
use mf6bmiError
use Mf6CoreModule
use KindModule
use iso_c_binding, only: c_int, c_char, c_double
use iso_c_binding, only: c_int, c_char, c_double, c_null_char
implicit none

integer(I4B), pointer :: iterationCounter => null() !< the counter for the outer iteration loop, initialized in xmi_prepare_iteration()
Expand Down Expand Up @@ -357,7 +357,7 @@ function xmi_get_version(mf_version) result(bmi_status) &
else
vstr = VERSIONNUMBER
end if
mf_version = string_to_char_array(vstr, len_trim(vstr))
mf_version = trim(vstr)//c_null_char
bmi_status = BMI_SUCCESS

end function xmi_get_version
Expand Down Expand Up @@ -412,7 +412,7 @@ function get_var_address(c_component_name, c_subcomponent_name, &

! convert to c string:
c_var_address(1:len(trim(mem_address)) + 1) = &
string_to_char_array(trim(mem_address), len(trim(mem_address)))
trim(mem_address)//c_null_char

bmi_status = BMI_SUCCESS

Expand Down

0 comments on commit 263e87a

Please sign in to comment.