Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mf6bmiutil): simplify string_to_char_array #1987

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions srcbmi/mf6bmi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ function bmi_get_component_name(name) result(bmi_status) &
! -- dummy variables
character(kind=c_char), intent(out) :: name(BMI_LENCOMPONENTNAME)
integer(kind=c_int) :: bmi_status !< BMI status code
! -- local variables

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

end function bmi_get_component_name
Expand Down Expand Up @@ -612,7 +611,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) = string_to_char_array(trim(srcstr))

else if (rank == 1) then
! an array of strings
Expand All @@ -634,7 +633,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) = string_to_char_array(trim(tempstr))
end do
deallocate (tempstr)
else
Expand Down Expand Up @@ -1172,7 +1171,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)))
string_to_char_array(trim(mem_type))

if (mem_type == 'UNKNOWN') then
write (bmi_last_error, fmt_general_err) 'unknown memory type'
Expand Down
2 changes: 1 addition & 1 deletion srcbmi/mf6bmiGrid.f90
Original file line number Diff line number Diff line change
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 = string_to_char_array(trim(grid_type_f))
bmi_status = BMI_SUCCESS
end function get_grid_type

Expand Down
14 changes: 4 additions & 10 deletions srcbmi/mf6bmiUtil.f90
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,12 @@ 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)
pure function string_to_char_array(string) 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
character(len=*), intent(in) :: string !< string to convert
character(kind=c_char, len=1) :: c_array(len(string) + 1) !< C-style character string

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

end function string_to_char_array

Expand Down
4 changes: 2 additions & 2 deletions srcbmi/mf6xmi.F90
Original file line number Diff line number Diff line change
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 = string_to_char_array(trim(vstr))
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)))
string_to_char_array(trim(mem_address))

bmi_status = BMI_SUCCESS

Expand Down
Loading