Converts a C string to a Fortran string
A From a memory viewpoint, a C string is like a character vector ending with a C_NULL_CHAR
, so as long as
it is not found, the characters are copied one by one in a fortran string
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=C_CHAR), | intent(in), | dimension(:) | :: | cs | C string |
|
character(len=*), | intent(out) | :: | fs | Fortran string |
||
integer(kind=I4), | intent(out) | :: | borne_s | resulting Fortran string length |
subroutine c_f_string(cs, fs, borne_s)
implicit none
character(kind=C_CHAR), dimension(:), intent(in) :: cs !! *C string*
character(len=*), intent(out) :: fs !! *Fortran string*
integer(kind=I4), intent(out) :: borne_s !! *resulting Fortran string length*
integer(kind=I4) :: i, ucs
ucs = size(cs) ! vector length
borne_s = ucs ! resulting string default length
i = 1
do
if (i>ucs) exit
if (cs(i)==C_NULL_CHAR) then ! fin de chaîne c rencontrée ; s'il n'y a pas de null_char
borne_s = i-1 ! c'est qu'on utilise tout le vecteur
exit
endif
i = i + 1
enddo
call empty(fs)
do i = 1, borne_s ! the C string is translated into fortran
fs(i:i) = cs(i)
enddo
return
endsubroutine c_f_string