Converts a Fortran string to a C string
A From a memory viewpoint, a C string is like a character vector ending with a C_NULL_CHAR
,
so the characters are copied one by one in a C_CHAR
vector that ends with a C_NULL_CHAR
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fs | fortran string |
||
character(kind=C_CHAR), | intent(out), | dimension(:) | :: | cs | resulting C string |
subroutine f_c_string(fs, cs)
implicit none
character(len=*), intent(in) :: fs !! *fortran string*
character(kind=C_CHAR), dimension(:), intent(out) :: cs !! *resulting C string*
integer(kind=I4) :: i, ufs
ufs = len_trim(fs) ! longueur de la chaîne fortran sans les null, les blancs, ...
if (ufs==0) then ! si la chaîne est vide
cs(1) = C_NULL_CHAR
else
do i = 1, ufs
cs(i) = fs(i:i)
enddo
if (ufs<size(cs)) cs(ufs+1) = C_NULL_CHAR ! si la fin du vecteur n'est pas atteinte
endif
return
endsubroutine f_c_string